XPath 选择带句点的节点 [英] XPath select node with periods
问题描述
我有一个 XML 文档,其中一些节点的名称中包含 .
:
I have an XML document where some of the nodes have a .
in their name:
<com.site.blah>
<id>asdkjasd</id>
<com.site.testing>
<name>test</name>
</com.site.testing>
</com.site.blah>
如果我尝试 @doc.search("/*/id").first.xpath
,它返回 /com.site.blah/id
,但如果然后我做: @doc.search("/com.site.blah/id").first.inspect
它返回 nil
.
If I try @doc.search("/*/id").first.xpath
, it returns /com.site.blah/id
, but if I then do: @doc.search("/com.site.blah/id").first.inspect
it returns nil
.
我希望能够进行 XPath 查询以选择 com.site.testing
下的名称,但它一直拒绝我的查询.
I want to be able to make an XPath query to select the name under com.site.testing
, but it keeps rejecting my queries.
有什么想法吗?
(如果有区别,我使用 hpricot)
(I am using hpricot if it makes a difference)
推荐答案
您的 XPath 库已损坏.XPath 名称测试是一个 QName (http://www.w3.org/TR/xpath/#NT-NameTest),在遵循 EBNF 的兔子洞之后,包含句点 (http://www.w3.org/TR/REC-xml/#NT-NameChar).向开发人员报告错误.
Your XPath library is broken. An XPath name test is a QName (http://www.w3.org/TR/xpath/#NT-NameTest), which, after following the rabbit hole of EBNF, includes periods (http://www.w3.org/TR/REC-xml/#NT-NameChar). Report the error to the developers.
作为一种解决方法,注释中提到的转义机制都不是 XPath 的一部分,但您可以尝试使用谓词来检查元素名称,如下所示:
As a workaround, neither of the escape mechanisms mentioned in the comments are part of XPath, but you can try using a predicate to check the element name, like so:
/*[name(.) = 'com.site.blah']/id
这篇关于XPath 选择带句点的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!