Search code examples
javaxpathparameterization

Is it possible parameterize a compiled Java XPath expression ala PreparedStatement style semantics?


Is it possible either with the stock JAX-P xpath expression engine or another compliant engine to compile an xpath expression that would allow for parametrization?

I'm looking to see if is if there is an API that would allow a developer to set placeholders within a compiled xpath and replace those values at run time.

Any insight on this as to whether or not it is possible and if there are caveats, pitfalls, or just plain "don't do that" type advice would be greatly appreciated.

(Note I corrected a "crossing of the streams" ... was having a conversation with a coworker with regard to xpath & regular expressions ... just happened to get mentally tongue tied ... sorry for the confusion)


Solution

  • I'm not quite sure where "regular expression" fits into your question.

    The JAXP API allows an XPath expression to contain variables (for example //emp[ssn=$e]), and the values of the variables are obtained at run-time by a call to the VariableResolver that you supply as part of the API. The JAXP spec is rather loose about what kind of values are acceptable and this may vary from one implementation to another.

    The Saxon XPath API (s9api) takes this further and allows you to explicitly declare variables and their types at the time the expression is compiled, and then to supply values for the variables (of any XPath 2.0 type) when the expression is executed.