Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   XML (http://www.velocityreviews.com/forums/f32-xml.html)
-   -   XPath expression (http://www.velocityreviews.com/forums/t547624-xpath-expression.html)

piramido 10-29-2007 02:01 AM

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


Richard Tobin 10-29-2007 08:48 AM

Re: XPath expression
 
In article <1193623318.972830.51690@o80g2000hse.googlegroups. com>,
piramido <piramido@googlemail.com> wrote:

>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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.

Philippe Poulard 10-29-2007 09:42 AM

Re: XPath expression
 
Richard Tobin a écrit :
> In article <1193623318.972830.51690@o80g2000hse.googlegroups. com>,
> piramido <piramido@googlemail.com> wrote:
>
>> 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 !

jasonturner76@googlemail.com 10-29-2007 10:08 AM

Re: XPath expression
 
Have a look at Xml Studio, it has a free XPath visualiser.

http://www.liquid-technologies.com/P...nShots.aspx?11


On 29 Oct, 02:01, piramido <piram...@googlemail.com> wrote:
> 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




piramido 10-29-2007 10:12 AM

Re: XPath expression
 
Hello Richard,

it works. Thank you so much!

Kind regards,
Sascha



All times are GMT. The time now is 04:00 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.