PRB: Specifying Fully Qualified Element Names in XPath Queries (280457)



The information in this article applies to:

  • Microsoft XML 3.0
  • Microsoft XML 3.0 SP1
  • Microsoft XML 4.0
  • Microsoft Visual Basic Professional Edition for Windows 5.0
  • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic Enterprise Edition for Windows 5.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0

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 : '<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: Execute the following steps in sequence to reproduce the problem described in the "Symptoms" section of this article:
  1. 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>
    					
  2. Open a standard EXE project in Visual Basic. Form1 is created by default.
  3. On the Project menu, click References, and then set a reference to Microsoft XML, v3.0.
  4. Drag-and-drop a Command button onto Form1.
  5. 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
    					
  6. 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'
  7. 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'"
    					
  8. 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.

Modification Type:MajorLast Reviewed:10/12/2001
Keywords:kbprb KB280457