使用 libxml-ruby 解析命名空间 XML
我正在尝试使用 libxml-ruby 解析以下格式的 XML(来自欧洲中央银行数据源):
<?xml version="1.0" encoding="UTF-8"?>
<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01"
xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
<gesmes:subject>Reference rates</gesmes:subject>
<gesmes:Sender>
<gesmes:name>European Central Bank</gesmes:name>
</gesmes:Sender>
<Cube>
<Cube time="2009-11-03">
<Cube currency="USD" rate="1.4658"/>
<Cube currency="JPY" rate="132.25"/>
<Cube currency="BGN" rate="1.9558"/>
</Cube>
</Cube>
</gesmes:Envelope>
我正在按如下方式加载文档:
require 'rubygems'
require 'xml/libxml'
doc = XML::Document.file('eurofxref-hist.xml')
但我正在努力想出正确的命名空间配置以允许 XPATH对数据的查询。
我可以使用以下代码提取所有 Cube 节点:
doc.find("//*[local-name()='Cube']")
但是考虑到父节点和子节点都称为 Cube 这实际上并不能帮助我迭代父节点。也许我可以修改此 XPATH 以仅查找带有 time
参数的节点?
我的目标是能够提取具有 time
属性的所有 Cube
节点(即
),这样我就可以提取日期并迭代子 Cube
节点中的汇率。
有人可以帮忙吗?
I'm attempting to parse XML in the following format (from the European Central Bank data feed) using libxml-ruby:
<?xml version="1.0" encoding="UTF-8"?>
<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01"
xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
<gesmes:subject>Reference rates</gesmes:subject>
<gesmes:Sender>
<gesmes:name>European Central Bank</gesmes:name>
</gesmes:Sender>
<Cube>
<Cube time="2009-11-03">
<Cube currency="USD" rate="1.4658"/>
<Cube currency="JPY" rate="132.25"/>
<Cube currency="BGN" rate="1.9558"/>
</Cube>
</Cube>
</gesmes:Envelope>
I'm loading the document as follows:
require 'rubygems'
require 'xml/libxml'
doc = XML::Document.file('eurofxref-hist.xml')
But I'm struggling to come up with the correct namespace configuration to allow XPATH queries on the data.
I can extract all the Cube
nodes using the following code:
doc.find("//*[local-name()='Cube']")
But given that both the parent node and child nodes are both called Cube
this really doesn't help me iterate over just the parent nodes. Perhaps I could modify this XPATH to only find those nodes with a time
parameter?
My aim is to be able to extract all the Cube
nodes which have a time
attribute (i.e. <Cube time="2009-11-03">
) so I can then extract the date and iterate over the exchange rates in the child Cube
nodes.
Can anyone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
其中任何一个都可以工作:
好的,这已经过测试并且可以工作
either of these will work:
Ok, this is tested and working
所以我想通了。根节点定义了两个命名空间,一个有前缀,一个没有:
定义前缀后,您可以很容易地引用前缀命名空间名称。使用原始问题中的 XML,此 XPATH:
将返回“参考率”。
因为 Cube 节点没有前缀,所以我们首先需要为全局命名空间定义一个命名空间前缀。这就是我实现这一目标的方法:
一旦定义了这一点,找到具有时间属性的多维数据集节点就很简单了:
So I figured this out. The root node defines two namespaces, one with a prefix, one without:
When a prefix is defined, you can quite easily reference the prefix namespaced names. Using the XML from the original question, this XPATH:
Will return "Reference rates".
Because the
Cube
nodes are not prefixed, we first need to define a namespace prefix for the global namespace. This is how I achieved this:Once this is defined, finding the Cube nodes with time attributes is trivial: