Microsoft KB Archive/288147

= PRB: Using XPath to Query Against a User-Defined Default Namespace =

Article ID: 288147

Article Last Modified on 10/12/2001

-

APPLIES TO


 * Microsoft XML Core Services 4.0
 * Microsoft XML Parser 3.0 Service Pack 1
 * Microsoft XML Core Services 4.0

-



This article was previously published under Q288147



SYMPTOMS
When you redefine the default namespace in the XML document, change the internal SelectionNamespaces property for the default namespace, and then try to programmatically use XPath to return nodes by using the selectNodes or selectSingleNodes method, no nodes are returned.



RESOLUTION
Either use prefixes with the namespaces when you specify the SelectionNamespaces property, or use Extensible Stylesheet Language (XSL) pattern-matching operations instead of XPath.



MORE INFORMATION
This behavior is by design.

Implementation of XPath queries against the DOMDocument requires that namespaces be declared in the Document Object Model (DOM) before the selectNodes or selectSingleNode methods are run.

Steps to Reproduce Behavior
 In Visual Basic, create a new Standard EXE project. Form1 is created by default.  Paste the following code into the Form_Unload event of Form1: ' You can change the ProgID to reflect the installed version of the Microsoft XML Parser: ' For example, with MSXML 4, you would use: '  Dim xmlDom As MSXML2.DOMDocument40 Dim xmlDom As MSXML2.DOMDocument Dim nodeList As MSXML2.IXMLDOMNodeList Dim iNode As MSXML2.IXMLDOMNode ' For example, with MSXML 4, you would use: ' Set xmlDom = New MSXML2.DOMDocument40

Set xmlDom = New MSXML2.DOMDocument With xmlDom .async = False .loadXML &quot;&quot; & _ &quot;&quot; & _ &quot;  This is a Test&quot; & _ &quot;&quot; .setProperty &quot;SelectionLanguage&quot;, &quot;XPath&quot; .setProperty &quot;SelectionNamespaces&quot;, &quot;xmlns='uri:MyNameSpace'&quot; Set nodeList = .selectNodes(&quot;//Test&quot;) End With

For Each iNode In nodeList msgbox iNode.xml Next iNode  On the Project menu, click References. From the list of available references, select Microsoft XML,v3.0 or later. Run the project. No XML is returned.</li>  Comment out the two setProperty methods.NOTE: Alternatively, you can modify the two lines of code as follows: .setProperty &quot;SelectionNamespaces&quot;, &quot;xmlns:myNS='uri:MyNameSpace'&quot; Set nodeList = .selectNodes(&quot;//myNS:Test&quot;) </li> Run the project. The XML nodes are returned.</li></ol>