# Rexml & nested loops

**URL:** <https://www.ruby-forum.com/t/rexml-nested-loops/139839>\
**Category:** Ruby\
**Created:** [June 8, 2008, 12:21am UTC](https://www.ruby-forum.com/t/rexml-nested-loops/139839 "2008-06-08T00:21:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Ash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@Paul\_Ash](https://www.ruby-forum.com/u/Paul_Ash)\
**Post date:** [June 8, 2008, 12:21am UTC](https://www.ruby-forum.com/t/rexml-nested-loops/139839/1 "2008-06-08T00:21:11Z")

</div>

I’m a bit of a newbie to Ruby, and to xpath… and hoping someone  
here can give me the one-liner version of why this is failing.

I have an XML structure like:

 Foo1 foobar1 foobar2 Foo2 foobar3 foobar4 

And the code

doc.each\_element(’//foo’) { |foo|  
puts “\*\*\* Processing #{foo.elements[‘fooId’].text}”  
foo.each\_element(’//subFoo’) { |subFoo|  
puts subFoo.elements[“subfooName”].text  
}  
}

What I was expecting was a nested loop… but what I get is:

doc.each\_element(’//foo’) { |foo|

This iterator does what I expect.

foo.each\_element(’//subFoo’) { |subFoo|

This iterator seems to give me all instances of subFoo in doc, not just  
in the current instance of foo.

I’m sure this i just something about ruby that I don’t understand…  
but can someone point me in the right direction?

Cheers

ash

---

<div class="post-metadata">

**Author:** ![Paul\_Ash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@Paul\_Ash](https://www.ruby-forum.com/u/Paul_Ash)\
**Post date:** [June 8, 2008, 10:43am UTC](https://www.ruby-forum.com/t/rexml-nested-loops/139839/2 "2008-06-08T10:43:12Z")

</div>

On 08.06.2008 00:15, Paul Ash wrote:

> ```
> <subfoo>
> <subfoo>
> 
> ```
> 
> doc.each\_element(‘//foo’) { |foo|  
> can someone point me in the right direction?  
> No, it’s something about XPath that you do not understand. 🙂 “//”  
> means “from the root of the document”, so you are iterating all “subfoo”  
> elements each time. I can think of two remedies:

1. do a single loop with an XPath expression that selects only “subfoo”  
below “foo”'s.

2. keep your nested loop but change the XPath expression to not start at  
the root. (I believe it should be “.//subfoo”.)

These are the two pages I usually consult when in doubt about XPath  
expressions:

[http://www.w3schools.com/xpath/](http://www.w3schools.com/xpath/)  
[http://www.zvon.org/xxl/XPathTutorial/General/examples.html](http://www.zvon.org/xxl/XPathTutorial/General/examples.html)

And then there’s of course the standard:

> **[xpath cover page](https://www.w3.org/TR/xpath/)**
>
> The World Wide Web Consortium (W3C) is an international community where Member organizations, a full-time staff, and the public work together to develop Web standards.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Paul\_Ash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@Paul\_Ash](https://www.ruby-forum.com/u/Paul_Ash)\
**Post date:** [June 8, 2008, 4:30pm UTC](https://www.ruby-forum.com/t/rexml-nested-loops/139839/3 "2008-06-08T16:30:55Z")

</div>

I get that part of the syntax - what I don’t (or didn’t) get is why  
the two xpath operations I have are referring to the same document - I  
assumed they would be scoped to only the data passed to the block.

---

<div class="post-metadata">

**Author:** ![Paul\_Ash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@Paul\_Ash](https://www.ruby-forum.com/u/Paul_Ash)\
**Post date:** [June 9, 2008, 12:55pm UTC](https://www.ruby-forum.com/t/rexml-nested-loops/139839/4 "2008-06-09T12:55:25Z")

</div>

2008/6/8 Dennis E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I get that part of the syntax - what I don’t (or didn’t) get is why  
> the two xpath operations I have are referring to the same document - I  
> assumed they would be scoped to only the data passed to the block.

Well, they are because they use the node as basis. But if you give  
the command “search everything from the root on” then no matter where  
you start the root node of that document will determine the start  
position. Hence you have “.” to denote the current node.

Cheers

robert

---

<div class="post-metadata">

**Author:** ![Paul\_Ash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@Paul\_Ash](https://www.ruby-forum.com/u/Paul_Ash)\
**Post date:** [June 9, 2008, 9:21pm UTC](https://www.ruby-forum.com/t/rexml-nested-loops/139839/5 "2008-06-09T21:21:03Z")

</div>

On Jun 9, 4:53 am, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Cheers
> 
> robert
> 
> –  
> use.inject do |as, often| as.you\_can - without end

Thanks a lot Robert… makes perfect sense now.

Cheers.  
Dennis
