ruby - REXML 无法从属性中进行选择。错误或不正确的 XPath?

标签 ruby xpath rexml

我尝试通过特殊属性从 SVG 文档中选择一个元素。 我建立了一个简单的例子。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg">
  <g id='1'>
    <path id='2' type='A'/>
    <rect id='3' type='B'/>
  </g>
</svg>

现在我使用以下语法通过其属性“type”检索路径元素:

require 'rexml/document'
include REXML
xmlfile = File.new "xml_as_specified_above.svg"
xmldoc = Document.new(xmlfile)
XPath.match( xmldoc.root, "//path[@type]" )

语法直接来自http://www.w3schools.com/xpath/xpath_syntax.asp . 我希望此表达式选择路径元素,但如下所示:

>> XPath.match( xmldoc.root, "//path[@type]" )
=> []

那么,在 XPath 中通过属性寻址路径元素的正确语法是什么? 还是 REXML(使用 3.1.7.3)中存在错误? 还检索“rect”元素的加分。

最佳答案

看起来正在使用不支持完整 XPath 规范的旧版本的 rexml。

尝试检查 puts XPath::VERSION 的输出以确保显示 3.1.73。

关于ruby - REXML 无法从属性中进行选择。错误或不正确的 XPath?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4898140/

相关文章:

ruby - Ruby 中我的 XML 之前的无效字符

xml - ruby 单元测试 : Is this a Valid (well-formed) XML Doc?

ruby - watir-webdriver:如何从我在其中找到子字符串的 HTML 中检索整行?

ruby-on-rails - Ruby 排序依据(整数) "comparison of NilClass with 3200 failed"

Ruby 模块给定参数调用一个方法?

XPath 3.0 递归查询

ruby-on-rails - REXML::Document.new 无法使用 US-ASCII 编码的 XML 进行解析

ruby - 如何从我使用的函数中引用对象

xml - 解析Soap响应-XML/XPATH(在soapUI中)(SoapUI响应中的命名空间)

sql - 我有一个 XML 脚本,如何在 Xpath 中编写查询?