Microsoft KB Archive/280457

= PRB: Specifying Fully Qualified Element Names in XPath Queries =

Article ID: 280457

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
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q280457



SYMPTOMS
Specifying an XPath query as the query string parameter in a call to the selectNodes or selectSingleNode MSXML DOM methods may generate the following error message when the query includes one or more fully qualified element names in the NamespacePrefix:ElementName format:

Run-time error '-2147467259(80004005)': Reference to undeclared namespace prefix : ''



CAUSE
The SelectionNamespaces property of the MSXML DOMDocument object has not been set to specify the namespaces that are used in the XML document.



RESOLUTION
Use the SetProperty method of the MSXML DOMDocument object to set its SelectionNamespaces property and specify the namespaces to be used in the XML document.



STATUS
This behavior is by design.



MORE INFORMATION
If a newer version of MSXML has been installed in side-by-side mode, you must explicitly use the Globally Unique Identifiers (GUIDs) or ProgIDs for that version to run the sample code. For example, MSXML version 4.0 can only be installed in side-by-side mode. For additional information about the code changes that are required to run the sample code with the MSXML 4.0 parser, click the following article number to view the article in the Microsoft Knowledge Base:

305019 INFO: MSXML 4.0 Specific GUIDs and ProgIds

Steps to Reproduce Behavior
Namespace support when querying the DOM by using XPath was first introduced in version 3.0 of the MSXML parser. Version 3.0 is currently the latest release of the MSXML parser. You need to install MSXML version 3.0 on your development computer to set up and test the following code sample.

The latest version of the MSXML parser and the MSXML 3.0 SDK can be downloaded from MSDN online XML Developer Center on the following Web site at:

http://msdn.microsoft.com/xml/default.asp

Execute the following steps in sequence to reproduce the problem described in the &quot;Symptoms&quot; section of this article:   Save the following XML code on your hard drive in a file called Books.xml:    Just XML   Professional XML</bk:Title> </bk:Book>  XML Step by Step</bk:Title> </bk:Book>  XML By Example</bk:Title> </bk:Book> </bk:Books> </li> <li>Open a standard EXE project in Visual Basic. Form1 is created by default.</li> <li>On the Project menu, click References, and then set a reference to Microsoft XML, v3.0.</li> <li>Drag-and-drop a Command button onto Form1.</li> <li> Copy and paste the following code into the Click event procedure of the Command button, and specify the path to Books.xml in the doc.Load statement: Dim doc As MSXML2.DOMDocument30 Dim BookList As MSXML2.IXMLDOMNodeList Dim BookElem As MSXML2.IXMLDOMElement

Set doc = New MSXML2.DOMDocument30

doc.Load &quot;<path to Books.xml>&quot; doc.setProperty &quot;SelectionLanguage&quot;, &quot;XPath&quot; 'doc.setProperty &quot;SelectionNamespaces&quot;, &quot;xmlns:bk='http://myserver/myschemas/Books'&quot;

Set BookList = doc.selectNodes(&quot;//bk:Book[position<=2]&quot;) For Each BookElem In BookList Debug.Print BookElem.Text Next

Set doc = Nothing </li> <li>Save and run the project, and note that clicking the Command button when Form1 is displayed generates the following error message:

Run-time error '-2147467259(80004005)': Reference to undeclared namespace prefix : 'bk'

</li> <li> Stop the execution of the project, and uncomment the following line of code in the Command button's Click event procedure: doc.setProperty &quot;SelectionNamespaces&quot;, &quot;xmlns:bk='http://myserver/myschemas/Books'&quot; </li> <li>Save and run the project again, and note that clicking the Command button now displays the Titles of the first two Book elements in Books.xml. The XPath query specified as the QueryString parameter in the call to the selectNodes method of the DOMDocument30 object uses the position XPath function to extract the first two Book elements.</li></ol>

Keywords: kbprb KB280457

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.