Wednesday, 10 July 2013

Current date and time in XLST under OSB

For my current assignment I needed to build quite complex transformations in OSB. Mainly because of my experiences I choose to do that in XSLT.
Eclipse has a quite nice XQuery mapper, but it lacks an XSLT mapper.
Since JDeveloper has a nice XSL Mapper tool, I incorporated a JDeveloper project in my OEPE OSB project.

Soon I found that the xpath2.0 functions of JDeveloper/SOASuite don't work under OSB. And I could not find how to use the XQuery functions in my Xslt, that do the same.

But I found a nice trick, mainly based on this forum post and this one. I adapted those examples a little, because of the wrong date formats and to get them working in my case.

First declare the following namspaces.
   xmlns:date="http://www.oracle.com/XSL/Transform/java/java.util.Date"
   xmlns:sdf= "http://www.oracle.com/XSL/Transform/java/java.text.SimpleDateFormat"
When examining those, it appears that "http://www.oracle.com/XSL/Transform/java/" tells the xsl-processor where to find java classes. And after that the path (package + class) to the actual java class can be appended. These prefixes should not get into the resulting xml, so you can add them to the exclude list:
     exclude-result-prefixes="... date sdf">
And then you can declare the next variables for the current date and the current time:
 
  <xsl:variable name="currentDate"><xsl:value-of select="sdf:format(sdf:new(&quot;yyyy-MM-dd&quot;),date:new())"></xsl:value-of></xsl:variable>
  <xsl:variable name="currentTime"><xsl:value-of select="sdf:format(sdf:new(&quot;hh:mm:ss&quot;),date:new())"></xsl:value-of></xsl:variable>
If you know java then you can figure out how that translates to a snippet of java code. And then you should be able to work out your way back. And doing so: you can program almost anything using java in XSLT...