PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > rexml & nested loops
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
rexml & nested loops

Réponse
 
LinkBack Outils de la discussion
Vieux 07/06/2008, 23h15   #1
Paul Ash
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut rexml & nested loops

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:

<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|
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



  Réponse avec citation
Vieux 08/06/2008, 09h35   #2
Robert Klemme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: rexml & nested loops

On 08.06.2008 00:15, Paul Ash wrote:
> 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:
>
> <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|
> 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?


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.zvon.org/xxl/XPathTutoria.../examples.html

And then there's of course the standard:

http://www.w3.org/TR/xpath

Kind regards

robert
  Réponse avec citation
Vieux 08/06/2008, 15h26   #3
Dennis Edwards
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: rexml & nested loops

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:35am, Robert Klemme <shortcut...@googlemail.com> wrote:
> On 08.06.2008 00:15, Paul Ash wrote:
>
>
>
> > 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:

>
> > <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|
> > 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?

>
> 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:.../examples.html
>
> And then there's of course the standard:
>
> http://www.w3.org/TR/xpath
>
> Kind regards
>
> robert


  Réponse avec citation
Vieux 09/06/2008, 11h53   #4
Robert Klemme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: rexml & nested loops

2008/6/8 Dennis Edwards <dedward@gmail.com>:
> 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

--
use.inject do |as, often| as.you_can - without end

  Réponse avec citation
Vieux 09/06/2008, 20h00   #5
Dennis Edwards
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: rexml & nested loops

On Jun 9, 4:53am, Robert Klemme <shortcut...@googlemail.com> wrote:
> 2008/6/8 Dennis Edwards <dedw...@gmail.com>:
>
> > 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
>
> --
> use.inject do |as, often| as.you_can - without end


Thanks a lot Robert.. makes perfect sense now.

Cheers.
Dennis
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h56.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15734 seconds with 13 queries