Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 under com.site.testing, but it keeps rejecting my queries.

Any ideas?

(I am using hpricot if it makes a difference)

like image 455
Mitch Dempsey Avatar asked Aug 09 '10 20:08

Mitch Dempsey


1 Answers

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
like image 100
Shaun McCance Avatar answered Sep 29 '22 22:09

Shaun McCance