Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 285163 - Last Review: October 12, 2001 - Revision: 1.0
PRB: XPath Query Returns Unexpected Data
This article was previously published under Q285163
On This Page
SYMPTOMS
When you execute an XPath query similar to either of the following, the expected data may not be returned:
book/title[0]
book/title[1]
The first query is actually querying for the set of the first title nodes for each book node. The second query is also querying the set of all the second title nodes for each book node. In order to find instances of title nodes that occur under a specified book node, the queries should be as follows:
(book/title)[0]
(book/title)[1]
This is because of operator precedence, which, by default, associates the index qualifier with the title rather than the book/title path. The use of parentheses forces the index qualifier to be associated with all book/title nodes.
RESOLUTION
To resolve problems such as this with XPath queries, verify the syntax of the XPath query and ensure that operator precedence is observed.
STATUS
This behavior is by design.
MORE INFORMATION
Steps to Reproduce Behavior
- Insert the following code into a new document:
<HTML>
<BODY ONLOAD="DataDiv.innerText=XMLData.xml;">
<SCRIPT LANGUAGE="JavaScript">
function RunQuery(strQuery)
{
var ResultNodes, Node, savedNodeString = String("");
ResultNodes = XMLData.documentElement.selectNodes(strQuery);
for (Node = ResultNodes.nextNode(); Node != null; Node = ResultNodes.nextNode())
{
savedNodeString+=Node.xml;
}
ResultsDiv.innerText = savedNodeString;
return;
}
</SCRIPT>
<INPUT TYPE="Button" VALUE="Query for book/title[0]" STYLE="width:170;" ONCLICK="RunQuery('book/title[0]');">
<INPUT TYPE="Button" VALUE="Query for book/title[1]" STYLE="width:170;" ONCLICK="RunQuery('book/title[1]');">
<INPUT TYPE="Button" VALUE="Query for (book/title)[0]" STYLE="width:170;" ONCLICK="RunQuery('(book/title)[0]');">
<INPUT TYPE="Button" VALUE="Query for (book/title)[1]" STYLE="width:170;" ONCLICK="RunQuery('(book/title)[1]');">
<BR><BR><BR>
<CENTER><B>Results:</B></CENTER><BR>
<HR WIDTH="80%" NOSHADE>
<DIV ID="ResultsDiv" style="border-color:black;border-width:1;border-style:solid;width:100%;height:100;">
</DIV>
<BR>
<CENTER><B>Source Data:</B></CENTER><BR>
<HR WIDTH="80%" NOSHADE>
<DIV ID="DataDiv" style="border-color:black;border-width:1;border-style:solid;width:100%;height:100;">
</DIV>
<XML ID="XMLData">
<?xml version="1.0" ?>
<bookstore>
<book style="biography">
<title>Adam Barr: International Man of Mystery</title>
<author>John Coake</author>
<price>12</price>
</book>
<book style="textbook">
<title>A Nanotechnology Primer</title>
<author>Josh Barnhill</author>
<price>55</price>
</book>
<book style="storybook">
<title>XQL: The Golden Years</title>
<price>30</price>
</book>
</bookstore>
</XML>
</BODY>
</HTML>
- Save the document as Sample.htm, and then browse to it by using an Internet Explorer (IE) Web browser, version 5.0 or greater. Click the buttons at the top of the page, and note that the first two queries (in which there are no parentheses) return the first and second titles of each book. Because there are no second titles, the second query returns nothing. The last two queries (with parentheses) return the first book title and the second book title respectively.
APPLIES TO
- Microsoft XML Parser 2.0
- Microsoft XML Parser 2.5
- Microsoft XML Parser 2.6
- Microsoft XML Parser 3.0
- Microsoft XML Core Services 4.0
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
Be the first to leave feedback, to help others about this knowledge base
article.
(Optional) Name
(Optional)
Public URL Or Email
Comments
No
HTML -- Text Only Please