1

Fetch XML Nodes using selectSingleNode in MSXML

I am trying to get the title field in below XML, but not able to

The XML is

<?xml version="1.0"?>
<rg:Group xmlns:rg="urn:mpeg:mpeg21:2003:01-REL-R-NS">
<r:license xmlns:r="urn:mpeg:mpeg21:2003:01-REL-R-NS" Id="{b11f85f6-59c1-4c17-9c22-d951ac7257}"><r:title>XrML 2.1 License</r:title>
(some fields)
</r:license>
</rg:Group>

Below is my code in XML

var objXML = new ActiveXObject("MSXML2.DOMDocument.6.0");
objXML.async = false;
objXML.load("\\license.xml");
var ns="xmlns:rg='urn:mpeg:mpeg21:2003:01-REL-R-NS' +"xmlns:r='urn:mpeg:mpeg21:2003:01-REL-R-NS'"
objXML.setProperty("SelectionNamespaces", ns);
objXML.setProperty("SelectionLanguage", "XPath");
WScript.Echo("ns:(after setProperty())\n  "+objXML.getProperty("SelectionNamespaces"));

var node = objXML.selectSingleNode("//Group/license/title");
WScript.Echo("root: \n"+node.text); //returns null
4

1 回答 1

0

Xpath 表达式必须是(否则命名空间的注册是没有意义的):

//r:Group/r:license/r:title

此外,您只需要注册一个命名空间作为前缀"rg",并且"r"恰好绑定到同一个命名空间。

您注册的前缀不需要与 XML 文档中使用的任何前缀相同——可以是"x",然后表达式将是:

//x:Group/x:license/x:title
于 2012-08-08T13:34:45.463 回答