【发布时间】:2015-09-18 12:15:25
【问题描述】:
我正在使用 Crystal,并试图在 XML 文档中检索节点的 ID:
<foo ID="bar"></foo>
我正在使用以下代码获取 ID
require "xml"
file = File.read("path/to/doc.xml")
xml = XML.parse(file)
xpath_context = XML::XPathContext.new(xml)
nodeset = xpath_context.evaluate("//foo/@ID")
如果我检查节点集,我会得到我期望的内容:
[#<XML::Attribute:0x1287690 name="ID" value="bar">]
而nodeset.class 返回具有an instance method [] 的XML::NodeSet。所以我相信我应该能够做到这一点来获得价值:
node = nodeset[0]
node.value
但是,当我调用 nodeset[0] 时,我收到以下错误:
undefined method '[]' for Float64 (compile-time type is (String | Float64 | Bool | XML::NodeSet))
node = nodeset[0]
当inspect 和class 都将其视为XML::Nodeset 时,我不明白为什么[] 方法将nodeset 视为Float64。
我错过了什么?
String有[]方法,而Float64没有,是不是巧合?
【问题讨论】:
标签: xml xpath crystal-lang