我尝试通过特殊属性从 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/