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

priyas
  • 415
  • 1
  • 8
  • 29
  • Have a look here: http://stackoverflow.com/questions/1575111/can-an-xslt-insert-the-current-date – Kokkie Sep 01 '14 at 13:42

1 Answers1

2

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).

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
  • @priyas glad it helped. When you get an answer that solves your problem it's good manners to _accept_ the answer by clicking on the tick mark to the left. As well as rewarding the answerer (and you) with reputation points, this makes the question show up as answered in the summary lists and means other people don't waste time on it unnecessarily. – Ian Roberts Sep 02 '14 at 08:15