| Class REXML::Element |
|
Represents a tagged XML element. Elements are characterized by having children, attributes, and names, and can themselves be children.
タグの付いたXML要素を表現します。Elementsは子や属性、名前を持つこ とによって特徴づけられ、それ自身が子になることができます。
| Methods |
| Attributes |
| :attributes | [R] | Mechanisms for accessing attributes and child elements of this element. この要素のattributesと子のelementsにアクセスする手段です。 |
| :context | [RW] | The context holds information about the processing environment, such as whitespace handling. contextは、空白の取り扱いのような、処理環境についての情報を持っ ています。 |
| :elements | [R] | Mechanisms for accessing attributes and child elements of this element. この要素のattributesと子のelementsにアクセスする手段です。 |
| Included modules |
| Public Class methods |
| new( arg = UNDEFINED, parent=nil, context=nil ) |
Constructor
| arg: | if not supplied, will be set to the default value. If a String, the name of this object will be set to the argument. If an Element, the object will be shallowly cloned; name, attributes, and namespaces will be copied. Children will not be copied. If a Source, the source will be scanned and parsed for an Element, and all child elements will be recursively parsed as well. |
| parent: | if supplied, must be a Parent, and will be used as the parent of this object. |
| context: | If supplied, must be a hash containing context items. Context items include: |
コンストラクタ
| arg: | もし、与えられていなければ、デフォルトの値が設定されるでしょう。 もし、Stringなら、このオブジェクトの名前はargに設定されるでしょ う。もし、Elementなら、このオブジェクトは浅くコピーされるでしょ う。;name、attributes、namespacesがコピーされるでしょう。子は コピーされないでしょう。もし、Sourceなら、そのソースはElement を探して調べられ、その上、全ての子の要素が再帰的に調べられるで しょう。 |
| parent: | もし、与えられていれば、Parentでなければいけません。そして、こ のオブジェクトの親として使われるでしょう。 |
| context: | もし、与えられていれば、ハッシュを含んだコンテキスト項目でなけ ればいけません。コンテキスト項目は以下のものを含みます。: |
オーバーライドします。
| parse_stream(source, listener) |
Parses a stream containing a single top-level element and possibly multiple children.
| source: | A REXML::Source to read from |
| listener: | A REXML::StreamListener (or something that implements the same method set) that will be notified when events occur. |
s = Source.new( "<a>some text</a>" )
l = Listener.new
class << l
def tag_start name, attributes
p "NEW ELEMENT, NAME OF #{name}"
end
end
Element::parse_stream s, l
一つの最高位の要素と、たぶん複数の子を含むストリームをパースしま す。
| source: | REXML::Sourceが読みます。 |
| listener: | イベントが起こったとき、REXML::StreamListener(あるいは同じメソッ |
ドセットをインプリメントしたもの)に通知されるでしょう。
s = Source.new( "<a>some text</a>" )
l = Listener.new
class << l
def tag_start name, attributes
p "NEW ELEMENT, NAME OF #{name}"
end
end
Element::parse_stream s, l
| Public Instance methods |
| clone() |
Creates a shallow copy of self.
d = Document.new "<a><b/><b/><c><d/></c></a>" new_a = d.root.clone puts new_a # => "<a/>"
自分自身の浅いコピーを作ります。
d = Document.new "<a><b/><b/><c><d/></c></a>" new_a = d.root.clone puts new_a # => "<a/>"
| root() |
Evaluates to the root element of the document that this element belongs to. If this element doesn't belong to a document, but does belong to another Element, the parent's root will be returned, until the earliest ancestor is found.
d = Document.new '<a><b><c/></b></a>' a = d[1] ; c = a[1][1] d.root # These all evaluate to the same Element, a.root # namely, <a> c.root #
要素が属するXML文書のルート要素を評価します。もし、要素がXML文書 に属していないが、別のElementに属しているときは、最も古い祖先が 見つかるまで親のルート要素を返すでしょう。 を返すでしょう。
d = Document.new '<a><b><c/></b></a>' a = d[1] ; c = a[1][1] d.root # 同じElementのこれらの全てを評価します。 a.root # すなわち、<a> c.root #
| whitespace() |
Evaluates to true if whitespace is respected for this element. This is the case if:
The evaluation is tested against expanded_name, and so is namespace sensitive.
もし、要素が空白を考慮するならtrueと評価します。これは以下のよ うな場合です。:
それゆえ、expanded_nameに対するテストの評価は、ネームスペース に敏感です。
| raw() |
Evaluates to true if raw mode is set for this element. This is the case if the context has :raw set to :all or an array containing the name of this element.
The evaluation is tested against expanded_name, and so is namespace sensitive.
もし、要素にrawモードが設定されていればtrueと評価します。これ はコンテキストが:allまたは、要素の名前を含んでいる配列に:raw が設定されている場合です。
それゆえ、expanded_nameに対するテストの評価は、ネームスペース に敏感です。
| prefixes() |
Evaluates to an Array containing the prefixes (names) of all defined namespaces at this context node.
doc = Document.new("<a xmlns:x='1' xmlns:y='2'><b/><c xmlns:z='3'/></a>")
doc.elements['//b'].prefixes # -> ['x', 'y']
このコンテキストノードに定義された全てのネームスペースの接頭語 (名前)を含んだArrayとして評価します。
doc = Document.new("<a xmlns:x='1' xmlns:y='2'><b/><c xmlns:z='3'/></a>")
doc.elements['//b'].prefixes # -> ['x', 'y']
| namespace(prefix=nil) |
Evalutas to the URI for a prefix, or the empty string if no such namespace is declared for this element. Evaluates recursively for ancestors. Returns the default namespace, if there is one.
| prefix: | the prefix to search for. If not supplied, returns the default namespace if one exists |
| Returns: | the namespace URI as a String, or nil if no such namespace exists. |
doc = Document.new("<a xmlns='1' xmlns:y='2'><b/><c xmlns:z='3'/></a>")
b = doc.elements['//b']
b.namespace # -> '1'
b.namespace("y") # -> '2'
接頭語のURIとして評価します。もし、この要素にネームペースが宣言 されていなければ空文字と評価します。祖先を再帰的に評価します。も し、祖先がなければ、デフォルトのネームスペースを返します。
| prefix: | 検索する接頭語です。もし、与えられなければ、存在するならば、デ フォルトのネームスペースを返します。 |
| 戻り値: | ネームスペースのURIをStringとして返します。もし、ネームスペー スが存在しなければnilを返します。 |
doc = Document.new("<a xmlns='1' xmlns:y='2'><b/><c xmlns:z='3'/></a>")
b = doc.elements['//b']
b.namespace # -> '1'
b.namespace("y") # -> '2'
| add_namespace( prefix, uri=nil ) |
Adds a namespace to this element.
| prefix: | the prefix string, or the namespace URI if uri is not supplied |
| uri: | the namespace URI. May be nil, in which prefix is used as the URI |
Evaluates to: this Element
a = Element.new("a")
a.add_namespace("foo", "bar")
a.add_namespace("twiddle")
puts a #-> <a xmlns:foo='bar' xmlns='twiddle'/>
要素にネームスペースを加えます。
| prefix: | 接頭語です。もし、uriが与えられなければネームスペースのURIと なります。 |
| uri: | ネームスペースのURIです。prefixをURIとして使いたい時はnilか も知れません。 |
評価結果: このElementを返します。
a = Element.new("a")
a.add_namespace("foo", "bar")
a.add_namespace("twiddle")
puts a #-> <a xmlns:foo='bar' xmlns='twiddle'/>
| delete_namespace(namespace="xmlns") |
Removes a namespace from this node. This only works if the namespace is actually declared in this node. If no argument is passed, deletes the default namespace.
Evaluates to: this element
doc = Document.new "<a xmlns:foo='bar' xmlns='twiddle'/>" doc.root.delete_namespace puts doc # -> <a xmlns:foo='bar'/> doc.root.delete_namespace 'foo' puts doc # -> <a/>
このノードからネームスペースを取り除きます。これは、このノードで 実際にネームスペースが宣言されているときだけ働きます。もし、引数 が渡されなかったときは、デフォルトのネームスペースを削除します。
評価結果: このElementを返します。
doc = Document.new "<a xmlns:foo='bar' xmlns='twiddle'/>" doc.root.delete_namespace puts doc # -> <a xmlns:foo='bar'/> doc.root.delete_namespace 'foo' puts doc # -> <a/>
| add_element(element=nil, attrs=nil) |
Adds a child to this element, optionally setting attributes in the element.
| element: | optional. If Element, the element is added. Otherwise, a new Element is constructed with the argument (see Element.initialize). |
| attrs: | If supplied, must be a Hash containing String name,value pairs, which will be used to set the attributes of the new Element. |
| Returns: | the Element that was added |
el = doc.add_element 'my-tag'
el = doc.add_element 'my-tag', {'attr1'=>'val1', 'attr2'=>'val2'}
el = Element.new 'my-tag'
doc.add_element el
この要素に子を加えます。要素に属性を設定するのは任意(オプション) です。
| element: | 任意(オプション)。もし、Elementならば、その要素が加えられます。 そうでなければ、引数とともにコンストラクタが呼ばれて新しい Elementを加えます(Element.initializeをみて下さい)。 |
| attrs: | もし与えられていれば、Stringの名前と値のペアを含んだHashでなけ ればいけません。これは、新しいElementの属性を設定するのにも使 うことができるでしょう。 |
| 戻り値: | 加えられた後のElement |
el = doc.add_element 'my-tag'
el = doc.add_element 'my-tag', {'属性1'=>'値1', '属性2'=>'値2'}
el = Element.new 'my-tag'
doc.add_element el
| delete_element(element) |
Deletes a child element.
| element: | Must be an Element, String, or Integer. If Element, the element is removed. If String, the element is found (via XPath) and removed. This means that any parent can remove any descendant. If Integer, the Element indexed by that number will be removed. |
| Returns: | the element that was removed. |
doc.delete_element "/a/b/c[@id='4']" doc.delete_element doc.elements["//k"] doc.delete_element 1
子要素を削除します。
| element: | Element、String、あるいはIntegerでなければいけません。も し、Elementなら、その要素が削除されます。もし、Stringなら (XPath経由で)見つかった要素が削除されます。これは、どんな 親でも任意の子を削除できるということです。もしIntegerなら、 数字によって添字付けされたElementが削除されるでしょう。 |
| 戻り値: | 削除された後の要素 |
doc.delete_element "/a/b/c[@id='4']" doc.delete_element doc.elements["//k"] doc.delete_element 1
| has_elements?() |
Evaluates to true if this element has at least one child Element
doc = Document.new "<a><b/><c>Text</c></a>" doc.root.has_elements # -> true doc.elements["/a/b"].has_elements # -> false doc.elements["/a/c"].has_elements # -> false
もし、この要素が一つでも子のElementを持っていたらtrueと評価し ます。
doc = Document.new "<a><b/><c>Text</c></a>" doc.root.has_elements # -> true doc.elements["/a/b"].has_elements # -> false doc.elements["/a/c"].has_elements # -> false
| each_element_with_attribute( key, value=nil, max=0, name=nil, &block ) {|Element| ...} |
Iterates through the child elements, yielding for each Element that has a particular attribute set.
| key: | the name of the attribute to search for |
| value: | the value of the attribute |
| max: | (optional) causes this method to return after yielding for this number of matching children |
| name: | (optional) if supplied, this is an XPath that filters the children to check. |
doc = Document.new "<a><b @id='1'/><c @id='2'/><d @id='1'/><e/></a>"
# Yields b, c, d
doc.root.each_element_with_attribute( 'id' ) {|e| p e}
# Yields b, d
doc.root.each_element_with_attribute( 'id', '1' ) {|e| p e}
# Yields b
doc.root.each_element_with_attribute( 'id', '1', 1 ) {|e| p e}
# Yields d
doc.root.each_element_with_attribute( 'id', '1', 0, 'd' ) {|e| p e}
子要素のためのイテレータで、特定の属性が設定されている各Element に対してyieldします。
| key: | 検索する属性の名前です。 |
| value: | 属性の値です。 |
| max: | (任意(オプション)) マッチした子のmaxの数だけyieldした後 にこのメソッドは戻って来ます(終了します)。 |
| name: | (任意(オプション)) もし、与えられれば、これは子と一致する ためのフィルタであるXPathです。 |
doc = Document.new "<a><b @id='1'/><c @id='2'/><d @id='1'/><e/></a>"
# Yields b, c, d
doc.root.each_element_with_attribute( 'id' ) {|e| p e}
# Yields b, d
doc.root.each_element_with_attribute( 'id', '1' ) {|e| p e}
# Yields b
doc.root.each_element_with_attribute( 'id', '1', 1 ) {|e| p e}
# Yields d
doc.root.each_element_with_attribute( 'id', '1', 0, 'd' ) {|e| p e}
| each_element_with_text( text=nil, max=0, name=nil, &block ) {|Element| ...} |
Iterates through the children, yielding for each Element that has a particular text set.
| text: | the text to search for. If nil, or not supplied, will itterate over all Element children that contain at least one Text node. |
| max: | (optional) causes this method to return after yielding for this number of matching children |
| name: | (optional) if supplied, this is an XPath that filters the children to check. |
doc = Document.new '<a><b>b</b><c>b</c><d>d</d><e/></a>'
# Yields b, c, d
doc.each_element_with_text {|e|p e}
# Yields b, c
doc.each_element_with_text('b'){|e|p e}
# Yields b
doc.each_element_with_text('b', 1){|e|p e}
# Yields d
doc.each_element_with_text(nil, 0, 'd'){|e|p e}
子要素のためのイテレータで、特定の属性が設定されている各Element に対してyieldします。
| text: | 検索するためのテキストです。もし、nilであったり、与えられなけ れば、少なくとも一つのTextノードを含む全てのElementの子を 繰り返します。 |
| max: | (任意(オプション)) マッチした子のmaxの数だけyieldした後 にこのメソッドは戻って来ます(終了します)。 |
| name: | (任意(オプション)) もし、与えられれば、これは子と一致する ためのフィルタであるXPathです。 |
doc = Document.new '<a><b>b</b><c>b</c><d>d</d><e/></a>'
# Yields b, c, d
doc.each_element_with_text {|e|p e}
# Yields b, c
doc.each_element_with_text('b'){|e|p e}
# Yields b
doc.each_element_with_text('b', 1){|e|p e}
# Yields d
doc.each_element_with_text(nil, 0, 'd'){|e|p e}
| each_element( xpath=nil, &block ) {|Element| ...} |
| get_elements( xpath ) |
This is a little slower than calling elements.each directly.
| xpath: | any XPath by which to search for elements in the tree |
| Returns: | an array of Elements that match the supplied path |
これはelements.eachを直接呼ぶより少し遅いです。
| xpath: | XMLツリーの中の要素を探すための全てのXPathです。 |
| 戻り値: | 与えられたパスにマッチしたElementsの配列です。 |
| next_element() |
Returns the next sibling that is an element, or nil if there is no Element sibling after this one
doc = Document.new '<a><b/>text<c/></a>' doc.root.elements['b'].next_element #-> <c/> doc.root.elements['c'].next_element #-> nil
要素の次の兄弟を返します。もし、この要素の次にElemntの兄弟がなけ ればnilを返します。
doc = Document.new '<a><b/>text<c/></a>' doc.root.elements['b'].next_element #-> <c/> doc.root.elements['c'].next_element #-> nil
| previous_element() |
Returns the previous sibling that is an element, or nil if there is no Element sibling prior to this one
doc = Document.new '<a><b/>text<c/></a>' doc.root.elements['c'].previous_element #-> <b/> doc.root.elements['b'].previous_element #-> nil
要素の前の兄弟を返します。もし、この要素の前にElemntの兄弟がなけ ればnilを返します。
doc = Document.new '<a><b/>text<c/></a>' doc.root.elements['c'].previous_element #-> <b/> doc.root.elements['b'].previous_element #-> nil
| has_text?() |
Evaluates to true if this element has at least one Text child
もし、要素が少なくとも一つのTextの子を持っていればtrueと評価し ます。
| text( path = nil ) |
A convenience method which returns the String value of the first child text element, if one exists, and nil otherwise.
Note that an element may have multiple Text elements, perhaps separated by other children. Be aware that this method only returns the first Text node.
doc = Document.new "<p>some text <b>this is bold!</b> more text</p>" # The element 'p' has two text elements, "some text " and " more text". doc.root.text #-> "some text "
もし、存在するなら、_最初の_テキスト要素の子のStringの値を返す便 利なメソッドです。そうでなければnilを返します。
要素は、たぶん、他の子に隔てられて、複数のText要素を持ってい るかも知れないことに注意して下さい。このメソッドは最初の Textノードしか返さないことを意識して下さい。
doc = Document.new "<p>some text <b>this is bold!</b> more text</p>" # The element 'p' has two text elements, "some text " and " more text". doc.root.text #-> "some text "
| get_text(path = nil) |
Returns the first child Text node, if any, or nil otherwise. This method returns the actual Text node, rather than the String content.
doc = Document.new "<p>some text <b>this is bold!</b> more text</p>" # The element 'p' has two text elements, "some text " and " more text". doc.root.get_text.value #-> "some text "
もしあれば、最初の子のTextノードを返します。そうでなければnil を返します。このメソッドは、現在はStringよりむしろTextノードを 返します。
doc = Document.new "<p>some text <b>this is bold!</b> more text</p>" # The element 'p' has two text elements, "some text " and " more text". doc.root.get_text.value #-> "some text "
| text=( text ) |
Sets the first Text child of this object. See text() for a discussion about Text children.
If a Text child already exists, the child is replaced by this content. This means that Text content can be deleted by calling this method with a nil argument. In this case, the next Text child becomes the first Text child. In no case is the order of any siblings disturbed.
| text: | If a String, a new Text child is created and added to this Element as the first Text child. If Text, the text is set as the first Child element. If nil, then any existing first Text child is removed. |
| Returns: | this Element. |
doc = Document.new '<a><b/></a>' doc.root.text = 'Sean' #-> '<a><b/>Sean</a>' doc.root.text = 'Elliott' #-> '<a><b/>Elliott</a>' doc.root.add_element 'c' #-> '<a><b/>Elliott<c/></a>' doc.root.text = 'Russell' #-> '<a><b/>Russell<c/></a>' doc.root.text = nil #-> '<a><b/><c/></a>'
このオブジェクトの最初のTextの子を設定します。Textの子の議論につ いてはtest()をみて下さい。
もし、Textの子がもう既に存在していたら、子は引数の内容で置き換え られます。これは、Textの内容はこのメソッドに引数としてnilを与え ることにより、削除されるということを意味してます。この場合、次の Textの子は最初のTextの子になります。どんな場合にも兄弟の順番がか き乱されることは決してありません。
| text: | もし、Stringなら、新しいTextの子が作られ、最初のTextの子として、 このElementに加えられます。もし、Textなら、最初のChild要素とし て設定されます。もし、nilなら、存在しているいかなる最初のText の子は取り除かれます。 |
| 戻り値: | このElementです。 |
doc = Document.new '<a><b/></a>' doc.root.text = 'Sean' #-> '<a><b/>Sean</a>' doc.root.text = 'Elliott' #-> '<a><b/>Elliott</a>' doc.root.add_element 'c' #-> '<a><b/>Elliott<c/></a>' doc.root.text = 'Russell' #-> '<a><b/>Russell<c/></a>' doc.root.text = nil #-> '<a><b/><c/></a>'
| add_text( text ) |
A helper method to add a Text child. Actual Text instances can be added with regular Parent methods, such as add() and <<()
| text: | if a String, a new Text instance is created and added to the parent. If Text, the object is added directly. |
| Returns: | this Element |
e = Element.new('a') #-> <e/>
e.add_text 'foo' #-> <e>foo</e>
e.add_text Text.new(' bar') #-> <e>foo bar</e>
Note that at the end of this example, the branch has 3 nodes; the 'e' element and 2 Text node children.
Textの子を追加するヘルパーメソッドです。実際はTextインスタンスが add()や<<()のようないつものParentのメソッドで追加されます。
| text: | もし、Stringなら、新しいTextインスタンスが作られそして親に追加 されます。もし、Textなら、直接追加されます。 |
| 戻り値: | このElemetnです。 |
e = Element.new('a') #-> <e/>
e.add_text 'foo' #-> <e>foo</e>
e.add_text Text.new(' bar') #-> <e>foo bar</e>
最後の例では、枝は3ノード持っていることに注意して下さい。
| has_attributes?() |
Evaluates to true if this element has any attributes set, false otherwise.
もし、要素がいくつか属性が設定されていればtrueと評価します。そ れ以外はfalseと評価します。
| add_attribute( key, value=nil ) |
Adds an attribute to this element, overwriting any existing attribute by the same name.
| key: | can be either an Attribute or a String. If an Attribute, the attribute is added to the list of Element attributes. If String, the argument is used as the name of the new attribute, and the value parameter must be supplied. |
| value: | Required if key is a String, and ignored if the first argument is an Attribute. This is a String, and is used as the value of the new Attribute. |
| Returns: | the Attribute added |
e = Element.new 'e'
e.add_attribute( 'a', 'b' ) #-> <e a='b'/>
e.add_attribute( 'x:a', 'c' ) #-> <e a='b' x:a='c'/>
e.add_attribute Attribute.new('b', 'd') #-> <e a='b' x:a='c' b='d'/>
要素に、存在する同じ名前の属性を上書きしながら属性を追加します。
| key: | AttributeかStringを取ることができます。もし、Attributeなら、そ の属性はElementの属性のリストに追加されます。もし、Stringなら、 引数は新しい属性の名前として使われます。そして、パラメータ valueが与えられていなければいけません。 |
| value: | もし、keyがStringのときに要求されます。最初の引数がAttribute の時は無視されます。これは、Stringで、新しいAttributeの値とし て使われます。 |
| 戻り値: | 加えられたAttributeです。 |
e = Element.new 'e'
e.add_attribute( 'a', 'b' ) #-> <e a='b'/>
e.add_attribute( 'x:a', 'c' ) #-> <e a='b' x:a='c'/>
e.add_attribute Attribute.new('b', 'd') #-> <e a='b' x:a='c' b='d'/>
| add_attributes(hash) |
Add multiple attributes to this element.
| hash: | is either a hash, or array of arrays |
el.add_attributes( {"name1"=>"value1", "name2"=>"value2"} )
el.add_attributes( [ ["name1","value1"], ["name2"=>"value2"] ] )
要素に複数の属性を追加します。
| hash: | 配列の配列かハッシュです。 |
el.add_attributes( {"name1"=>"value1", "name2"=>"value2"} )
el.add_attributes( [ ["name1","value1"], ["name2"=>"value2"] ] )
| delete_attribute(key) |
Removes an attribute
| key: | either an Attribute or a String. In either case, the attribute is found by matching the attribute name to the argument, and then removed. If no attribute is found, no action is taken. |
| Returns: | the attribute removed, or nil if this Element did not contain a matching attribute |
e = Element.new('E')
e.add_attribute( 'name', 'Sean' ) #-> <E name='Sean'/>
r = e.add_attribute( 'sur:name', 'Russell' ) #-> <E name='Sean' sur:name='Russell'/>
e.delete_attribute( 'name' ) #-> <E sur:name='Russell'/>
e.delete_attribute( r ) #-> <E/>
属性を取り除きます。
| key: | AttributeかStringをとります。どちらの場合も属性は、属性の名前と 引数をマッチさせる事によりみつけられ、取り除かれます。もし、属 性が見つからなかったときは何もしません。 |
| 戻り値: | 取り除かれた属性です。あるいは、Elementがマッチする属性を含ん でないときはnilです。 |
e = Element.new('E')
e.add_attribute( 'name', 'Sean' ) #-> <E name='Sean'/>
r = e.add_attribute( 'sur:name', 'Russell' ) #-> <E name='Sean' sur:name='Russell'/>
e.delete_attribute( 'name' ) #-> <E sur:name='Russell'/>
e.delete_attribute( r ) #-> <E/>
| write(writer, indent=-1) |
Writes out this element, and recursively, all children.
| writer: | A String or IO (or any object supporting <<(String)) |
| indent: | if supplied, must be an Integer, which is then used to determine the indentation of this element and its children. |
out = '' doc.write( out ) #-> doc is written to the string 'out' doc.write( $stdout ) #-> doc written to the console
要素と再帰的に全ての子を書き出します。
| writer: | StringまたはIO(または<<(String)をサポートしているオブジェ クト)です |
| indent: | もし与えられていれば、Integerでなければいけません。これは、要 素とその子のインデントを決めるために使われます。 |
out = '' doc.write( out ) #-> doc is written to the string 'out' doc.write( $stdout ) #-> doc written to the console