How To Specify Namespace when Querying the DOM with XPath (294797)
The information in this article applies to:
- Microsoft XML 3.0
- Microsoft XML 3.0 SP1
- Microsoft XML 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
<?xml version ="1.0"?>
<a:Books xmlns:a="x-schema:bookschema.xml" >
<a:Book>
<title>Presenting XML</title>
<author>Richard Light</author>
</a:Book>
</a:Books>
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("SelectionNamespaces","xmlns:a='x-schema:bookschema.xml'");
pXMLDoc->documentElement->selectNodes("/a:Books/a:Book");
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:
<?xml version ="1.0"?>
<Books xmlns="x-schema:bookschema.xml" >
<Book>
<title>Presenting XML</title>
<author>Richard Light</author>
</Book>
</Books>
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.
REFERENCESFor additional information, click the article number below
to view the article in the Microsoft Knowledge Base:
288147 PRB: Using XPath to Query Against a User-Defined Default Namespace
Modification Type: | Minor | Last Reviewed: | 7/13/2004 |
---|
Keywords: | kbhowto KB294797 |
---|
|