xml-namespaces - 解析 MSXML 时引用未声明的命名空间前缀

标签 xml-namespaces msxml msxml6

我该如何解决

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 以删除所有 namespace 的使用:

<?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

我试过添加选择命名空间 到 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');

也可以看看
  • “undeclared reference to namespace prefix ” error
  • XMLReader - How to handle undeclared namespace
  • PRB: Specifying Fully Qualified Element Names in XPath Queries
  • XPath not working properly.
  • 最佳答案

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

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

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

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

    为此,您可以将调用更改为 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 表达式和原始文档之间的前缀不同这一事实并不重要。重要的是前缀绑定(bind)到的 namespace 。

    关于xml-namespaces - 解析 MSXML 时引用未声明的命名空间前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5951628/

    相关文章:

    html - 没有互联网连接,xmlns 属性如何工作并被浏览器理解?

    xml - 如何使用 Excel VBA 导入 XML 数据?

    javascript - 如何让 IE9 MSXML6 执行包含 javascript 的集成 XSLT 处理?

    xml - MSXML 替代品

    xml - 无法获取 xsl :import or xsl:include to work on MSXML6

    xml - MSXML2.DOMDocument60 使 Excel 崩溃

    java - 如何在 Java 中删除 XML 命名空间

    php - 无法用冒号解析 xml 数据 (:) from response using getNamespaces()

    xml - XML 模式中子元素的命名空间前缀

    delphi - 如何使用delphi访问msxml6中complextype中的粒子?