• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

XSLT 2.o : to Use <matches> tag

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

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
System.setProperty("javax.xml.transform.TransformerFactory",
"net.sf.saxon.TransformerFactoryImpl");


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

myTransformer (foo_xml, foo_xsl);
}
catch (Exception ex) {
ex.printStackTrace();
}

}

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.
 
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

matches(/COD/CONTENT/SP_WORKFLOW/ORIGPUBDATE,"[0-9]{4}/[0-9]{2}/[0-9]{2}"))



[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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic