解析 MSXML 时引用未声明的命名空间前缀
如何解决
Reference to undeclared namespace prefix: '%s'
Microsoft 的 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>
How do i get Microsoft XML to work with namespaces?
编辑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');
另请参阅
How do I solve the
Reference to undeclared namespace prefix: '%s'
problem with Microsoft's msxml implementation?
I'm using an XML feed from a government web-site that contains values i need to parse. The xml contains namespaces:
<?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>
Running the XPath query:
/rdf:RDF/item/cb:statistics/cb:exchangeRate/cb:targetCurrency
fails with the error:
Reference to undeclared namespace prefix: 'rdf'
Edit:
If i edit the original XML to remove all use of namespaces:
<?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>
The query /rdf/item/statistics/exchangeRate/baseCurrency
doesn't fail, and returns nodes:
<baseCurrency>CAD</baseCurrency>
How do i get Microsoft XML to work with namespaces?
Edit 2
i've tried adding SelectionNamespaces to the DOMDocument object:
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"');
Now the xpath query doesn't fail, but it also returns no nodes:
nodes = doc.selectNodes('/rdf:RDF/item/cb:statistics/cb:exchangeRate/cb:targetCurrency');
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
SelectionNamespaces
是正确的方法,您只是缺少一个命名空间。请注意,您的 XML 文档显式设置默认命名空间,如下所示:
这意味着任何没有前缀的元素(例如
item
元素)实际上位于默认命名空间中。因此,如果您想使用 XPath 表达式选择该元素,则必须首先设置适当的选择命名空间。为此,您可以更改对
setProperty
的调用,如下所示:在这里,您已将文档中的默认命名空间分配给 XPath 表达式中的
rss:
前缀。完成此更改后,以下 XPath 表达式应该可以正常工作:它可以工作,因为它使用正确的命名空间引用
item
元素。 XPath 表达式和原始文档之间的前缀不同这一事实并不重要。重要的是前缀绑定到的名称空间。Using
SelectionNamespaces
is the correct approach, you are just missing a namespace.Notice that your XML document explicitly sets the default namespace as follows:
This means that any element without a prefix, such as the
item
element, is actually in the default namespace. So if you want to select that element with an XPath expression, you must first set an appropriate selection namespace.To do this, you can change your call to
setProperty
like so:Here you've assigned the default namespace from the document to the
rss:
prefix in your XPath expression. With that change in place, the following XPath expression should work correctly:It works because it references the
item
element using the correct namespace. The fact that the prefix differs between the XPath expression and the original document is immaterial. It is the namespace which the prefix is bound to that matters.不要忘记将 xsd 文件或架构加载到 xmldoc 对象,
的方法
这是我没有足够声誉来发表评论 。但那一点节省了我很多时间。
太感谢了
Dont forget to load the xsd file or schema to the xmldoc object
is the way to go
I dont have enough reputation to comment. But that bit there saved me a lot of time.
Thank you so much
如果您使用
XMLSerializer
并看到此错误,则您可能遇到了此处描述的 IE 错误:https ://stackoverflow.com/a/11399681
我花了很多时间才意识到发生了这种情况,所以我认为最好将这两个问题联系起来。
If you are using
XMLSerializer
and see this error, it is likely that you are running into the IE bug described here:https://stackoverflow.com/a/11399681
It took me a lot of time to realize that this was happening, so I thought it best to link these two issues.