954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

xpath question

I have this xml snippet:
Test bookOther30.002003-10-01

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.

demonic_death
Newbie Poster
1 post since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

>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::*
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You