XPath

Discussion in 'XML' started by SiJP, Dec 11, 2007.

  1. SiJP

    SiJP Guest

    Consider the following XML

    <?xml version="1.0" encoding="UTF-8"?>
    <Root>
    <Data>
    <ID>1005</ID>
    <Reference>
    <Reference>ABC</Reference>
    <Country>UK</Country>
    </Reference>
    <Reference>
    <Reference>123</Reference>
    <Country>USA</Country>
    </Reference>
    </Data>
    </Root>


    Using XPath, I would like to: Select the value of the Reference Node,
    Where the ID = 1005, And the Country is USA. The value of the node
    would therefore be 123.

    I'm not great with XPath, but had a go and got it completely wrong:
    XPath = "//Data/[ID ="1005"]/../Reference/[Country="USA"]/Reference"

    I'd be ever so grateful if someone can help out with this XPath!

    Cheers
    Simon


    (If its any help, I'm using vs.net 2005 and trying to populate an
    XPathNodeIterator using XPathNavigator.Select)
     
    SiJP, Dec 11, 2007
    #1
    1. Advertising

  2. SiJP

    Serge Baltic Guest

    Hello,

    > Consider the following XML
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > <Root>
    > <Data>
    > <ID>1005</ID>
    > <Reference>
    > <Reference>ABC</Reference>
    > <Country>UK</Country>
    > </Reference>
    > <Reference>
    > <Reference>123</Reference>
    > <Country>USA</Country>
    > </Reference>
    > </Data>
    > </Root>
    > Using XPath, I would like to: Select the value of the Reference Node,
    > Where the ID = 1005, And the Country is USA. The value of the node
    > would therefore be 123.
    >
    > I'm not great with XPath, but had a go and got it completely wrong:
    > XPath = "//Data/[ID ="1005"]/../Reference/[Country="USA"]/Reference"


    Should be something like /Root/Data[ID='1005']/Reference[Country='USA']/Reference

    Square brackets should apply to the node you're selecting, rather than go
    as an independent path element.

    BTW it's a logical error to have an element named “Reference†take part as
    a grouping node at one place and as a value holder in another. That is, the
    semantics should be consistend per element name throughout the document.
    One of them'd better be renamed.

    (H) Serg
     
    Serge Baltic, Dec 11, 2007
    #2
    1. Advertising

  3. "Serge Baltic" <> wrote in message
    news:...
    > Hello,
    >
    > > Consider the following XML
    > >
    > > <?xml version="1.0" encoding="UTF-8"?>
    > > <Root>
    > > <Data>
    > > <ID>1005</ID>
    > > <Reference>
    > > <Reference>ABC</Reference>
    > > <Country>UK</Country>
    > > </Reference>
    > > <Reference>
    > > <Reference>123</Reference>
    > > <Country>USA</Country>
    > > </Reference>
    > > </Data>
    > > </Root>
    > > Using XPath, I would like to: Select the value of the Reference Node,
    > > Where the ID = 1005, And the Country is USA. The value of the node
    > > would therefore be 123.
    > >
    > > I'm not great with XPath, but had a go and got it completely wrong:
    > > XPath = "//Data/[ID ="1005"]/../Reference/[Country="USA"]/Reference"

    >
    > Should be something like

    /Root/Data[ID='1005']/Reference[Country='USA']/Reference
    >
    > Square brackets should apply to the node you're selecting, rather than go
    > as an independent path element.
    >
    > BTW it's a logical error to have an element named "Reference" take part as
    > a grouping node at one place and as a value holder in another. That is,

    the
    > semantics should be consistend per element name throughout the document.
    > One of them'd better be renamed.
    >



    Whilst I agree that naming a child element using the same name as the parent
    is a little odd. In what way is it a 'Logical error'? Where can these
    logic rules be found?

    --
    Anthony Jones - MVP ASP/ASP.NET
     
    Anthony Jones, Dec 11, 2007
    #3
  4. SiJP

    SiJP Guest

    Thanks very much guys. I wasn't far off, but now understand where I
    went wrong!

    Merrry Christmas,

    Simon
     
    SiJP, Dec 13, 2007
    #4
    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,983
    jan V
    Jul 29, 2005
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,423
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. Anna
    Replies:
    0
    Views:
    532
  4. goog
    Replies:
    0
    Views:
    514
  5. Tjerk Wolterink

    XPath: efficiency in xpath expressions

    Tjerk Wolterink, Nov 13, 2004, in forum: XML
    Replies:
    1
    Views:
    1,653
    Richard Tobin
    Nov 13, 2004
Loading...

Share This Page