Microsoft KB Archive/313372

= PRB: MSXML 4.0: Matching Nodes Are Not Returned When You Run XPath Queries Against XML Documents that Specify a Default Namespace Declaration =

Article ID: 313372

Article Last Modified on 9/26/2005

-

APPLIES TO


 * Microsoft XML Core Services 4.0

-



This article was previously published under Q313372



SYMPTOMS
When you use the MSXML 4.0 Document Object Model (DOM) methods (selectNodes and selectSingleNode) to run XPath queries against an XML document that specifies a default namespace declaration, the matching node or nodes are not returned.



CAUSE
The default namespace declaration is not added to the Namespace names of the DOMDocument object, or a namespace prefix is not specified for the default namespace declaration when it is added to the Namespace names of the DOMDocument object.



RESOLUTION
Add the default namespace declaration to the Namespace names of the DOMDocument object by specifying a namespace prefix. To do this, use the setProperty method of the DOMDocument object to set the SelectionNamespaces internal property.



STATUS
This behavior is by design.



MORE INFORMATION
MSXML 4.0 no longer supports the XSLPattern query language. It only supports the XPath query language. This implies that simple tree-style hierarchical queries to access nodes in an XML document are also run as XPath queries in MSXML 4.0. When you use the MSXML 4.0 DOM to run XPath queries against an XML document that specifies a default namespace declaration, you must use the setProperty method of the DOMDocument object to add the default namespace declaration by specifying a namespace prefix, to the Namespace names of the DOMDocument object.

Steps to Reproduce Behavior
To recreate the problem and test the specified resolution by using a Visual Basic Standard EXE project, follow these steps:   In Notepad, create an XML document that contains the following XML named Books.xml in the root folder of drive C:    Beginning XML Wrox   XML Step by Step</Title> MSPress</Publisher> </Book>  Professional XML</Title> Wrox</Publisher> </Book>  Developing XML solutions</Title> MSPress</Publisher> </Book> </Books> </li> <li>In Visual Basic, create a new Standard EXE project.</li> <li>Add a reference to Microsoft XML, version 4.0.</li> <li>Drag a command button onto Form1.</li> <li> Paste the following code in the Click event procedure of the command button:NOTE: This code loads the sample Books.xml document into an instance of the MSXML 4.0 DOMDocument40 object. It then uses the SelectNodes DOM method to run an XPath query against the DOMDocument40 object to identify the titles that are published by MSPress. Dim xmldoc As MSXML2.DOMDocument40 Dim bookList As MSXML2.IXMLDOMNodeList Dim bookNode As MSXML2.IXMLDOMNode

Set xmldoc = New MSXML2.DOMDocument40 'xmldoc.setProperty &quot;SelectionNamespaces&quot;, &quot;xmlns:bk='urn:books'&quot; xmldoc.Load &quot;c:\books.xml&quot;

Set bookList = xmldoc.selectNodes(&quot;//Publisher[. = 'MSPress']/parent::node/Title&quot;) 'Set bookList = xmldoc.selectNodes(&quot;//bk:Publisher[. = 'MSPress']/parent::node/bk:Title&quot;)

For Each bookNode In bookList Debug.Print &quot;Title : &quot; & bookNode.Text Next </li> <li>Save and run the project.</li> <li>Click the command button when the form is displayed. Note that although the XPath query is valid, it does not generate any results, and the matching titles are not written to the Visual Basic Immediate window.</li> <li>Stop the project.</li> <li> Uncomment the following line of code immediately after the Set xmldoc = New MSXML2.DOMDocument40 statement to add the default namespace declaration by specifying a namespace prefix to the Namespace names of the DOMDocument object: 'xmldoc.setProperty &quot;SelectionNamespaces&quot;, &quot;xmlns:bk='urn:books'&quot; </li> <li> Comment the call to the selectNodes DOM method that is used to execute the XPath query. Set bookList = xmldoc.selectNodes(&quot;//Publisher[. = 'MSPress']/parent::node/Title&quot;) </li> <li> Uncomment the following statement: 'Set bookList = xmldoc.selectNodes(&quot;//bk:Publisher[. = 'MSPress']/parent::node/bk:Title&quot;) Note that the element names are now prefixed with the bk namespace alias that you specify when the default namespace declaration is added to the Namespace names of the DOMDocument object.

</li> <li>Save and run the project. Click the command button when the form is displayed. Note that the matching nodes are listed in the Visual Basic Immediate window.</li></ol>

<div class="references_section">