priyas
priyas

Reputation: 415

Populate current date time using XSLT 2.0

I am trying to convert XML to another XML using XSLT 2.0. Below is the code that I am using.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:eof="http://style.rbsfm.com/EnrichODCFeed" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
            xmlns:fn="http://www.w3.org/2005/xpath-functions">
 <xsl:template match="/">
   <xsl:element name="tradeArrivalTime">
            <xsl:value-of  select="fn:current-date()"/>
        </xsl:element>
  </xsl:template>
 </xsl:stylesheet>

On Testing this XSL using Junit , I am getting below Error

(Location of error unknown)javax.xml.transform.TransformerException: Instance method
call to method current-date requires an Object instance as first argument

On Testing it using Eclipse XSLT complier , I am getting below Exception :

17:56:08,138 ERROR [main] JAXPSAXProcessorInvoker  - Cannot find external method      
'xpath-functions.currentDate' (must be public).
javax.xml.transform.TransformerConfigurationException: Cannot find external method     
'xpath-functions.currentDate' (must be public).

Please suggest . I am not able to figure out the mistake

Upvotes: 0

Views: 2121

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122364

That error suggests that the XSLT processor you're using does not support XSLT 2.0. The default javax.xml.transform processor is 1.0-only, if you want to use XSLT 2.0 then you need to use a 2.0-compatible processor such as Saxon.

Saxon supports the same javax.xml.transform APIs so the only change you need to make to your code in order to use Saxon (once you have added the relevant JAR file or dependency to your project) is to replace

TransformerFactory.newInstance()

with

new net.sf.saxon.TransformerFactoryImpl()

Note that a 1.0 processor will not necessarily complain if you pass it a stylesheet that says version="2.0". Instead it will use a "forwards-compatible" processing mode where xsl: elements and functions it does not know about will not be treated as compile-time errors (only as run-time errors in cases where they are actually called).

Upvotes: 2

Related Questions