- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在使用 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
返回 XML::NodeSet
其中有 an instance method []
.所以我相信我应该能够这样做以获得值(value):
node = nodeset[0]
node.value
但是,当我调用 nodeset[0]
我收到以下错误:
undefined method '[]' for Float64 (compile-time type is (String | Float64 | Bool | XML::NodeSet))
node = nodeset[0]
我不明白为什么 []
方法将节点集视为 Float64,同时 inspect
和 class
将其视为 XML::Nodeset
.
我错过了什么?
String 有一个 []
是巧合吗?方法,但 Float64 没有?
最佳答案
当您执行evaluate
时,返回类型是所有可能值的联合类型。在这种情况下,XML::NodeSet
是运行时类型(注意与编译时类型的区别)。
如果你能保证返回类型总是一个节点集,那么你可以简单地做:
nodeset = xpath_context.evaluate("//foo/@ID") as XML::NodeSet
但如果结果具有不同的类型,则会引发异常。另一种选择是有条件地进行:
if nodeset.is_a?(XML::NodeSet)
# use nodeset here without casting, the compiler will restrict the type
end
或者甚至使用 case
语句:
case nodeset
when XML::NodeSet
# ...
end
关于xml - 使用 Crystal 从 XML::Nodeset 中的第一个节点检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32651833/
我发现了一个 xsl:key 似乎不起作用的情况。 我正在将 XSLT 1 与 Xalan(已编译)一起使用,这就是正在发生的事情: 1.- 这有效:名为 的 key 测试 1 工作正常:
我正在尝试在 Java 中创建 XPath 表达式(8,默认 XPath 实现)。我正在执行以下操作: Object res = xpath.evaluate("(//*[local-name()='
我正在转换一个 XML 文档。有一个属性@prettydate这是一个类似于“Friday, May 7, 2010”的字符串。我想拆分该字符串并添加指向月份和年份的链接。我正在使用 exslt:st
我一直在阅读关于在文档中选择唯一节点(使用 Muenchian 方法)的不同问题,但在我的情况下我不能使用键(或者我不知道如何使用键),因为我正在处理节点集并且不在文件上。 并且不能在节点集上设置键。
我解析一个非常大的 xml 文件(来自 jpylyzer,一个 jp2 属性提取器)。此 xml 包含许多 JP2 图像的属性,每个图像都具有相同的元素,例如: //results/jpylyzer/
我在 Nokogiri 中使用 XPath 解析从 Google 联系人 API 返回的 XML。 def getcontact doc = Nokogiri::XML.parse(open(ur
我正在使用 Nokogiri 的 xpath 方法寻找我的 input 元素。它返回类 Nokogiri::XML::NodeSet 的对象: [#, #, #, #, #]> 有没有比使用 to_s
我正在尝试在 Memcache 中缓存 Weatherman 的响应 ( https://github.com/dlt/yahoo_weatherman ) 以避免多次获取天气,我正在这样做: w
我拉了一个Nokogiri::XML::NodeSet来自页面,结果如下: 我无法弄清楚如何转换它(对人类来说很明显)标记到 Mechanize/Nokogiri 解析的对象中,这样我就
我正在使用 Crystal,并试图检索 XML 文档中节点的 ID: 我正在使用以下代码获取 ID require "xml" file = File.read("path/to/doc.xml")
我正在使用 R 中的 xml2 包解析一些非常大的 xml 文件。read_xml() 成功加载了大文件,但是当我尝试使用 xml_find_all() 时,我收到“错误:内存分配失败:增长的节点集命
我正在使用 R 中的 xml2 包解析一些非常大的 xml 文件。read_xml() 成功加载了大文件,但是当我尝试使用 xml_find_all() 时,我收到“错误:内存分配失败:增长的节点集命
我试图为 xml 文件中的特定元素设置一个属性,并且我成功地使用了 doc.css('Object').attr("Id").value = timestamp 这很好,直到“对象”不存在导致程序异常
我想按标题值对 Nokigiri 节点集进行排序。尝试了以下代码,但排序不起作用:/有什么建议吗? doc.xpath("//item").sort{|x,y| x.xpath('foo:attr[@
我想从 Nokogiri NodeSet 中获取未转义的内部 html。有谁知道如何做到这一点? 最佳答案 有什么不妥吗? nodeset.inner_html 关于ruby - 如何获取 ruby
我有一个 XmlNodeList,我需要将其设置为一种格式,然后我可以通过从 C# 扩展方法调用它来在 XSLT 样式表中重复使用它。 有人可以帮忙吗?我读到它可能与使用 XPathNavigator
我开始在 w3cschool 学习 xpath。我正在尝试评估游戏中的玩家数量。 (因此计算屏幕名称和 text() 元素/属性) 这是我的示例 xml: My Game
我有一个像这样的颜色元素序列: 还有一个单词序列: 1px Blue Solid 找到 Colors/Color/@name 属性与 Wo
当试图模拟 Faraday::Response对象,我试过: mock(Faraday::Response, :env => {:body => "...some xml..."}) ...但是当我调
我的代码在 Jetty 中工作正常,但一旦在 WebSphere 中部署,我就会收到 java.lang.VerifyError 异常,根本没有任何额外信息(甚至没有消息)。 代码: Class c
我是一名优秀的程序员,十分优秀!