XPath select node with periods

ε祈祈猫儿з 提交于 2019-12-08 20:18:22

问题


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>

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.

I want to be able to make an XPath query to select the name under com.site.testing, but it keeps rejecting my queries.

Any ideas?

(I am using hpricot if it makes a difference)


回答1:


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.

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


来源:https://stackoverflow.com/questions/3443916/xpath-select-node-with-periods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!