Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 280457 - Last Review: October 12, 2001 - Revision: 1.2
PRB: Specifying Fully Qualified Element Names in XPath Queries
This article was previously published under Q280457
On This Page
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 : '<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Â
(http://kbalertz.com/Feedback.aspx?kbNumber=305019/EN-US/
)
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:
Execute the following steps in sequence to reproduce the problem described in the "Symptoms" section of this article:
- Save the following XML code on your hard drive in a file called Books.xml:
<?xml version="1.0"?>
<bk:Books xmlns:bk='http://myserver/myschemas/Books'>
<bk:Book>
<bk:Title>Just XML</bk:Title>
</bk:Book>
<bk:Book>
<bk:Title>Professional XML</bk:Title>
</bk:Book>
<bk:Book>
<bk:Title>XML Step by Step</bk:Title>
</bk:Book>
<bk:Book>
<bk:Title>XML By Example</bk:Title>
</bk:Book>
</bk:Books>
- Open a standard EXE project in Visual Basic. Form1 is created by default.
- On the Project menu, click References, and then set a reference to Microsoft XML, v3.0.
- Drag-and-drop a Command button onto Form1.
- 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 "<path to Books.xml>"
doc.setProperty "SelectionLanguage", "XPath"
'doc.setProperty "SelectionNamespaces", "xmlns:bk='http://myserver/myschemas/Books'"
Set BookList = doc.selectNodes("//bk:Book[position()<=2]")
For Each BookElem In BookList
Debug.Print BookElem.Text
Next
Set doc = Nothing
- 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'
- Stop the execution of the project, and uncomment the following line of code in the Command button's Click event procedure:
doc.setProperty "SelectionNamespaces", "xmlns:bk='http://myserver/myschemas/Books'"
- 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.
APPLIES TO
- Microsoft XML Parser 3.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 Enterprise Edition for Windows 6.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