我正在使用 Nokogiri 来解析一些类似于以下内容的 XML:
<item name="item one">
<other name="other name"/>
<third />
</item>
<item name="item two">
<other />
<third />
</item>
我正在使用 Nokogiri 解析这些项目,如下所示:
xmldoc.xpath("//item").each do |node|
node.xpath("//*[@name]") # Gives me *all* elements in the doc
node.xpath(".//*[@name]") # Gives me child elements of the item
end
我可以用什么表达来得到[item, other]
当我迭代时从第一个节点开始?
The .
在开头将使 XPath 表达式特定于上下文,使用@
来获取属性,@*
获取所有属性(通配符):
node.xpath(".//*/@*")
node.xpath("@*")
并且,获取当前节点的所有属性以及所有子节点的所有属性:
node.xpath(".//@*")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)