Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XPath

Reply
Thread Tools

XPath

 
 
SiJP
Guest
Posts: n/a
 
      12-11-2007


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)

 
Reply With Quote
 
 
 
 
Serge Baltic
Guest
Posts: n/a
 
      12-11-2007
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


 
Reply With Quote
 
 
 
 
Anthony Jones
Guest
Posts: n/a
 
      12-11-2007

"Serge Baltic" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
SiJP
Guest
Posts: n/a
 
      12-13-2007
Thanks very much guys. I wasn't far off, but now understand where I
went wrong!

Merrry Christmas,

Simon
 
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
"Memory leak" in javax.xml.xpath.XPath Marvin_123456 Java 4 07-29-2005 03:49 PM
XPath: efficiency in xpath expressions Tjerk Wolterink XML 1 11-13-2004 06:03 PM
Are there any XPath parsers that generate XPath trees? goog XML 0 01-14-2004 01:47 PM
XPath that does not include other XPath Anna XML 0 07-31-2003 07:55 AM
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