Class XPath


  • public final class XPath
    extends java.lang.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:
    DomTripXPath
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static DomTripXPath compile​(java.lang.String expression)
      Compiles an XPath expression for repeated evaluation.
      static java.util.List<eu.maveniverse.domtrip.Element> select​(java.lang.Object context, java.lang.String expression)
      Evaluates an XPath expression and returns matching elements.
      static java.util.Optional<eu.maveniverse.domtrip.Element> selectFirst​(java.lang.Object context, java.lang.String expression)
      Evaluates an XPath expression and returns the first matching element.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • select

        public static java.util.List<eu.maveniverse.domtrip.Element> select​(java.lang.Object context,
                                                                            java.lang.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 java.util.Optional<eu.maveniverse.domtrip.Element> selectFirst​(java.lang.Object context,
                                                                                     java.lang.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​(java.lang.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