Method: REXML::Element#each_element_with_attribute
- Defined in:
- lib/rexml/element.rb
#each_element_with_attribute(key, value = nil, max = 0, name = nil, &block) ⇒ Object
:call-seq:
each_element_with_attribute(attr_name, value = nil, max = 0, xpath = nil) {|e| ... }
Calls the given block with each child element that meets given criteria.
When only string argument attr_name is given, calls the block with each child element that has that 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'/>
With argument attr_name and string argument value given, calls the block with each child element that has that attribute with that value:
a.each_element_with_attribute('id', '1') {|e| p e }
Output:
<b id='1'/>
<d id='1'/>
With arguments attr_name, value, and integer argument max given, calls the block with at most max child elements:
a.each_element_with_attribute('id', '1', 1) {|e| p e }
Output:
<b id='1'/>
With all arguments given, including xpath, calls the block with only those child elements that meet the first three criteria, and also match the given xpath:
a.each_element_with_attribute('id', '1', 2, '//d') {|e| p e }
Output:
<d id='1'/>
840 841 842 843 844 845 846 847 848 |
# File 'lib/rexml/element.rb', line 840 def each_element_with_attribute( key, value=nil, max=0, name=nil, &block ) # :yields: Element each_with_something( proc {|child| if value.nil? child.attributes[key] != nil else child.attributes[key]==value end }, max, name, &block ) end |