I have this xml snippet:
<book id="bk102">
<author name= "Smith, john" >
<title>Test book</title>
<genre>Other</genre>
<price>30.00</price>
<publish_date>2003-10-01</publish_date>
</author>
</book>

and I am writing an xpath parser this way:
//book[@id='bk102']//book[@genre ='Other']//book[price=30.00]

I know that I can get it by using just the first query,
( //book[@id='bk102'] )
but what if I want to use the one above? I don't get anything back from selectNodes.

MSXML2::IXMLDOMNodeListPtr pXMLNodeList = NULL;
pXMLNodeList = pXMLDoc->selectNodes("//book[@id='bk102']//book[@genre ='Other']//book[price=30.00]");

Thanks in advance.

>I don't get anything back from selectNodes.
Because what you're asking for isn't what you intended to ask for. Let's break it down:

>//book[@id='bk102']
This is okay, it selects the book element with an id attribute that matches the string 'bk102'. So far so good.

>//book[@genre ='Other']
book doesn't have a genre attribute, which is what the @ prefix says. You need to specify that genre is an element and that it's in a child element of book.

>//book[price=30.00]
Same thing, you're missing an intervening element because price (like genre) is an element of author, which is a child of book. This isn't the best XML design in my opinion, but I'm working with what you've provided.

What you want to do is select a book with a matching id and an author with a matching genre and an author with a matching price. Here are two ways to do it:

//book[@id='bk102' and author/genre['Other'] and author/price['30.00']]

//book[@id='bk102']/author[genre['Other'] and price['30.00']]/parent::*
This article has been dead for over six months. Start a new discussion instead.