• Post Reply Bookmark Topic Watch Topic
  • New Topic

XSLT 2.o : to Use <matches> tag  RSS feed

Deepak Nambiar
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I want to use matches function which comes in XSLT 2.0 . I used like below :
<xsl:when test='not(matches(/COD/CONTENT/SP_WORKFLOW/ORIGPUBDATE,"[0-9]{4}/[0-9]{2}/[0-9]{2}"))'>

its comparing a date format to regex.

I wrote on Java XSLT transformation code for this using SAXON 9

public static void main(String args[]) {

// set the TransformFactory to use the Saxon TransformerFactoryImpl method

String foo_xml = "inputxml_xml"; //input xml
String foo_xsl = "input_xsl"; //input xsl
try {

myTransformer (foo_xml, foo_xsl);
catch (Exception ex) {


public static void myTransformer (String sourceID, String xslID)
throws TransformerException, TransformerConfigurationException, UnsupportedEncodingException, FileNotFoundException {

InputStreamReader reader = new InputStreamReader(
new FileInputStream("input_xml"), "UTF-8");
Source xmlSource = new StreamSource(reader);
System.out.println("reader encoding == " + reader.getEncoding());

Source xsltSource = new StreamSource(new InputStreamReader(
new FileInputStream("input_xsl"), "UTF-8"));
// Create a transform factory instance.
TransformerFactory tfactory = TransformerFactoryImpl.newInstance();
OutputStreamWriter writer = new OutputStreamWriter(
new FileOutputStream("output_xml"), "UTF-8");

Result result = new StreamResult(writer);
// Create a transformer for the stylesheet.
Transformer transformer = tfactory.newTransformer(xsltSource);
// Transform the source XML to System.out.
transformer.transform(xmlSource, result);

But the functionality of matches is not working...

Please help me in this matter.
g tsuji
Ranch Hand
Posts: 697
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[0] The path is an absolute path, hence, the return (sequence) is independent of the context node within the xslt. Make sure that's what is original intention.

[1] The line as such would be a correct line if and only if the path returns either an empty sequence or a sequence with one item only, otherwise, it would be a runtime error. If it is empty sequence, the match will return false and it is not an error.

[2] Since it is an absolute path, it is not clear which node, if any, would be targetted to be tested. Suppose it is intended to test only the first entry, if any, the proper line would look like this.

[3] If the pattern matches any "substring" within the text, the function would return true, otherwise, false.

ps: Just a side note: the in-params have not been put into good use in myTransformer. I suppose it is work-in-progress.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!