18

我该如何解决

Reference to undeclared namespace prefix: '%s'

微软的 msxml 实现有问题吗?


我正在使用来自政府网站的 XML 提要,其中包含我需要解析的值。xml 包含命名空间:

<?xml version="1.0" encoding="ISO-8859-1"?>
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns="http://purl.org/rss/1.0/"
    xmlns:cb="http://www.cbwiki.net/wiki/index.php/Specification_1.1"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:dcterms="http://purl.org/dc/terms/"
    xmlns:xsi="http://www.w3c.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.w3c.org/1999/02/22-rdf-syntax-ns#rdf.xsd">
    <item rdf:about="http://www.bankofcanada.ca/stats/rates_rss/STATIC_IEXE0101.xml">
        <cb:statistics>
            <cb:exchangeRate>
                <cb:value decimals="4">1.0351</cb:value>
                <cb:baseCurrency>CAD</cb:baseCurrency>
                <cb:targetCurrency>USD</cb:targetCurrency>
                <cb:rateType>Bank of Canada noon rate</cb:rateType>
                <cb:observationPeriod frequency="daily">2011-05-09T12:15:00-04:00</cb:observationPeriod>
            </cb:exchangeRate>
        </cb:statistics>
    </item>
</rdf:RDF>

运行 XPath 查询:

/rdf:RDF/item/cb:statistics/cb:exchangeRate/cb:targetCurrency

失败并出现错误:

Reference to undeclared namespace prefix: 'rdf'

编辑

如果我编辑原始 XML 以删除所有名称空间的使用:

<?xml version="1.0" encoding="ISO-8859-1"?>
<rdf>
    <item>
        <statistics>
            <exchangeRate>
                <value decimals="4">1.0351</value>
                <baseCurrency>CAD</baseCurrency>
                <targetCurrency>USD</targetCurrency>
                <rateType>Bank of Canada noon rate</rateType>
                <observationPeriod frequency="daily">2011-05-09T12:15:00-04:00</observationPeriod>
            </exchangeRate>
        </statistics>
    </item>
</rdf>

查询/rdf/item/statistics/exchangeRate/baseCurrency不会失败,并返回节点:

<baseCurrency>CAD</baseCurrency>

如何让 Microsoft XML 使用命名空间?


编辑 2

我尝试将SelectionNamespaces添加到 DOMDocument 对象:

doc.setProperty('SelectionNamespaces', 'xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cb="http://www.cbwiki.net/wiki/index.php/Specification_1.1"');

现在 xpath 查询没有失败,但它也没有返回节点:

nodes = doc.selectNodes('/rdf:RDF/item/cb:statistics/cb:exchangeRate/cb:targetCurrency');

也可以看看

4

3 回答 3

22

使用SelectionNamespaces是正确的方法,你只是缺少一个命名空间。

请注意,您的 XML 文档显式设置了默认命名空间,如下所示:

xmlns="http://purl.org/rss/1.0/"

这意味着任何没有前缀的元素,例如item元素,实际上都在默认命名空间中。因此,如果要使用 XPath 表达式选择该元素,则必须首先设置适当的选择名称空间。

为此,您可以将呼叫更改为setProperty

doc.setProperty('SelectionNamespaces', 'xmlns:rss="http://purl.org/rss/1.0/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cb="http://www.cbwiki.net/wiki/index.php/Specification_1.1"');

在这里,您已将文档中的默认名称空间分配给rss:XPath 表达式中的前缀。进行该更改后,以下 XPath 表达式应该可以正常工作:

nodes = doc.selectNodes('/rdf:RDF/rss:item/cb:statistics/cb:exchangeRate/cb:targetCurrency');

它之所以有效,是因为它item使用正确的命名空间引用了元素。XPath 表达式和原始文档之间的前缀不同这一事实并不重要。重要的是前缀绑定到的名称空间。

于 2011-05-11T08:50:39.247 回答
1
doc.setProperty('SelectionNamespaces', 'xmlns:rss="http://purl.org/rss/1.0/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cb="http://www.cbwiki.net/wiki/index.php/Specification_1.1"');

不要忘记将 xsd 文件或架构加载到 xmldoc 对象

是要走的路

我没有足够的声誉发表评论。但是那里的那一点为我节省了很多时间。

非常感谢

于 2014-11-19T22:34:28.023 回答
1

如果您正在使用XMLSerializer并看到此错误,则很可能您遇到了此处描述的 IE 错误:

https://stackoverflow.com/a/11399681

我花了很多时间才意识到这正在发生,所以我认为最好将这两个问题联系起来。

于 2015-06-08T15:41:48.667 回答