Match pattern vs XPath

David Lee
  • David Lee
    David Lee

    Is there any API in Saxon that makes a Match Pattern into an XPath ?

    I'm using the XPath classes from s9API and find I need to handle "Match Patterns" instead.
    Is there some kind of regex or api that I can take a match pattern and convert it into an xpath to use with the saxon API ?

    Example: (trivial case)

    Match Pattern = "foo"
    Xpath expression = "//foo"

    but its not as simple as adding "//" to the pattern because the match pattern CAN start with "/" and look like an xpath expression.

    The other thought I had was to iterate the nodes one by one cand call the XPath evaluator on each node in turn as the context ... but I think that may be exponentially more inefficient then trying to convert a match pattern to an xpath expression and calling the evaluator once.

    • Michael Kay
      Michael Kay

      The s9api XPathCompiler class in 9.1 introduced a method compilerPattern() which takes an XSLT pattern as input and returns an equivalent XPath expression. The equivalence isn't quite as you had it in mind however - rather than returning an expression that selects all the nodes that match, it returns an expression that evaluates to true() if the context node matches, false() it it doesn't.

      So you can use this by iterating over all the nodes in the document (use XdmNode.axisIterator(Axis.DESCENDANT_OR_SELF)) and testing each one in turn against the pattern.