XPath expression

P

piramido

Hello,

I'm in trouble with a simple XPath expression. I've got the following
XML file

<?xml version="1.0" encoding="UTF-8"?>
<a>
<b>
<c>A</c>
</b>
<b>
<c>B</c>
</b>
<b>
<c>C</c>
</b>
<b>
<c>D</c>
<c>E</c>
<c>F</c>
<c>G</c>
</b>
</a>

and I'd like to reference the c-element with content B, i.e. the
second c-element in the file. But when I execute a/b/c[2], I get the c-
element with content E (the fifth one). Can somebody give me a hint in
what is wrong with this expression. In my opinion, at first a/b/c is
evaluated, which give a set of 7 nodes (7 c-element). Out of this set
the second node is taken, which would be the c-element with content B.

Best regards,
Sascha
 
R

Richard Tobin

piramido said:
and I'd like to reference the c-element with content B, i.e. the
second c-element in the file. But when I execute a/b/c[2], I get the c-
element with content E (the fifth one). Can somebody give me a hint in
what is wrong with this expression. In my opinion, at first a/b/c is
evaluated, which give a set of 7 nodes (7 c-element). Out of this set
the second node is taken, which would be the c-element with content B.

That's not how it works. You don't get the second node of a/b/c;
you get the c[2] of a/b. Only the fourth a/b has a c[2], and that's
the one you get.

(a/b/c)[2] will select the right element.

-- Richard
 
P

Philippe Poulard

Richard Tobin a écrit :
piramido said:
and I'd like to reference the c-element with content B, i.e. the
second c-element in the file. But when I execute a/b/c[2], I get the c-
element with content E (the fifth one). Can somebody give me a hint in
what is wrong with this expression. In my opinion, at first a/b/c is
evaluated, which give a set of 7 nodes (7 c-element). Out of this set
the second node is taken, which would be the c-element with content B.

That's not how it works. You don't get the second node of a/b/c;
you get the c[2] of a/b. Only the fourth a/b has a c[2], and that's
the one you get.

and a/b/c[1] will give you the 4 c elements that contain A B C D : you
evaluate a step upon the result of the previous one ; but as Richard
shows you, () are used for grouping
(a/b/c)[2] will select the right element.

-- Richard


--
Cordialement,

///
(. .)
--------ooO--(_)--Ooo--------
| Philippe Poulard |
-----------------------------
http://reflex.gforge.inria.fr/
Have the RefleX !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top