Nokogiri/Xpath 命名空间查询 [英] Nokogiri/Xpath namespace query

查看:71
本文介绍了Nokogiri/Xpath 命名空间查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 xpath 提取 dc:title 元素.我可以使用以下代码提取元数据.

I'm trying to pull out the dc:title element using an xpath. I can pull out the metadata using the following code.

doc = <<END
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://www.idpf.org/2007/opf" version="2.0">
  <metadata xmlns:dc="URI">
    <dc:title>title text</dc:title>
  </metadata>
</package>
END

doc = Nokogiri::XML(doc)

# Awesome this works!
puts '//xmlns:metadata'
puts doc.xpath('//xmlns:metadata')
# => <metadata xmlns:dc="URI"><dc:title>title text</dc:title></metadata>

如您所见,以上似乎工作正常.但是我似乎无法从这个节点树中获取标题信息,以下所有都失败了.

As you can see the above appears to work correctly. However I don't seem to be able to get the title information from this node tree, all of the below fail.

puts doc.xpath('//xmlns:metadata/title')
# => nil

puts doc.xpath('//xmlns:metadata/dc:title')
# => ERROR: `evaluate': Undefined namespace prefix

puts doc.xpath('//xmlns:dc:title')
# => ERROR: 'evaluate': Invalid expression: //xmlns:dc:title

有人可以用上面的 xml 文档解释如何在 xpath 中使用命名空间.

Could someone please explain how namespaces should be used in an xpath with the above xml doc.

推荐答案

解析时需要注册所有命名空间.Nokogiri 自动在根节点上注册命名空间.任何不在根节点上的命名空间都必须自己注册.这应该有效:

All namespaces need to be registered when parsing. Nokogiri automatically registers namespaces on the root node. Any namespaces that are not on the root node you have to register yourself. This should work:

puts doc.xpath('//dc:title', 'dc' => "URI")

或者,您可以完全删除命名空间.仅当您确定不会有冲突的节点名称时才执行此操作.

Alternately, you can remove namespaces altogether. Only do this if you are certain there will be no conflicting node names.

doc.remove_namespaces!
puts doc.xpath('//title')

这篇关于Nokogiri/Xpath 命名空间查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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