问题描述
所以我试图用 XPath 解析一些 WootAPI.我遇到的一个问题是,一些元素的名称中有冒号,例如 woot:price
或 woot:condition
.现在,由于冒号,尝试使用 XPath //rss/channel/item/woot:price
不会获取元素 woot:price
的内容,我认为.我该怎么做才能得到它?
冒号是因为元素具有命名空间前缀并且绑定到 Woot 命名空间.
您应该阅读XML 命名空间以及它们如何影响 XPATH 和 XSLT.>
如果您想在 XPATH 中引用 Woot 元素,您需要:
- 声明 Woot 命名空间
http://www.woot.com/
以便当您在 XPATH 中使用该命名空间前缀时会被理解. - 使用更通用的 XPATH 语句,该语句使用使用
local-name()
和namespace-uri()
的谓词过滤器来匹配元素.//rss/channel/item/*[local-name()='price' and namespace-uri()='http://www.woot.com/']
So I'm trying to parse some of the WootAPI with XPath. A problem that I'm running into is that a couple of the elements have colons in their name, such as woot:price
or woot:condition
. Now, trying to use the XPath //rss/channel/item/woot:price
won't grab the contents of the element woot:price
, because of the colon, I think. What can I do to get it anyway?
The colons are because the elements have a namespace prefix and are bound to the Woot namespace.
You should read up on XML namespaces and how they affect XPATH and XSLT.
If you want to reference the Woot elements in your XPATH you will either need to:
- Declare the Woot namespace
http://www.woot.com/
so that when you use that namespace prefix in your XPATH it will be understood. - Use a more generic XPATH statement that uses predicate filters that use
local-name()
andnamespace-uri()
to match the element.//rss/channel/item/*[local-name()='price' and namespace-uri()='http://www.woot.com/']
这篇关于使用 XPath 解析包含冒号的元素名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!