XPath

SiJP

Usenet Archive
Joined
Nov 11, 2005
Messages
3
Reaction score
0
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)
 

Serge Baltic

Usenet Archive
Joined
Dec 11, 2007
Messages
1
Reaction score
0
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
 

Anthony Jones

Usenet Archive
Joined
Apr 8, 2004
Messages
1,734
Reaction score
0
Serge Baltic said:
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?
 

SiJP

Usenet Archive
Joined
Nov 11, 2005
Messages
3
Reaction score
0
Thanks very much guys. I wasn't far off, but now understand where I went wrong!

Merrry Christmas,

Simon
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,049
Messages
3,104,245
Members
248,133
Latest member
DujonT

Latest Threads

Top