5 Replies Latest reply: Jun 5, 2012 4:03 PM by odie_63 RSS

    XSL transformation issue

    nk_arw
      Hi,

      I have written a small Java class which transforms a XML data file using a XSLT file and this going to be a standalone application.

      In XSL I am trying to convert a date string to Year. Example if value of TRX_DATE node in XML file is "2012-05-29T00:00:00.000+00:00", then it should be transformed to "2012". To achieve this I am using Xpath.

      *<?xml version="1.0" encoding="UTF-8" ?>*
      *<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"*
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:fn="http://www.w3.org/2005/xpath-functions">
      *:*
      *<xsl:value-of select="fn:year-from-dateTime(TRX_DATE)"/>*
      *:*
      *</xsl:stylesheet>*

      Without "<xsl:value-of select="fn:year-from-dateTime(TRX_DATE)"/>" is working fine.

      But with "<xsl:value-of select="fn:year-from-dateTime(TRX_DATE)"/>", Java class execution throws error

      ERROR:  'The first argument to the non-static Java function 'yearFromDateTime' is not a valid object reference.'
      FATAL ERROR:  'Could not compile stylesheet'
      javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet
      *     at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:828)*
      *     at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:617)*
      *     at TransformationEngine.main(TransformationEngine.java:9)*

      The transformation doesn't work in IE or XML Notepad either. I reckon this is due to fact that fn:year-from-dateTime belongs to XPath 2.0 I don't know how to enforce XPath 2.0 within my XSLT.

      Namespace 'http://www.w3.org/2005/xpath-functions' does not contain any functions.

      I hoped that it will work with in Java code but that doesn't work as well.

      Please goive any pointer what I can do here to resolve this issue?

      Regards,
        • 1. Re: XSL transformation issue
          AlexAnd
          try
          to get date from "2012-05-29T00:00:00.000+00:00" as in sample by odie_63 Re: How to extract date part from dateTime datatype of XML
          >
          let $myDateTime := xs:dateTime("2012-09-28T21:49:45")
          return <result>{$myDateTime cast as xs:date}</result>

          let $myDateTime := xs:dateTime("2012-09-28T21:49:45")
          return <result>{xs:date($myDateTime)}</result>
          >

          then get year from date based on fn:year-from-date
          • 2. Re: XSL transformation issue
            Jason_(A_Non)
            Does your xsl have any other XPath 2.0 functions in it? It looks like you are using Xalan, which doesn't support XPath 2.0 (http://xml.apache.org/xalan-j/). You will need to determine if the xslt engine being used supports XPath 2.0. If that is correct, that would be the root cause of your issue.
            • 3. Re: XSL transformation issue
              odie_63
              Unless you require schema-aware XSLT (a 2.0 feature too) and need to handle the proper datatypes, why not simply :
              <xsl:value-of select="fn:substring(TRX_DATE, 1, 4)"/>
              {code}                                                                                                                                                                                                                                                                                                                                                                                                
              • 4. Re: XSL transformation issue
                nk_arw
                Hi,

                Thanks for your reply. Now I understand from error message that the XSLT processor used by my Java code actually implements Xalan which doesn't support XPath 2.0. Unfortunately I have to use Java to transform my XML payload file and hence I have to live with Xalan. I guess I have to resort back to custom Java method being called fom XSLT to extract Year, Month and Date from DateTime string. Is there a better way to achieve this using some XPath 1.0 functions directly?

                Regards
                • 5. Re: XSL transformation issue
                  odie_63
                  Hi,
                  Is there a better way to achieve this using some XPath 1.0 functions directly?
                  You probably missed my reply? ;)

                  Is it not simple enough ?

                  year : substring(DT, 1, 4)
                  month : substring(DT, 6, 2)
                  day : substring(DT, 9, 2)
                  date : substring(DT, 1, 10) or substring-before(DT, "T")
                  time : substring(DT, 12) or substring-after(DT, "T")