XPath expression

Discussion in 'XML' started by piramido, Oct 29, 2007.

  1. piramido

    piramido Guest

    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, Oct 29, 2007
    #1
    1. Advertising

  2. In article <>,
    piramido <> 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.
    Richard Tobin, Oct 29, 2007
    #2
    1. Advertising

  3. Richard Tobin a écrit :
    > In article <>,
    > piramido <> 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 !
    Philippe Poulard, Oct 29, 2007
    #3
  4. piramido

    Guest

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

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


    On 29 Oct, 02:01, piramido <> 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
    , Oct 29, 2007
    #4
  5. piramido

    piramido Guest

    Hello Richard,

    it works. Thank you so much!

    Kind regards,
    Sascha
    piramido, Oct 29, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    1,958
    jan V
    Jul 29, 2005
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,370
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. Anna
    Replies:
    0
    Views:
    509
  4. goog
    Replies:
    0
    Views:
    497
  5. GIMME
    Replies:
    3
    Views:
    11,924
    vforvikash
    Dec 29, 2008
Loading...

Share This Page