Does anyone know of a neat way in java or any packages that will allow me to check if a Node in a DOM will match on a template in an xsl stylesheet.
For example;
<xsl:template match="elem/child/item">
...
</xsl:template>
Would be a template I’m looking for a match for using something like;
Node n = getNode();
String pattern = "elem/child/item"
boolean match = PatternMatcher.isMatch(n, pattern);
Of course where the pattern used could be any sort of XPath expression that could be used as a template match in an xsl stylesheet.
I would greatly appreciate it if anyone knows a nice way this could be acheived through packages etc. Thanks
Saxon’s XPathCompiler object:
http://www.saxonica.com/documentation/javadoc/net/sf/saxon/s9api/XPathCompiler.html
has a method compilePattern() that allows you to compile an XSLT Pattern. This is returned in the form of an XPathExecutable, which can be evaluated by (a) supplying the target node as the context node for evaluation, and (b) evaluating the expression to return a boolean which is true if the pattern matches the node or false otherwise.