Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XPath query problem

Status
Not open for further replies.

Ator

Programmer
Jan 23, 2006
26
SE
Hi

I have a XML file like below.
This XML is loaded into a DataSet, and I then create a XmlDataDocument. (Environment = VS, C#)

Code:
XmlDataDocument doc = new XmlDataDocument(dataset);

Using this document, I create a XPathNavigator:
Code:
 XPathNavigator nav = doc.CreateNavigator();

I now want to pick all <author>s who has written book "Book nr 1" (only one in this case).
But how do I do this?

Code:
 XmlNodeList nodeList = doc.SelectNodes("//root/authors/author[bookID=...]");

How do I connect bookID in one single XPath-query?

Kind Regards

Code:
<root>
 <authors>
  <author>
   <name>John</name>
   <bookID>1</bookID>
  </author>
  <author>
   <name>Peter</name>
   <bookID>2</bookID>
  </author>  
 </authors>
 <books>
  <book>
   <bookID>1</bookID>
   <name>Book nr 1</name>
  </book>
  <book>
   <bookID>2</bookID>
   <name>Book nr 2</name>
  </book>  
 </books>
</root>
 
You did not mention the context. Try this.

[tt] XmlNodeList nodeList = doc.SelectNodes("[highlight]/[/highlight]root/authors/author[bookID=[blue]current()/parent::*/bookID[/blue]]");[/tt]

 
Further note:
The above assume the context node is on a /root/books/book/name. If it is not, it would sure be inapplicable and need change. Looking again at your line, maybe that's not what you are on.
 
Correction:

Sorry! I should not use xslt function at all in the context. Please disregard the above. Try instead this.
[tt]
XmlNodeList nodeList = doc.SelectNodes("/root/authors/author[bookID='") + onode.SelectSingleNode("./parent::*/bookID").text + "']")
[/tt]
where onode is the current node at one of the /root/books/book/name.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top