javaxpathpetitparser

Parse XPath with PetitParser in Java


Does anybody know if there is an existing implementation of an XPath parser in Java that uses PetitParser? I assume it is more or less impossible to cover all thinkable XPath expressions that are understood by e.g. javax.xml.xpath.XPath but maybe there is an implementation that covers some XPath expressions like

//div[@id='123']//span or //ul/li[last()]/../span or /bookstore/book[position()>=2 and position() <= (last() - 1)]

to give some examples.


Solution

  • The XML package in Dart comes with a simple XPath implementation that was written using PetitParser. It should be relatively easy to convert that grammar (and evaluator) to the Java version of PetitParser.

    Note that the grammar does not support the full standard (yet), but the most commonly used parts are there. There is also an online demo, where you can play with the functionality.