Class XPath

java.lang.Object
eu.maveniverse.domtrip.jaxen.XPath

public final class XPath extends Object
Static utility for evaluating full XPath 1.0 expressions against DomTrip documents.

This class provides convenient one-shot methods that wrap JaxenException into DomTripException for consistency with the core DomTrip API. For repeated evaluation of the same expression, use compile(String) to avoid re-parsing.

Quick Queries:


 // Find all test dependencies
 List<Element> testDeps = XPath.select(root, "//dependency[scope='test']");

 // Find first matching element
 Optional<Element> junit = XPath.selectFirst(root,
     "//dependency[contains(groupId, 'junit')]");
 

Compiled Expressions:


 // Compile once, evaluate many times
 DomTripXPath expr = XPath.compile("//dependency[scope='test']");
 List<Element> results1 = expr.selectElements(root1);
 List<Element> results2 = expr.selectElements(root2);
 

Advanced Queries (not available in mini-XPath):


 // Boolean operators
 XPath.select(root, "//dependency[scope='test' and groupId='junit']");

 // String functions
 XPath.select(root, "//dependency[contains(groupId, 'spring')]");
 XPath.select(root, "//dependency[starts-with(groupId, 'org.')]");

 // Negation
 XPath.select(root, "//dependency[not(@optional)]");

 // Union
 XPath.select(root, "//groupId | //artifactId");

 // Full axis navigation
 XPath.select(root, "//dependency/following-sibling::dependency");
 
Since:
1.3.0
See Also:
  • Method Details

    • select

      public static List<eu.maveniverse.domtrip.Element> select(Object context, String expression)
      Evaluates an XPath expression and returns matching elements.
      Parameters:
      context - the context node to evaluate against
      expression - the XPath 1.0 expression
      Returns:
      list of matching elements
      Throws:
      eu.maveniverse.domtrip.DomTripException - if the expression is invalid or evaluation fails
    • selectFirst

      public static Optional<eu.maveniverse.domtrip.Element> selectFirst(Object context, String expression)
      Evaluates an XPath expression and returns the first matching element.
      Parameters:
      context - the context node to evaluate against
      expression - the XPath 1.0 expression
      Returns:
      the first matching element, or empty if none match
      Throws:
      eu.maveniverse.domtrip.DomTripException - if the expression is invalid or evaluation fails
    • compile

      public static DomTripXPath compile(String expression)
      Compiles an XPath expression for repeated evaluation.

      Use this when the same expression will be evaluated against multiple context nodes to avoid re-parsing the expression each time.

      Parameters:
      expression - the XPath 1.0 expression to compile
      Returns:
      the compiled expression
      Throws:
      eu.maveniverse.domtrip.DomTripException - if the expression is syntactically invalid