使用 XPath 解析包含冒号的元素名称 [英] Use XPath to parse element name containing a colon

查看:97
本文介绍了使用 XPath 解析包含冒号的元素名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图用 XPath 解析一些 WootAPI.我遇到的一个问题是,一些元素的名称中有冒号,例如 woot:pricewoot:condition.现在,由于冒号,尝试使用 XPath //rss/channel/item/woot:price 不会获取元素 woot:price 的内容,我认为.我该怎么做才能得到它?

解决方案

冒号是因为元素具有命名空间前缀并且绑定到 Woot 命名空间.

您应该阅读XML 命名空间以及它们如何影响 XPATH 和 XSLT.>

如果您想在 XPATH 中引用 Woot 元素,您需要:

  1. 声明 Woot 命名空间 http://www.woot.com/ 以便当您在 XPATH 中使用该命名空间前缀时会被理解.
  2. 使用更通用的 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:

  1. Declare the Woot namespace http://www.woot.com/ so that when you use that namespace prefix in your XPATH it will be understood.
  2. Use a more generic XPATH statement that uses predicate filters that use local-name() and namespace-uri() to match the element.
    • //rss/channel/item/*[local-name()='price' and namespace-uri()='http://www.woot.com/']

这篇关于使用 XPath 解析包含冒号的元素名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆