PRB: XPath Query Returns Unexpected Data (285163)



The information in this article applies to:

  • Microsoft XML 2.0
  • Microsoft XML 2.5
  • Microsoft XML 2.6
  • Microsoft XML 3.0
  • Microsoft XML 4.0

This article was previously published under Q285163

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

  1. 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>
    					
  2. 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.

Modification Type:MajorLast Reviewed:10/12/2001
Keywords:kbDSupport kbprb KB285163