[]( index, name=nil) public

Fetches a child element. Filters only Element children, regardless of the XPath match.

index

the search parameter. This is either an Integer, which will be used to find the index’th child Element, or an XPath, which will be used to search for the Element. Because of the nature of XPath searches, any element in the connected XML document can be fetched through any other element. The Integer index is 1-based, not 0-based. This means that the first child element is at index 1, not 0, and the +n+th element is at index n, not n-1. This is because XPath indexes element children starting from 1, not 0, and the indexes should be the same.

name

optional, and only used in the first argument is an Integer. In that case, the index’th child Element that has the supplied name will be returned. Note again that the indexes start at 1.

Returns

the first matching Element, or nil if no child matched

doc = Document.new ‘<a><c id=“1”/><c id=“2”/><d/>’ doc.root.elements[1] #-> <b/> doc.root.elements[‘c’] #-> <c id=“1”/> doc.root.elements[2,‘c’] #-> <c id=“2”/>

Show source
Register or log in to add new notes.