Monday, January 05, 2009

Going crazy with XSL and Xalan

Recently I spent some time transforming one XML to another with an XSD and I was going crazy!

So let's start:

Passing parameters to the XSD

In the XSD write for instance <xsl:param name="fileName"></xsl:param> to access the parameter and in Java you do xmlTransformer.getTransformer().setParameter("fileName", "test"); You then can refer to the parameter with $filename

Calling Java methods from the XSD
  1. Write a static Java method which does what you want, e.g. public static String getFileMd5Hash(String path) {...}
  2. Declare the class containing those methods in the header of the XSD as a new namespace:
    xmlns:md5="xalan://com.mycompany.myclass"
    exclude-result-prefixes="md5 xalan">
  3. Call the method with <xsl:value-of select="md5:getFileIdMd5Hash($fileName)"> $filename is accessing the filename parameter declared in the previous method -- you can also have void or use the name of a node

Labels: , ,

0 Comments:

Post a Comment

<< Home