Crystal: XML xpath union operator broken

Created on 8 Nov 2019  路  5Comments  路  Source: crystal-lang/crystal

Following xpath will fail with an XML::Error although it's valid xpath:

require "xml"
doc = XML.parse("<foobar><foo></foo><bar></bar><baz></baz></foobar>")
doc.xpath_nodes("//(foo|bar|baz)")

Most helpful comment

The xpath pipe operator only combines full expressions, not partial expressions as in your example. You can use or with other selectors inside of [] to achieve what you want though. See https://stackoverflow.com/questions/13013696/in-xpath-is-it-possible-to-use-the-or-operator-with-node-names for an example

EDIT clarification: this applies to XPath 1.0, and libxml2 only supports 1.0, not 2.0

All 5 comments

It doesn't work either in Ruby's Nokogiri, which uses libxml2 under the hood too. Maybe it's a bug or a limitation of libxml2.

Using "//foo|//bar|//baz" works, though.

The xpath pipe operator only combines full expressions, not partial expressions as in your example. You can use or with other selectors inside of [] to achieve what you want though. See https://stackoverflow.com/questions/13013696/in-xpath-is-it-possible-to-use-the-or-operator-with-node-names for an example

EDIT clarification: this applies to XPath 1.0, and libxml2 only supports 1.0, not 2.0

See above, libxml2 only supports xpath 1.

Maybe we could add a line in the doc saying that, as it's not something I'd think to look

Was this page helpful?
0 / 5 - 0 ratings