XPath select node with periods
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 u开发者_如何学Cnder com.site.testing
, but it keeps rejecting my queries.
Any ideas?
(I am using hpricot if it makes a difference)
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
精彩评论