Class XPathExpression
java.lang.Object
eu.maveniverse.domtrip.XPathExpression
Mini-XPath expression support for string-based element queries.
Compiles a subset of XPath expressions into efficient element queries
that can be evaluated against any element in a DomTrip document tree.
This provides a convenient string-based alternative to the programmatic
ElementQuery API.
Supported Expressions:
foo/bar/baz | Direct child path |
//foo | Descendant-or-self (search anywhere below) |
foo//bar | bar anywhere under foo children |
. | Current element |
.. | Parent element |
* | Any element (wildcard) |
foo[@attr] | Element with attribute present |
foo[@attr='value'] | Element with attribute equal to value |
foo[bar='text'] | Element with child text content |
foo[1] | First element (1-based) |
foo[last()] | Last element |
Usage Examples:
// Compile once, reuse many times
XPathExpression expr = XPathExpression.compile("//dependency[@scope='test']");
List<Element> results = expr.select(root);
Optional<Element> first = expr.selectFirst(root);
// Or use convenience methods on Element
List<Element> deps = element.select("dependencies/dependency");
Optional<Element> match = element.selectFirst("dependency[groupId='junit']");
// Or on Editor
List<Element> allDeps = editor.select("//dependency");
What is NOT Supported:
- Full axis specifiers (
preceding-sibling::,ancestor::) - XPath functions (
contains(),normalize-space()) - Boolean operators (
and,or) - Arithmetic operators
- Union operator (
|)
- Since:
- 1.3.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic XPathExpressionCompiles an XPath expression string into a reusableXPathExpression.Returns the original expression string.Evaluates this expression against the given context element and returns all matching elements.selectFirst(Element context) Evaluates this expression against the given context element and returns the first match.toString()
-
Method Details
-
compile
Compiles an XPath expression string into a reusableXPathExpression.The compiled expression can be evaluated multiple times against different context elements without re-parsing.
- Parameters:
expression- the XPath expression to compile- Returns:
- a compiled XPathExpression
- Throws:
DomTripException- if the expression is null, empty, or syntactically invalid
-
select
-
selectFirst
-
expression
-
toString
-