Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XPath expression

Reply
Thread Tools

XPath expression

 
 
piramido
Guest
Posts: n/a
 
      10-29-2007
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

 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      10-29-2007
In article <(E-Mail Removed). com>,
piramido <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Philippe Poulard
Guest
Posts: n/a
 
      10-29-2007
Richard Tobin a écrit :
> In article <(E-Mail Removed). com>,
> piramido <(E-Mail Removed)> 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 !
 
Reply With Quote
 
jasonturner76@googlemail.com
Guest
Posts: n/a
 
      10-29-2007
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 <(E-Mail Removed)> 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



 
Reply With Quote
 
piramido
Guest
Posts: n/a
 
      10-29-2007
Hello Richard,

it works. Thank you so much!

Kind regards,
Sascha

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
"Memory leak" in javax.xml.xpath.XPath Marvin_123456 Java 4 07-29-2005 03:49 PM
Problem selecting a node with XPATH if attribute value contains backslashes - how to force XPATH string to be treated as literal? Alastair Cameron XML 1 07-08-2003 07:24 PM



Advertisments