Add bin and edit workflow
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 9s

This commit is contained in:
2026-09-16 13:11:16 -06:00
parent c8ac4fcae5
commit 4cee170d66
17576 changed files with 895740 additions and 2 deletions
@@ -0,0 +1,87 @@
== Class Child
Class Child includes module Node;
see {Tasks for Node}[node_rdoc.html].
:include: ../tocs/child_toc.rdoc
=== Relationships
==== Task: Set the Parent
Use method {Child#parent=}[../../../../REXML/Parent.html#method-i-parent-3D]
to set the parent:
e0 = REXML::Element.new('foo')
e1 = REXML::Element.new('bar')
e1.parent # => nil
e1.parent = e0
e1.parent # => <foo/>
==== Task: Insert Previous Sibling
Use method {Child#previous_sibling=}[../../../../REXML/Parent.html#method-i-previous_sibling-3D]
to insert a previous sibling:
xml_string = '<root><a/><c/></root>'
d = REXML::Document.new(xml_string)
d.root.to_a # => [<a/>, <c/>]
c = d.root[1] # => <c/>
b = REXML::Element.new('b')
c.previous_sibling = b
d.root.to_a # => [<a/>, <b/>, <c/>]
==== Task: Insert Next Sibling
Use method {Child#next_sibling=}[../../../../REXML/Parent.html#method-i-next-sibling-3D]
to insert a previous sibling:
xml_string = '<root><a/><c/></root>'
d = REXML::Document.new(xml_string)
d.root.to_a # => [<a/>, <c/>]
a = d.root[0] # => <a/>
b = REXML::Element.new('b')
a.next_sibling = b
d.root.to_a # => [<a/>, <b/>, <c/>]
=== Removal or Replacement
==== Task: Remove Child from Parent
Use method {Child#remove}[../../../../REXML/Parent.html#method-i-remove]
to remove a child from its parent; returns the removed child:
xml_string = '<root><a/><b/><c/></root>'
d = REXML::Document.new(xml_string)
d.root.to_a # => [<a/>, <b/>, <c/>]
b = d.root[1] # => <b/>
b.remove # => <b/>
d.root.to_a # => [<a/>, <c/>]
==== Task: Replace Child
Use method {Child#replace_with}[../../../../REXML/Parent.html#method-i-replace]
to replace a child;
returns the replaced child:
xml_string = '<root><a/><b/><c/></root>'
d = REXML::Document.new(xml_string)
d.root.to_a # => [<a/>, <b/>, <c/>]
b = d.root[1] # => <b/>
d = REXML::Element.new('d')
b.replace_with(d) # => <b/>
d.root.to_a # => [<a/>, <d/>, <c/>]
=== Document
==== Task: Get the Document
Use method {Child#document}[../../../../REXML/Parent.html#method-i-document]
to get the document for the child:
xml_string = '<root><a/><b/><c/></root>'
d = REXML::Document.new(xml_string)
d.root.to_a # => [<a/>, <b/>, <c/>]
b = d.root[1] # => <b/>
b.document == d # => true
REXML::Child.new.document # => nil
@@ -0,0 +1,276 @@
== Class Document
Class Document has methods from its superclasses and included modules;
see:
- {Tasks for Element}[element_rdoc.html].
- {Tasks for Parent}[parent_rdoc.html].
- {Tasks for Child}[child_rdoc.html].
- {Tasks for Node}[node_rdoc.html].
- {Module Enumerable}[https://docs.ruby-lang.org/en/master/Enumerable.html].
:include: ../tocs/document_toc.rdoc
=== New Document
==== Task: Create an Empty Document
Use method {Document::new}[../../../../REXML/Document.html#method-c-new]
to create an empty document.
d = REXML::Document.new
==== Task: Parse a \String into a New Document
Use method {Document::new}[../../../../REXML/Document.html#method-c-new]
to parse an XML string into a new document:
xml_string = '<root><a/>text<b/>more<c/></root>'
d = REXML::Document.new(xml_string)
d.root # => <root> ... </>
==== Task: Parse an \IO Stream into a New Document
Use method {Document::new}[../../../../REXML/Document.html#method-c-new]
to parse an XML \IO stream into a new document:
xml_string = '<root><a/>text<b/>more<c/></root>'
File.write('t.xml', xml_string)
d = File.open('t.xml', 'r') do |file|
REXML::Document.new(file)
end
d.root # => <root> ... </>
==== Task: Create a Document from an Existing Document
Use method {Document::new}[../../../../REXML/Document.html#method-c-new]
to create a document from an existing document.
The context and attributes are copied to the new document,
but not the children:
xml_string = '<root><a/>text<b/>more<c/></root>'
d = REXML::Document.new(xml_string)
d.children # => [<root> ... </>]
d.context = {raw: :all, compress_whitespace: :all}
d.add_attributes({'bar' => 0, 'baz' => 1})
d1 = REXML::Document.new(d)
d1.context # => {:raw=>:all, :compress_whitespace=>:all}
d1.attributes # => {"bar"=>bar='0', "baz"=>baz='1'}
d1.children # => []
==== Task: Clone a Document
Use method {Document#clone}[../../../../REXML/Document.html#method-i-clone]
to clone a document.
The context and attributes are copied to the new document,
but not the children:
xml_string = '<root><a/>text<b/>more<c/></root>'
d = REXML::Document.new(xml_string)
d.children # => [<root> ... </>]
d.context = {raw: :all, compress_whitespace: :all}
d.add_attributes({'bar' => 0, 'baz' => 1})
d1 = d.clone # => < bar='0' baz='1'/>
d1.context # => {:raw=>:all, :compress_whitespace=>:all}
d1.attributes # => {"bar"=>bar='0', "baz"=>baz='1'}
d1.children # => []
=== Document Type
==== Task: Get the Document Type
Use method {Document#doctype}[../../../../REXML/Document.html#method-i-doctype]
to get the document type:
d = REXML::Document.new('<!DOCTYPE document SYSTEM "subjects.dtd">')
d.doctype.class # => REXML::DocType
d = REXML::Document.new('')
d.doctype.class # => nil
==== Task: Set the Document Type
Use method {document#add}[../../../../REXML/Document.html#method-i-add]
to add or replace the document type:
d = REXML::Document.new('')
d.doctype.class # => nil
d.add(REXML::DocType.new('foo'))
d.doctype.class # => REXML::DocType
=== XML Declaration
==== Task: Get the XML Declaration
Use method {document#xml_decl}[../../../../REXML/Document.html#method-i-xml_decl]
to get the XML declaration:
d = REXML::Document.new('<!DOCTYPE document SYSTEM "subjects.dtd">')
d.xml_decl.class # => REXML::XMLDecl
d.xml_decl # => <?xml ... ?>
d = REXML::Document.new('')
d.xml_decl.class # => REXML::XMLDecl
d.xml_decl # => <?xml ... ?>
==== Task: Set the XML Declaration
Use method {document#add}[../../../../REXML/Document.html#method-i-add]
to replace the XML declaration:
d = REXML::Document.new('<!DOCTYPE document SYSTEM "subjects.dtd">')
d.add(REXML::XMLDecl.new)
=== Children
==== Task: Add an Element Child
Use method
{document#add_element}[../../../../REXML/Document.html#method-i-add_element]
to add an element to the document:
d = REXML::Document.new('')
d.add_element(REXML::Element.new('root'))
d.children # => [<root/>]
==== Task: Add a Non-Element Child
Use method
{document#add}[../../../../REXML/Document.html#method-i-add]
to add a non-element to the document:
xml_string = '<root><a/>text<b/>more<c/></root>'
d = REXML::Document.new(xml_string)
d.add(REXML::Text.new('foo'))
d.children # => [<root> ... </>, "foo"]
=== Writing
==== Task: Write to $stdout
Use method
{document#write}[../../../../REXML/Document.html#method-i-write]
to write the document to <tt>$stdout</tt>:
xml_string = '<root><a/>text<b/>more<c/></root>'
d = REXML::Document.new(xml_string)
d.write
Output:
<root><a/>text<b/>more<c/></root>
==== Task: Write to IO Stream
Use method
{document#write}[../../../../REXML/Document.html#method-i-write]
to write the document to <tt>$stdout</tt>:
xml_string = '<root><a/>text<b/>more<c/></root>'
d = REXML::Document.new(xml_string)
File.open('t.xml', 'w') do |file|
d.write(file)
end
p File.read('t.xml')
Output:
"<root><a/>text<b/>more<c/></root>"
==== Task: Write with No Indentation
Use method
{document#write}[../../../../REXML/Document.html#method-i-write]
to write the document with no indentation:
xml_string = '<root><a><b><c></c></b></a></root>'
d = REXML::Document.new(xml_string)
d.write({indent: 0})
Output:
<root>
<a>
<b>
<c/>
</b>
</a>
</root>
==== Task: Write with Specified Indentation
Use method
{document#write}[../../../../REXML/Document.html#method-i-write]
to write the document with a specified indentation:
xml_string = '<root><a><b><c></c></b></a></root>'
d = REXML::Document.new(xml_string)
d.write({indent: 2})
Output:
<root>
<a>
<b>
<c/>
</b>
</a>
</root>
=== Querying
==== Task: Get the Document
Use method
{document#document}[../../../../REXML/Document.html#method-i-document]
to get the document (+self+); overrides <tt>Element#document</tt>:
xml_string = '<root><a><b><c></c></b></a></root>'
d = REXML::Document.new(xml_string)
d.document == d # => true
==== Task: Get the Encoding
Use method
{document#document}[../../../../REXML/Document.html#method-i-document]
to get the document (+self+); overrides <tt>Element#document</tt>:
xml_string = '<root><a><b><c></c></b></a></root>'
d = REXML::Document.new(xml_string)
d.encoding # => "UTF-8"
==== Task: Get the Node Type
Use method
{document#node_type}[../../../../REXML/Document.html#method-i-node_type]
to get the node type (+:document+); overrides <tt>Element#node_type</tt>:
xml_string = '<root><a><b><c></c></b></a></root>'
d = REXML::Document.new(xml_string)
d.node_type # => :document
==== Task: Get the Root Element
Use method
{document#root}[../../../../REXML/Document.html#method-i-root]
to get the root element:
xml_string = '<root><a><b><c></c></b></a></root>'
d = REXML::Document.new(xml_string)
d.root # => <root> ... </>
==== Task: Determine Whether Stand-Alone
Use method
{document#stand_alone?}[../../../../REXML/Document.html#method-i-stand_alone-3F]
to get the stand-alone value:
d = REXML::Document.new('<?xml standalone="yes"?>')
d.stand_alone? # => "yes"
==== Task: Get the Version
Use method
{document#version}[../../../../REXML/Document.html#method-i-version]
to get the version:
d = REXML::Document.new('<?xml version="2.0" encoding="UTF-8"?>')
d.version # => "2.0"
@@ -0,0 +1,602 @@
== Class Element
Class Element has methods from its superclasses and included modules;
see:
- {Tasks for Parent}[parent_rdoc.html].
- {Tasks for Child}[child_rdoc.html].
- {Tasks for Node}[node_rdoc.html].
- {Module Enumerable}[https://docs.ruby-lang.org/en/master/Enumerable.html].
:include: ../tocs/element_toc.rdoc
=== New Element
==== Task: Create a Default Element
Use method
{Element::new}[../../../../REXML/Element.html#method-c-new]
with no arguments to create a default element:
e = REXML::Element.new
e.name # => "UNDEFINED"
e.parent # => nil
e.context # => nil
==== Task: Create a Named Element
Use method
{Element::new}[../../../../REXML/Element.html#method-c-new]
with a string name argument
to create a named element:
e = REXML::Element.new('foo')
e.name # => "foo"
e.parent # => nil
e.context # => nil
==== Task: Create an Element with Name and Parent
Use method
{Element::new}[../../../../REXML/Element.html#method-c-new]
with name and parent arguments
to create an element with name and parent:
p = REXML::Parent.new
e = REXML::Element.new('foo', p)
e.name # => "foo"
e.parent # => #<REXML::Parent @parent=nil, @children=[<foo/>]>
e.context # => nil
==== Task: Create an Element with Name, Parent, and Context
Use method
{Element::new}[../../../../REXML/Element.html#method-c-new]
with name, parent, and context arguments
to create an element with name, parent, and context:
p = REXML::Parent.new
e = REXML::Element.new('foo', p, {compress_whitespace: :all})
e.name # => "foo"
e.parent # => #<REXML::Parent @parent=nil, @children=[<foo/>]>
e.context # => {:compress_whitespace=>:all}
==== Task: Create a Shallow Clone
Use method
{Element#clone}[../../../../REXML/Element.html#method-i-clone]
to create a shallow clone of an element,
copying only the name, attributes, and context:
e0 = REXML::Element.new('foo', nil, {compress_whitespace: :all})
e0.add_attribute(REXML::Attribute.new('bar', 'baz'))
e0.context = {compress_whitespace: :all}
e1 = e0.clone # => <foo bar='baz'/>
e1.name # => "foo"
e1.context # => {:compress_whitespace=>:all}
=== Attributes
==== Task: Create and Add an Attribute
Use method
{Element#add_attribute}[../../../../REXML/Element.html#method-i-add_attribute]
to create and add an attribute:
e = REXML::Element.new
e.add_attribute('attr', 'value') # => "value"
e['attr'] # => "value"
e.add_attribute('attr', 'VALUE') # => "VALUE"
e['attr'] # => "VALUE"
==== Task: Add an Existing Attribute
Use method
{Element#add_attribute}[../../../../REXML/Element.html#method-i-add_attribute]
to add an existing attribute:
e = REXML::Element.new
a = REXML::Attribute.new('attr', 'value')
e.add_attribute(a)
e['attr'] # => "value"
a = REXML::Attribute.new('attr', 'VALUE')
e.add_attribute(a)
e['attr'] # => "VALUE"
==== Task: Add Multiple Attributes from a Hash
Use method
{Element#add_attributes}[../../../../REXML/Element.html#method-i-add_attributes]
to add multiple attributes from a hash:
e = REXML::Element.new
h = {'foo' => 0, 'bar' => 1}
e.add_attributes(h)
e['foo'] # => "0"
e['bar'] # => "1"
==== Task: Add Multiple Attributes from an Array
Use method
{Element#add_attributes}[../../../../REXML/Element.html#method-i-add_attributes]
to add multiple attributes from an array:
e = REXML::Element.new
a = [['foo', 0], ['bar', 1]]
e.add_attributes(a)
e['foo'] # => "0"
e['bar'] # => "1"
==== Task: Retrieve the Value for an Attribute Name
Use method
{Element#[]}[../../../../REXML/Element.html#method-i-5B-5D]
to retrieve the value for an attribute name:
e = REXML::Element.new
e.add_attribute('attr', 'value') # => "value"
e['attr'] # => "value"
==== Task: Retrieve the Attribute Value for a Name and Namespace
Use method
{Element#attribute}[../../../../REXML/Element.html#method-i-attribute]
to retrieve the value for an attribute name:
xml_string = "<root xmlns:a='a' a:x='a:x' x='x'/>"
d = REXML::Document.new(xml_string)
e = d.root
e.attribute("x") # => x='x'
e.attribute("x", "a") # => a:x='a:x'
==== Task: Delete an Attribute
Use method
{Element#delete_attribute}[../../../../REXML/Element.html#method-i-delete_attribute]
to remove an attribute:
e = REXML::Element.new('foo')
e.add_attribute('bar', 'baz')
e.delete_attribute('bar')
e.delete_attribute('bar')
e['bar'] # => nil
==== Task: Determine Whether the Element Has Attributes
Use method
{Element#has_attributes?}[../../../../REXML/Element.html#method-i-has_attributes-3F]
to determine whether the element has attributes:
e = REXML::Element.new('foo')
e.has_attributes? # => false
e.add_attribute('bar', 'baz')
e.has_attributes? # => true
=== Children
<em>Element Children</em>
==== Task: Create and Add an Element
Use method
{Element#add_element}[../../../../REXML/Element.html#method-i-add_element]
to create a new element and add it to this element:
e0 = REXML::Element.new('foo')
e0.add_element('bar')
e0.children # => [<bar/>]
==== Task: Add an Existing Element
Use method
{Element#add_element}[../../../../REXML/Element.html#method-i-add_element]
to add an element to this element:
e0 = REXML::Element.new('foo')
e1 = REXML::Element.new('bar')
e0.add_element(e1)
e0.children # => [<bar/>]
==== Task: Create and Add an Element with Attributes
Use method
{Element#add_element}[../../../../REXML/Element.html#method-i-add_element]
to create a new element with attributes, and add it to this element:
e0 = REXML::Element.new('foo')
e0.add_element('bar', {'name' => 'value'})
e0.children # => [<bar name='value'/>]
==== Task: Add an Existing Element with Added Attributes
Use method
{Element#add_element}[../../../../REXML/Element.html#method-i-add_element]
to add an element to this element:
e0 = REXML::Element.new('foo')
e1 = REXML::Element.new('bar')
e0.add_element(e1, {'name' => 'value'})
e0.children # => [<bar name='value'/>]
==== Task: Delete a Specified Element
Use method
{Element#delete_element}[../../../../REXML/Element.html#method-i-delete_element]
to remove a specified element from this element:
e0 = REXML::Element.new('foo')
e1 = REXML::Element.new('bar')
e0.add_element(e1)
e0.children # => [<bar/>]
e0.delete_element(e1)
e0.children # => []
==== Task: Delete an Element by Index
Use method
{Element#delete_element}[../../../../REXML/Element.html#method-i-delete_element]
to remove an element from this element by index:
e0 = REXML::Element.new('foo')
e1 = REXML::Element.new('bar')
e0.add_element(e1)
e0.children # => [<bar/>]
e0.delete_element(1)
e0.children # => []
==== Task: Delete an Element by XPath
Use method
{Element#delete_element}[../../../../REXML/Element.html#method-i-delete_element]
to remove an element from this element by XPath:
e0 = REXML::Element.new('foo')
e1 = REXML::Element.new('bar')
e0.add_element(e1)
e0.children # => [<bar/>]
e0.delete_element('//bar/')
e0.children # => []
==== Task: Determine Whether Element Children
Use method
{Element#has_elements?}[../../../../REXML/Element.html#method-i-has_elements-3F]
to determine whether the element has element children:
e0 = REXML::Element.new('foo')
e0.has_elements? # => false
e0.add_element(REXML::Element.new('bar'))
e0.has_elements? # => true
==== Task: Get Element Descendants by XPath
Use method
{Element#get_elements}[../../../../REXML/Element.html#method-i-get_elements]
to fetch all element descendant children by XPath:
xml_string = <<-EOT
<root>
<a level='1'>
<a level='2'/>
</a>
</root>
EOT
d = REXML::Document.new(xml_string)
d.root.get_elements('//a') # => [<a level='1'> ... </>, <a level='2'/>]
==== Task: Get Next Element Sibling
Use method
{Element#next_element}[../../../../REXML/Element.html#method-i-next_element]
to retrieve the next element sibling:
d = REXML::Document.new '<a><b/>text<c/></a>'
d.root.elements['b'].next_element #-> <c/>
d.root.elements['c'].next_element #-> nil
==== Task: Get Previous Element Sibling
Use method
{Element#previous_element}[../../../../REXML/Element.html#method-i-previous_element]
to retrieve the previous element sibling:
d = REXML::Document.new '<a><b/>text<c/></a>'
d.root.elements['c'].previous_element #-> <b/>
d.root.elements['b'].previous_element #-> nil
<em>Text Children</em>
==== Task: Add a Text Node
Use method
{Element#add_text}[../../../../REXML/Element.html#method-i-add_text]
to add a text node to the element:
d = REXML::Document.new('<a>foo<b/>bar</a>')
e = d.root
e.add_text(REXML::Text.new('baz'))
e.to_a # => ["foo", <b/>, "bar", "baz"]
e.add_text(REXML::Text.new('baz'))
e.to_a # => ["foo", <b/>, "bar", "baz", "baz"]
==== Task: Replace the First Text Node
Use method
{Element#text=}[../../../../REXML/Element.html#method-i-text-3D]
to replace the first text node in the element:
d = REXML::Document.new('<root><a/>text<b/>more<c/></root>')
e = d.root
e.to_a # => [<a/>, "text", <b/>, "more", <c/>]
e.text = 'oops'
e.to_a # => [<a/>, "oops", <b/>, "more", <c/>]
==== Task: Remove the First Text Node
Use method
{Element#text=}[../../../../REXML/Element.html#method-i-text-3D]
to remove the first text node in the element:
d = REXML::Document.new('<root><a/>text<b/>more<c/></root>')
e = d.root
e.to_a # => [<a/>, "text", <b/>, "more", <c/>]
e.text = nil
e.to_a # => [<a/>, <b/>, "more", <c/>]
==== Task: Retrieve the First Text Node
Use method
{Element#get_text}[../../../../REXML/Element.html#method-i-get_text]
to retrieve the first text node in the element:
d = REXML::Document.new('<root><a/>text<b/>more<c/></root>')
e = d.root
e.to_a # => [<a/>, "text", <b/>, "more", <c/>]
e.get_text # => "text"
==== Task: Retrieve a Specific Text Node
Use method
{Element#get_text}[../../../../REXML/Element.html#method-i-get_text]
to retrieve the first text node in a specified element:
d = REXML::Document.new "<root>some text <b>this is bold!</b> more text</root>"
e = d.root
e.get_text('//root') # => "some text "
e.get_text('//b') # => "this is bold!"
==== Task: Determine Whether the Element has Text Nodes
Use method
{Element#has_text?}[../../../../REXML/Element.html#method-i-has_text-3F]
to determine whether the element has text:
e = REXML::Element.new('foo')
e.has_text? # => false
e.add_text('bar')
e.has_text? # => true
<em>Other Children</em>
==== Task: Get the Child at a Given Index
Use method
{Element#[]}[../../../../REXML/Element.html#method-i-5B-5D]
to retrieve the child at a given index:
d = REXML::Document.new '><root><a/>text<b/>more<c/></root>'
e = d.root
e[0] # => <a/>
e[1] # => "text"
e[2] # => <b/>
==== Task: Get All CDATA Children
Use method
{Element#cdatas}[../../../../REXML/Element.html#method-i-cdatas]
to retrieve all CDATA children:
xml_string = <<-EOT
<root>
<![CDATA[foo]]>
<![CDATA[bar]]>
</root>
EOT
d = REXML::Document.new(xml_string)
d.root.cdatas # => ["foo", "bar"]
==== Task: Get All Comment Children
Use method
{Element#comments}[../../../../REXML/Element.html#method-i-comments]
to retrieve all comment children:
xml_string = <<-EOT
<root>
<!--foo-->
<!--bar-->
</root>
EOT
d = REXML::Document.new(xml_string)
d.root.comments.map {|comment| comment.to_s } # => ["foo", "bar"]
==== Task: Get All Processing Instruction Children
Use method
{Element#instructions}[../../../../REXML/Element.html#method-i-instructions]
to retrieve all processing instruction children:
xml_string = <<-EOT
<root>
<?target0 foo?>
<?target1 bar?>
</root>
EOT
d = REXML::Document.new(xml_string)
instructions = d.root.instructions.map {|instruction| instruction.to_s }
instructions # => ["<?target0 foo?>", "<?target1 bar?>"]
==== Task: Get All Text Children
Use method
{Element#texts}[../../../../REXML/Element.html#method-i-texts]
to retrieve all text children:
xml_string = '<root><a/>text<b/>more<c/></root>'
d = REXML::Document.new(xml_string)
d.root.texts # => ["text", "more"]
=== Namespaces
==== Task: Add a Namespace
Use method
{Element#add_namespace}[../../../../REXML/Element.html#method-i-add_namespace]
to add a namespace to the element:
e = REXML::Element.new('foo')
e.add_namespace('bar')
e.namespaces # => {"xmlns"=>"bar"}
==== Task: Delete the Default Namespace
Use method
{Element#delete_namespace}[../../../../REXML/Element.html#method-i-delete_namespace]
to remove the default namespace from the element:
d = REXML::Document.new "<a xmlns:foo='bar' xmlns='twiddle'/>"
d.to_s # => "<a xmlns:foo='bar' xmlns='twiddle'/>"
d.root.delete_namespace # => <a xmlns:foo='bar'/>
d.to_s # => "<a xmlns:foo='bar'/>"
==== Task: Delete a Specific Namespace
Use method
{Element#delete_namespace}[../../../../REXML/Element.html#method-i-delete_namespace]
to remove a specific namespace from the element:
d = REXML::Document.new "<a xmlns:foo='bar' xmlns='twiddle'/>"
d.to_s # => "<a xmlns:foo='bar' xmlns='twiddle'/>"
d.root.delete_namespace # => <a xmlns:foo='bar'/>
d.to_s # => "<a xmlns:foo='bar'/>"
d.root.delete_namespace('foo')
d.to_s # => "<a/>"
==== Task: Get a Namespace URI
Use method
{Element#namespace}[../../../../REXML/Element.html#method-i-namespace]
to retrieve a specific namespace URI for the element:
xml_string = <<-EOT
<root>
<a xmlns='1' xmlns:y='2'>
<b/>
<c xmlns:z='3'/>
</a>
</root>
EOT
d = REXML::Document.new(xml_string)
b = d.elements['//b']
b.namespace # => "1"
b.namespace('y') # => "2"
==== Task: Retrieve Namespaces
Use method
{Element#namespaces}[../../../../REXML/Element.html#method-i-namespaces]
to retrieve all namespaces for the element:
xml_string = '<a xmlns="foo" xmlns:x="bar" xmlns:y="twee" z="glorp"/>'
d = REXML::Document.new(xml_string)
d.root.attributes.namespaces # => {"xmlns"=>"foo", "x"=>"bar", "y"=>"twee"}
==== Task: Retrieve Namespace Prefixes
Use method
{Element#prefixes}[../../../../REXML/Element.html#method-i-prefixes]
to retrieve all prefixes (namespace names) for the element:
xml_string = <<-EOT
<root>
<a xmlns:x='1' xmlns:y='2'>
<b/>
<c xmlns:z='3'/>
</a>
</root>
EOT
d = REXML::Document.new(xml_string, {compress_whitespace: :all})
d.elements['//a'].prefixes # => ["x", "y"]
d.elements['//b'].prefixes # => ["x", "y"]
d.elements['//c'].prefixes # => ["x", "y", "z"]
=== Iteration
==== Task: Iterate Over Elements
Use method
{Element#each_element}[../../../../REXML/Element.html#method-i-each_element]
to iterate over element children:
d = REXML::Document.new '<a><b>b</b><c>b</c><d>d</d><e/></a>'
d.root.each_element {|e| p e }
Output:
<b> ... </>
<c> ... </>
<d> ... </>
<e/>
==== Task: Iterate Over Elements Having a Specified Attribute
Use method
{Element#each_element_with_attribute}[../../../../REXML/Element.html#method-i-each_element_with_attribute]
to iterate over element children that have a specified attribute:
d = REXML::Document.new '<a><b id="1"/><c id="2"/><d id="1"/><e/></a>'
a = d.root
a.each_element_with_attribute('id') {|e| p e }
Output:
<b id='1'/>
<c id='2'/>
<d id='1'/>
==== Task: Iterate Over Elements Having a Specified Attribute and Value
Use method
{Element#each_element_with_attribute}[../../../../REXML/Element.html#method-i-each_element_with_attribute]
to iterate over element children that have a specified attribute and value:
d = REXML::Document.new '<a><b id="1"/><c id="2"/><d id="1"/><e/></a>'
a = d.root
a.each_element_with_attribute('id', '1') {|e| p e }
Output:
<b id='1'/>
<d id='1'/>
==== Task: Iterate Over Elements Having Specified Text
Use method
{Element#each_element_with_text}[../../../../REXML/Element.html#method-i-each_element_with_text]
to iterate over element children that have specified text:
=== Context
#whitespace
#ignore_whitespace_nodes
#raw
=== Other Getters
#document
#root
#root_node
#node_type
#xpath
#inspect
@@ -0,0 +1,97 @@
== Module Node
:include: ../tocs/node_toc.rdoc
=== Siblings
==== Task: Find Previous Sibling
Use method
{Node.previous_sibling_node}[../../../../REXML/Node.html#method-i-previous_sibling]
to retrieve the previous sibling:
d = REXML::Document.new('<root><a/><b/><c/></root>')
b = d.root[1] # => <b/>
b.previous_sibling_node # => <a/>
==== Task: Find Next Sibling
Use method
{Node.next_sibling_node}[../../../../REXML/Node.html#method-i-next_sibling]
to retrieve the next sibling:
d = REXML::Document.new('<root><a/><b/><c/></root>')
b = d.root[1] # => <b/>
b.next_sibling_node # => <c/>
=== Position
==== Task: Find Own Index Among Siblings
Use method
{Node.index_in_parent}[../../../../REXML/Node.html#method-i-index_in_parent]
to retrieve the 1-based index of this node among its siblings:
d = REXML::Document.new('<root><a/><b/><c/></root>')
b = d.root[1] # => <b/>
b.index_in_parent # => 2
=== Recursive Traversal
==== Task: Traverse Each Recursively
Use method
{Node.each_recursive}[../../../../REXML/Node.html#method-i-each_recursive]
to traverse a tree of nodes recursively:
xml_string = '<root><a><b><c></c></b><b><c></c></b></a></root>'
d = REXML::Document.new(xml_string)
d.root.each_recursive {|node| p node }
Output:
<a> ... </>
<b> ... </>
<c/>
<b> ... </>
<c/>
=== Recursive Search
==== Task: Traverse Each Recursively
Use method
{Node.find_first_recursive}[../../../../REXML/Node.html#method-i-find_first_recursive]
to search a tree of nodes recursively:
xml_string = '<root><a><b><c></c></b><b><c></c></b></a></root>'
d = REXML::Document.new(xml_string)
d.root.find_first_recursive {|node| node.name == 'c' } # => <c/>
=== Representation
==== Task: Represent a String
Use method {Node.to_s}[../../../../REXML/Node.html#method-i-to_s]
to represent the node as a string:
xml_string = '<root><a><b><c></c></b><b><c></c></b></a></root>'
d = REXML::Document.new(xml_string)
d.root.to_s # => "<root><a><b><c/></b><b><c/></b></a></root>"
=== Parent?
==== Task: Determine Whether the Node is a Parent
Use method {Node.parent?}[../../../../REXML/Node.html#method-i-parent-3F]
to determine whether the node is a parent;
class Text derives from Node:
d = REXML::Document.new('<root><a/>text<b/>more<c/></root>')
t = d.root[1] # => "text"
t.parent? # => false
Class Parent also derives from Node, but overrides this method:
p = REXML::Parent.new
p.parent? # => true
@@ -0,0 +1,267 @@
== Class Parent
Class Parent has methods from its superclasses and included modules;
see:
- {Tasks for Child}[child_rdoc.html].
- {Tasks for Node}[node_rdoc.html].
- {Module Enumerable}[https://docs.ruby-lang.org/en/master/Enumerable.html].
:include: ../tocs/parent_toc.rdoc
=== Queries
==== Task: Get the Count of Children
Use method {Parent#size}[../../../../REXML/Parent.html#method-i-size]
(or its alias +length+) to get the count of the parent's children:
p = REXML::Parent.new
p.size # => 0
xml_string = '<root><a/><b/><c/></root>'
d = REXML::Document.new(xml_string)
d.root.size # => 3
==== Task: Get the Child at a Given Index
Use method {Parent#[]}[../../../../REXML/Parent.html#method-i-5B-5D]
to get the child at a given index:
xml_string = '<root><a/><b/><c/></root>'
d = REXML::Document.new(xml_string)
d.root[1] # => <b/>
d.root[-1] # => <c/>
d.root[50] # => nil
==== Task: Get the Index of a Given Child
Use method {Parent#index}[../../../../REXML/Parent.html#method-i-index]
to get the index (0-based offset) of a child:
d = REXML::Document.new('<root></root>')
root = d.root
e0 = REXML::Element.new('foo')
e1 = REXML::Element.new('bar')
root.add(e0) # => <foo/>
root.add(e1) # => <bar/>
root.add(e0) # => <foo/>
root.add(e1) # => <bar/>
root.index(e0) # => 0
root.index(e1) # => 1
==== Task: Get the Children
Use method {Parent#children}[../../../../REXML/Parent.html#method-i-children]
(or its alias +to_a+) to get the parent's children:
xml_string = '<root><a/><b/><c/></root>'
d = REXML::Document.new(xml_string)
d.root.children # => [<a/>, <b/>, <c/>]
==== Task: Determine Whether the Node is a Parent
Use method {Parent#parent?}[../../../../REXML/Parent.html#method-i-parent-3F]
to determine whether the node is a parent;
class Text derives from Node:
d = REXML::Document.new('<root><a/>text<b/>more<c/></root>')
t = d.root[1] # => "text"
t.parent? # => false
Class Parent also derives from Node, but overrides this method:
p = REXML::Parent.new
p.parent? # => true
=== Additions
==== Task: Add a Child at the Beginning
Use method {Parent#unshift}[../../../../REXML/Parent.html#method-i-unshift]
to add a child as at the beginning of the children:
xml_string = '<root><a/><b/><c/></root>'
d = REXML::Document.new(xml_string)
d.root.children # => [<a/>, <b/>, <c/>]
d.root.unshift REXML::Element.new('d')
d.root.children # => [<d/>, <a/>, <b/>, <c/>]
==== Task: Add a Child at the End
Use method {Parent#<<}[../../../../REXML/Parent.html#method-i-3C-3C]
(or an alias +push+ or +add+) to add a child as at the end of the children:
xml_string = '<root><a/><b/><c/></root>'
d = REXML::Document.new(xml_string)
d.root.children # => [<a/>, <b/>, <c/>]
d.root << REXML::Element.new('d')
d.root.children # => [<a/>, <b/>, <c/>, <d/>]
==== Task: Replace a Child with Another Child
Use method {Parent#replace}[../../../../REXML/Parent.html#method-i-replace]
xml_string = '<root><a/><b/><c/></root>'
d = REXML::Document.new(xml_string)
d.root.children # => [<a/>, <b/>, <c/>]
b = d.root[1] # => <b/>
d.replace_child(b, REXML::Element.new('d'))
d.root.children # => [<a/>, <c/>]
==== Task: Replace Multiple Children with Another Child
Use method {Parent#[]=}[../../../../REXML/Parent.html#method-i-parent-5B-5D-3D]
to replace multiple consecutive children with another child:
xml_string = '<root><a/><b/><c/><d/></root>'
d = REXML::Document.new(xml_string)
d.root.children # => [<a/>, <b/>, <c/>, <d/>]
d.root[1, 2] = REXML::Element.new('x')
d.root.children # => [<a/>, <x/>, <d/>]
d.root[1, 5] = REXML::Element.new('x')
d.root.children # => [<a/>, <x/>] # BUG?
==== Task: Insert Child Before a Given Child
Use method {Parent#insert_before}[../../../../REXML/Parent.html#method-i-insert_before]
to insert a child immediately before a given child:
xml_string = '<root><a/><b/><c/></root>'
d = REXML::Document.new(xml_string)
d.root.children # => [<a/>, <b/>, <c/>]
b = d.root[1] # => <b/>
x = REXML::Element.new('x')
d.root.insert_before(b, x)
d.root.children # => [<a/>, <x/>, <b/>, <c/>]
==== Task: Insert Child After a Given Child
Use method {Parent#insert_after}[../../../../REXML/Parent.html#method-i-insert_after]
to insert a child immediately after a given child:
xml_string = '<root><a/><b/><c/></root>'
d = REXML::Document.new(xml_string)
d.root.children # => [<a/>, <b/>, <c/>]
b = d.root[1] # => <b/>
x = REXML::Element.new('x')
d.root.insert_after(b, x)
d.root.children # => [<a/>, <b/>, <x/>, <c/>]
=== Deletions
==== Task: Remove a Given Child
Use method {Parent#delete}[../../../../REXML/Parent.html#method-i-delete]
to remove all occurrences of a given child:
d = REXML::Document.new('<root></root>')
a = REXML::Element.new('a')
b = REXML::Element.new('b')
d.root.add(a)
d.root.add(b)
d.root.add(a)
d.root.add(b)
d.root.children # => [<a/>, <b/>, <a/>, <b/>]
d.root.delete(b)
d.root.children # => [<a/>, <a/>]
==== Task: Remove the Child at a Specified Offset
Use method {Parent#delete_at}[../../../../REXML/Parent.html#method-i-delete_at]
to remove the child at a specified offset:
d = REXML::Document.new('<root></root>')
a = REXML::Element.new('a')
b = REXML::Element.new('b')
d.root.add(a)
d.root.add(b)
d.root.add(a)
d.root.add(b)
d.root.children # => [<a/>, <b/>, <a/>, <b/>]
d.root.delete_at(2)
d.root.children # => [<a/>, <b/>, <b/>]
==== Task: Remove Children That Meet Specified Criteria
Use method {Parent#delete_if}[../../../../REXML/Parent.html#method-i-delete_if]
to remove children that meet criteria specified in the given block:
d = REXML::Document.new('<root></root>')
d.root.add(REXML::Element.new('x'))
d.root.add(REXML::Element.new('xx'))
d.root.add(REXML::Element.new('xxx'))
d.root.add(REXML::Element.new('xxxx'))
d.root.children # => [<x/>, <xx/>, <xxx/>, <xxxx/>]
d.root.delete_if {|child| child.name.size.odd? }
d.root.children # => [<xx/>, <xxxx/>]
=== Iterations
==== Task: Iterate Over Children
Use method {Parent#each_child}[../../../../REXML/Parent.html#method-i-each_child]
(or its alias +each+) to iterate over all children:
xml_string = '<root><a/><b/><c/></root>'
d = REXML::Document.new(xml_string)
d.root.children # => [<a/>, <b/>, <c/>]
d.root.each_child {|child| p child }
Output:
<a/>
<b/>
<c/>
==== Task: Iterate Over Child Indexes
Use method {Parent#each_index}[../../../../REXML/Parent.html#method-i-each_index]
to iterate over all child indexes:
xml_string = '<root><a/><b/><c/></root>'
d = REXML::Document.new(xml_string)
d.root.children # => [<a/>, <b/>, <c/>]
d.root.each_index {|child| p child }
Output:
0
1
2
=== Clones
==== Task: Clone Deeply
Use method {Parent#deep_clone}[../../../../REXML/Parent.html#method-i-deep_clone]
to clone deeply; that is, to clone every nested node that is a Parent object:
xml_string = <<-EOT
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
EOT
d = REXML::Document.new(xml_string)
root = d.root
shallow = root.clone
deep = root.deep_clone
shallow.to_s.size # => 12
deep.to_s.size # => 590