SelectionNamespaces Property

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - DOM Reference

SelectionNamespaces Property

Specifies namespaces for use in XPath expressions when it is necessary to define new namespaces externally. Namespaces are defined in the XML style, as a space-separated list of namespace declaration attributes. You can use this property to set the default namespace as well.

[JScript]

JScript Syntax

domObj.setProperty(strProp, strVal);
strVal= domObj.getProperty(strProp);
[Visual Basic]

Visual Basic Syntax

domObj.setProperty(strProp, strVal)
strVal= domObj.getProperty(strProp)
[C/C++]

C/C++ Syntax

HRESULT setProperty(BSTR strProp, VARIANT strVal);
HRESULT getProperty(BSTR strProp, VARIANT* strVal);

Value

strProp
A BSTR string whose value is "SelectionNamespaces".
strVal
A VARIANT string containing a space-separated list of namespace declaration attributes. The default value is an empty string ("").

Remarks

When an XML document contains elements defined in an external namespace, you must use this property to specify that namespace in order to use DOM methods such as selectNodes or selectSingleNode to navigate the document.

Example

The following JScript example shows how to use the SelectionNamespances property to specify namespaces in order to query elements that belong to different namespaces from an XML document.

XML File (example.xml)

The following example contains elements that belong to "a" (in bold) and "b" (in italic), in addition to elements that do not belong to any namespace.

<?xml version="1.0"?>
<root>
   <branch>branch</branch>
   <a:root xmlns:a="http://myserver.com">
      <a:branch>a-branch</a:branch>
      <b:branch xmlns:b="http://yourserver.com">
         b-branch
      </b:branch>
   </a:root>
</root>

JScript Code

Notice that the following code uses "na" and "nb" as the namespace aliases for the "http://myserver.com" and "http://yourserver.com" namespaces. The corresponding namespace aliases are "a" and "b" in the input document (example.xml).

var dom = new ActiveXObject("MSXML2.DOMDocument.5.0");
dom.async= false;
dom.ValidateOnParse = false;
dom.load("example.xml");
if (dom.parseError.errorCode!=0) 
{
    alert("can't load dom" + dom.parseError.reason);
       exit;
}

ns = "xmlns:na='http://myserver.com' xmlns:nb='http://yourserver.com'";

alert("ns:(before setProperty())\n  "+dom.getProperty("SelectionNamespaces"));
dom.setProperty("SelectionNamespaces", ns);
alert("ns:(after setProperty())\n  "+dom.getProperty("SelectionNamespaces"));

node = dom.selectSingleNode("//root");
alert("root: \n"+node.xml);

node = dom.selectSingleNode("//na:root");
alert("a:root: \n"+node.xml);

node = dom.selectSingleNode("//branch");
alert("branch: \n"+node.xml);

node = dom.selectSingleNode("//na:branch");
alert("a:branch: \n"+node.xml);

node = dom.selectSingleNode("//nb:branch");
alert("b:branch: \n"+node.xml);

function alert(str)
{
  WScript.echo(str+"\n");
}

Try It!

  1. Copy example.xml and example.js onto your local drive.
  2. Open a command prompt and navigate to the directory where example.xml and example.js are located.
  3. Type the following command from the command prompt:
    Cscript example.js

You should get the following output.

Output

ns:(before setProperty())


ns:(after setProperty())
  xmlns:na='http://myserver.com' xmlns:nb='http://yourserver.com'

root:
<root>
        <branch>branch</branch>
        <a:root xmlns:a="http://myserver.com">
                <a:branch>a-branch</a:branch>
                <b:branch xmlns:b="http://yourserver.com">
                    b-branch
                </b:branch>
        </a:root>
</root>

a:root:
<a:root xmlns:a="http://myserver.com">
        <a:branch>a-branch</a:branch>
        <b:branch xmlns:b="http://yourserver.com">
         b-branch
      </b:branch>
</a:root>

branch:
<branch>branch</branch>

a:branch:
<a:branch xmlns:a="http://myserver.com">a-branch</a:branch>

b:branch:
      <b:branch xmlns:b="http://yourserver.com">
         b-branch
      </b:branch>

Applies To

Component: MSXML 3.0 and later

Interface: IXMLDOMDocument2

Method: setProperty | getProperty