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. On Jun 8, 2:35 ¨Âí¬ Òïâåòô Ëìåííå ¼óèïòôãõô®®®Àçïïçìåíáéì®ãïí¾ ÷òïôåº > On 08.06.2008 00:15, Paul Ash wrote: > > > > > I'm a bit of a newbie to Ruby, and to xpath..... and hoping someone herean give me the one-liner version of why this is failing. > > > I have an XML structure like: > > > <foos> > > <foo> > > <fooID>Foo1</fooID> > > <subfoos> > > <subfoo> > > <subfooName>foobar1</subfooName> > > </subfoo> > > <subfoo> > > <subfooName>foobar2</subfooName> > > </subfoo> > > </subfoos> > > </foo> > > <foo> > > <fooID>Foo2</fooID> > > <subfoos> > > <subfoo> > > <subfooName>foobar3</subfooName> > > </subfoo> > > <subfoo> > > <subfooName>foobar4</subfooName> > > </subfoo> > > </subfoos> > > </foo> > > </foos> > > > And the code > > > doc.each_element('//foo') { |foo| > > ¨Âõô¢ªªª Ðòïãåóóéîç £ûæïï®åìåíåîôóÛ§æïïÉä§Ý®ôåøôý¢ > > ¨Âïï®åáãèßåìåíåîô¨§¯¯óõâÆïï§üóõâÆïïü > > ¨ÂõôóõâÆïï®åìåíåîôóÛ¢óõâæïïÎáíå¢Ý®ôåøô > > ¨Â > > ¨Â > > > What I was expecting was a nested loop.. but what I get is: > > > doc.each_element('//foo') { |foo| > > > This iterator does what I expect. > > > ¨Âïï®åáãèßåìåíåîô¨§¯¯óõâÆïï§üóõâÆïïü > > > 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... butan 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. ¨Â ãáôèéîïæ ô÷òåíåäéåó> > 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.zvon.org/xxl/XPathTutorial/General/examples.html > > And then there's of course the standard: > > http://www.w3.org/TR/xpath > > Kind regards > > robert