XPath expression to "select all"?

Discussion in 'XML' started by JS, May 4, 2009.

  1. JS

    JS Guest

    I have this XML file:

    <root id="root" >
    <head>
    <section secid="section">
    <subsection>
    <item order="0">
    <data id="D1" value="red"/>
    <data id="D2" value="blue"/>
    </item>
    <item order="1">
    <data id="D1" value="green"/>
    <data id="D2" value="yellow"/>
    </item>
    <item order="2">
    <data id="D1" value="black"/>
    <data id="D2" value="white"/>
    </item>
    </subsection>
    </section>
    </head>
    </root>

    I would like to create a XPath expression that selects all values where data
    id="D1". The result should be:

    red
    green
    black

    I have tried something like (assuming that we are standing in root):

    /head/section/subsection/item/data[@id="D1"]/@value

    but it only returns the first value:

    red

    How do I get the values from the remaining item elements?
    JS, May 4, 2009
    #1
    1. Advertising

  2. JS wrote:
    > /head/section/subsection/item/data[@id="D1"]/@value
    >
    > but it only returns the first value:


    Actually, it is returning all the values, but the string value of a set
    of results is the string value of the first result.

    To get the rest, you need to iterate through the results. How you do
    that depends on the tool you're using to execute this XPath.
    Joe Kesselman, May 5, 2009
    #2
    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. Hannes Heckner
    Replies:
    1
    Views:
    759
    Martin Honnen
    Oct 13, 2003
  2. GIMME
    Replies:
    3
    Views:
    11,923
    vforvikash
    Dec 29, 2008
  3. -berlin.de
    Replies:
    7
    Views:
    1,058
    Richard Tobin
    Feb 26, 2007
  4. eggie5
    Replies:
    1
    Views:
    526
    Joseph Kesselman
    Oct 2, 2007
  5. Matt
    Replies:
    2
    Views:
    193
    Roland Hall
    Jan 11, 2004
Loading...

Share This Page