0

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.

Matthias
  • 263
  • 4
  • 11
  • Are you just looking for something that parses XPath expressions, or for something capable of evaluating them as well? And which XPath version? Parsing is the easy part of the job. – Michael Kay Mar 31 '23 at 22:19
  • Just the parsing. We use PetitParser to create our own simple programming language that our customer can use to formulate rules that are evaluated in another step. Xpaths are part of the language, however if some features of the XPath specification are missing in the parser definition we will find workarounds if we need to. – Matthias Apr 01 '23 at 06:07
  • My answer to the question is phrased is that I don't know if anybody knows. Sorry. But from a quick look at PetitParser (I hadn't come across it before) I see no obvious reason why the task should be "more or less impossible". There's a grammar for XPath 1.0 in the W3C specification and it's not that big. There are some lexical complications involving reserved words etc but your first attempt could gloss over those. – Michael Kay Apr 01 '23 at 07:59

1 Answers1

1

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.

Lukas Renggli
  • 8,754
  • 23
  • 46