Microsoft KB Archive/294797

= How To Specify Namespace when Querying the DOM with XPath =

Article ID: 294797

Article Last Modified on 7/13/2004

-

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 Q294797



SUMMARY
With the Microsoft XML Parser (MSXML) 3.0 release, XPath provides a convenient way to query XML documents and return a node or a node set. When you use XPath query with the selectSingleNode and selectNodes methods of the IXMLDOMNode object, you must use qualified names. For example, to select the Book node with the following XML data    Presenting XML Richard Light   if we use a as the alias of the x-schema:bookschema.xml Uniform Resource Identifier (URI), the corresponding XPath query is the following: pXMLDoc->setProperty(&quot;SelectionNamespaces&quot;,&quot;xmlns:a='x-schema:bookschema.xml'&quot;); pXMLDoc->documentElement->selectNodes(&quot;/a:Books/a:Book&quot;); In this case, using the qualified name is straightforward. When the default namespace is used, however, using the qualified name can be more difficult, as in the following example:    Presenting XML Richard Light   Note that no prefix is used in the node tags. The qualified name must still be used inside the XPath query, otherwise the query (for example, /Books/Book) returns no result because there are no matching nodes.



MORE INFORMATION
The following Visual C++ sample is provided to demonstrate the technique.

To specify the namespace when you query the DOM with XPath, follow these steps:   Create a Win32 console project, and add a new .cpp file to the project. Paste the following code into the .cpp file and name the file Test.cpp:
 * 1) include 

using namespace MSXML2;
 * 1) import &quot;msxml3.dll&quot;

void dump_com_error(_com_error &e);

int main(int argc, char* argv[]) {   CoInitialize(NULL); try{ IXMLDOMDocument2Ptr pXMLDoc; HRESULT hr = pXMLDoc.CreateInstance(__uuidof(DOMDocument)); pXMLDoc->async = false; // default - true, pXMLDoc->validateOnParse = true; hr = pXMLDoc->load(&quot;books.xml&quot;); if(hr!=VARIANT_TRUE) {           IXMLDOMParseErrorPtr  pError; pError = pXMLDoc->parseError; _bstr_t parseError =_bstr_t(&quot;At line &quot;)+ _bstr_t(pError->Getline) + _bstr_t(&quot;\n&quot;)+ _bstr_t(pError->Getreason); MessageBox(NULL,parseError, &quot;Parse Error&quot;,MB_OK); return -1; }       hr = pXMLDoc->setProperty(&quot;SelectionLanguage&quot;, &quot;XPath&quot;); hr = pXMLDoc->setProperty(&quot;SelectionNamespaces&quot;, &quot;xmlns:a='x-schema:bookschema.xml'&quot;);

IXMLDOMNodeListPtr pNodeList; pNodeList = pXMLDoc->documentElement->selectNodes(&quot;/a:Books/a:Book&quot;); int count = pNodeList->Getlength; char pLength[64]; sprintf(pLength, &quot;Total number of nodes selected is %d&quot;, count); MessageBox(NULL,pLength,&quot;Test&quot;, MB_OK); }   catch(_com_error &e) {       dump_com_error(e); return -1; }   return 0; }

void dump_com_error(_com_error &e) {   printf(&quot;Error\n&quot;); printf(&quot;\a\tCode = %08lx\n&quot;, e.Error); printf(&quot;\a\tCode meaning = %s&quot;, e.ErrorMessage); _bstr_t bstrSource(e.Source); _bstr_t bstrDescription(e.Description); printf(&quot;\a\tSource = %s\n&quot;, (LPCSTR) bstrSource); printf(&quot;\a\tDescription = %s\n&quot;, (LPCSTR) bstrDescription); }

</li> <li> Save the following XML as Books.xml in the same project folder as Test.cpp. <?xml version =&quot;1.0&quot;?>   Presenting XML Richard Light 334 </Book>  Mastering XML John Smith 209 </Book> </Books> </li> <li> Save the following XML as Bookschema.xml in the same project folder as Test.cpp. <?xml version=&quot;1.0&quot;?> <Schema xmlns=&quot;urn:schemas-microsoft-com:xml-data&quot;> <ElementType name=&quot;title&quot; /> <ElementType name=&quot;author&quot; /> <ElementType name=&quot;pages&quot; /> <ElementType name=&quot;Book&quot; model=&quot;closed&quot;> <element type=&quot;title&quot; /> <element type=&quot;author&quot; /> <element type=&quot;pages&quot; /> </ElementType> <ElementType name=&quot;Books&quot; model=&quot;closed&quot;> <element type=&quot;Book&quot; /> </ElementType> </Schema> </li> <li>Compile and run the application. A message box shows the number of nodes that are returned by the XPath query.</li></ol>

Note the following: <ul> <li>The same sample code can used with an explicit URI as a namespace.</li> <li> In the following lines IXMLDOMDocument2Ptr pXMLDoc; ... hr = pXMLDoc->setProperty(&quot;SelectionLanguage&quot;, &quot;XPath&quot;); hr = pXMLDoc->setProperty(&quot;SelectionNamespaces&quot;, &quot;xmlns:a='x-schema:bookschema.xml'&quot;); ... pNodeList = pXMLDoc->documentElement->selectNodes(&quot;/a:Books/a:Book&quot;); the setProperty method is not available with the IXMLDOMDocument interface. </li></ul>

A qualified name (QName) is composed of a prefix and a local part. The prefix provides the namespace prefix of the qualified name, and must be associated with a namespace URI.

<div class="references_section">