• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3715
  • Last Modified:

EXSLT and Java example

Hi,
I am trying to run exslt transformation date functions using java

This is my exslt.xsl file

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:date="http://exslt.org/dates-and-times"
                extension-element-prefixes="date"
                version="1.0">

  <xsl:template match="/">

    right now:
    date:date: <xsl:value-of select="date:date()"/>
    date:date-time: <xsl:value-of select="date:date-time()"/>
    date:day-name: <xsl:value-of select="date:day-name()"/>
    date:day-abbreviation: <xsl:value-of select="date:day-abbreviation()"/>
    date:day-in-week: <xsl:value-of select="date:day-in-week()"/>
    date:day-in-month: <xsl:value-of select="date:day-in-month()"/>
    date:day-in-year: <xsl:value-of select="date:day-in-year()"/>
    date:day-of-week-in-month: <xsl:value-of
select="date:day-of-week-in-month()"/>
    date:hour-in-day: <xsl:value-of select="date:hour-in-day()"/>
    date:leap-year: <xsl:value-of select="date:leap-year()"/>
    date:minute-in-hour: <xsl:value-of select="date:minute-in-hour()"/>
    date:month-abbreviation: <xsl:value-of select="date:month-abbreviation()"/>
    date:month-in-year: <xsl:value-of select="date:month-in-year()"/>
    date:month-name: <xsl:value-of select="date:month-name()"/>
    date:second-in-minute: <xsl:value-of select="date:second-in-minute()"/>
    date:time: <xsl:value-of select="date:time()"/>
    date:week-in-year: <xsl:value-of select="date:week-in-year()"/>
    date:year: <xsl:value-of select="date:year()"/>

    with parameter of '2003-07-11' passed:
    date:day-name: <xsl:value-of select="date:day-name('2003-07-11')"/>
    date:day-abbreviation: <xsl:value-of select="date:day-abbreviation
     ('2003-07-11')"/>
    date:day-in-week: <xsl:value-of select="date:day-in-week('2003-07-11')"/>
    date:day-in-year: <xsl:value-of select="date:day-in-year('2003-07-11')"/>
    date:day-of-week-in-month: <xsl:value-of select="date:day-of-week-in-month
     ('2003-07-11')"/>
    date:leap-year: <xsl:value-of select="date:leap-year('2003-07-11')"/>
    date:month-abbreviation: <xsl:value-of select="date:month-abbreviation
     ('2003-07-11')"/>
    date:month-name: <xsl:value-of select="date:month-name('2003-07-11')"/>
    date:week-in-year: <xsl:value-of select="date:week-in-year('2003-07-11')"/>

  </xsl:template>

</xsl:stylesheet>

and my java code is like this

          // Create transformer factory
          TransformerFactory factory = TransformerFactory.newInstance();

          // Use the factory to create a template containing the xsl file
          Templates template = factory.newTemplates(new StreamSource(
              new FileInputStream(xslFilename)));

          // Use the template to create a transformer
          Transformer xformer = template.newTransformer();

          // Prepare the input file
          Source source = new StreamSource(new FileInputStream(inFilename));

          // Create a new document to hold the results
          DocumentBuilder builder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
          Document doc = builder.newDocument();
          Result result = new DOMResult(doc);
          // Apply the xsl file to the source file and create the DOM tree
          xformer.transform(source, result);

When I run the code I get the following error

javax.xml.transform.TransformerException: org.w3c.dom.DOMException:
HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it
is not permitted.

at org.apache.xalan.transformer.TransformerImpl.transformNode(TransformerImpl.java:1226)

at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:638)

at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1088)

at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1066)

at com.ema.correspondence.util.Untitled2.parseXmlFile(Untitled2.java:51)

at com.ema.correspondence.util.Untitled2.main(Untitled2.java:123)

Caused by: org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt
was made to insert a node where it is not permitted.

or can someone direct me to a "hello world" in EXSLT

Thank you
0
sunray_2003
Asked:
sunray_2003
  • 6
  • 3
1 Solution
 
sudhakar_koundinyaCommented:
Try to remove below line from your xsl file

<?xml version="1.0" encoding="UTF-8"?>

It works

Regards
Sudhakar Chavali
0
 
sudhakar_koundinyaCommented:
0
 
sudhakar_koundinyaCommented:
Or try to use below code

But some instructions

 Place xalan.jar,xmlApis.jar,xsltc.jar under

<JAVA_HOME>/jre/lib/endorsed folder


   public static void main ( String[] args )
         throws Exception
     {
          XSLCompiler app = new XSLCompiler () ;
          app.transform ( "c:/eml.xsl","c:/a1.xml" ) ;
         
     }

     /**
      * Compiles an XSL stylesheet into a translet, wraps tshe translet
      * inside a Templates object and dumps it to a file.
      */
     public Transformer getTransformer ( String xsl )
         throws Exception
     {
          try
          {
          // Set XSLTC's TransformerFactory implementation as the default
          System.setProperty ( "javax.xml.transform.TransformerFactory" ,
                     "org.apache.xalan.xsltc.trax.TransformerFactoryImpl" ) ;

          // Get an input stream for the XSL stylesheet
          StreamSource stylesheet = new StreamSource ( xsl ) ;

          // The TransformerFactory will compile the stylesheet and
          // put the translet classes inside the Templates object
          TransformerFactory factory = TransformerFactory.newInstance () ;
          factory.setAttribute ( "generate-translet" , Boolean.TRUE ) ;
          Templates templates = factory.newTemplates ( stylesheet ) ;
          Transformer transformer = templates.newTransformer () ;
          return transformer ;
          }
          catch ( Exception e )
          {
          System.err.println ( "Exception: " + e ) ;
          e.printStackTrace () ;
          throw new Exception ( "Error " , e ) ;
          }

     }

      public void transform ( String xslFileName , String xml )
         throws Exception
     {
          Transformer transformer = getTransformer ( xslFileName ) ;
          if ( transformer == null )
          {

          throw new RuntimeException ( "Bad XSL File or transformer creation problem" ) ;
          }
          ByteArrayOutputStream transformmedStream = new ByteArrayOutputStream () ;
          FileInputStream fin = new FileInputStream ( xml ) ;
          transformer.transform ( new StreamSource ( fin ) , new StreamResult ( transformmedStream ) ) ;
          System.err.println ( transformmedStream.toString () ) ;
     }
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
sunray_2003Author Commented:
Hi SK,

I tried using  the java almanac example and  the xsl file with  the
first line removed but itis still thorowng me this exception.

I have Xalan, Xeceres, JDOM  on my classpath

javax.xml.transform.TransformerException:
javax.xml.transform.TransformerException: Instance method call to
method date requires an Object instance as first argument
at org.apache.xalan.extensions.ExtensionHandlerJavaPackage.callFunction(ExtensionHandlerJavaPackage.java:395)
at org.apache.xalan.extensions.ExtensionsTable.extFunction(ExtensionsTable.java:257)


Thanks
0
 
sudhakar_koundinyaCommented:
have you placed xalan.jar,xmlApis.jar,xsltc.jar under <JAVA_HOME>/jre/lib/endorsed folder ??
0
 
sudhakar_koundinyaCommented:
You should look into this also

http://xml.apache.org/xalan-j/xsltc_usage.html
0
 
sudhakar_koundinyaCommented:
And also May I see your Samp;le XML file?

0
 
sunray_2003Author Commented:
//java code

import java.io.*;
    import org.w3c.dom.*;
    import org.xml.sax.*;
    import javax.xml.parsers.*;
    import javax.xml.transform.*;
    import javax.xml.transform.dom.*;
    import javax.xml.transform.stream.*;

    public class BasicXsl {
        // This method applies the xslFilename to inFilename and writes
        // the output to outFilename.
        public static void xsl(String inFilename, String outFilename,
String xslFilename) {
            try {
                // Create transformer factory
                TransformerFactory factory = TransformerFactory.newInstance();

                // Use the factory to create a template containing the xsl file
                Templates template = factory.newTemplates(new StreamSource(
                    new FileInputStream(xslFilename)));

                // Use the template to create a transformer
                Transformer xformer = template.newTransformer();

                // Prepare the input and output files
                Source source = new StreamSource(new
FileInputStream(inFilename));
                Result result = new StreamResult(new
FileOutputStream(outFilename));

                // Apply the xsl file to the source file and write the
result to the output file
                xformer.transform(source, result);
            } catch (FileNotFoundException e) {
              e.printStackTrace();
            } catch (TransformerConfigurationException e) {
                // An error occurred in the XSL file
                e.printStackTrace();
            } catch (TransformerException e) {
                // An error occurred while applying the XSL file
                // Get location of error in input file
                e.printStackTrace();
                SourceLocator locator = e.getLocator();
                int col = locator.getColumnNumber();
                int line = locator.getLineNumber();
                String publicId = locator.getPublicId();
                String systemId = locator.getSystemId();
                e.printStackTrace();
            }
        }

        public static void main(String args[])
        {
          BasicXsl.xsl("C:\\date.xml", "c:\\out.txt", "C:\\exslt.xsl");
        }


    }//end class

//xml
<?xml version="1.0" encoding="ISO-8859-1"?>

<formats>
<dummy>xslt</dummy>
<name>ibm rocks</name>
<test>abcd</test>
</formats>

//xsl holding xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:date="http://exslt.org/dates-and-times"
                extension-element-prefixes="date"
                version="1.0">

  <xsl:template match="/">

    right now:
    date:date: <xsl:value-of select="date:date()"/>
    date:date-time: <xsl:value-of select="date:date-time()"/>
    date:day-name: <xsl:value-of select="date:day-name()"/>
    date:day-abbreviation: <xsl:value-of select="date:day-abbreviation()"/>
    date:day-in-week: <xsl:value-of select="date:day-in-week()"/>
    date:day-in-month: <xsl:value-of select="date:day-in-month()"/>
    date:day-in-year: <xsl:value-of select="date:day-in-year()"/>
    date:day-of-week-in-month: <xsl:value-of
select="date:day-of-week-in-month()"/>
    date:hour-in-day: <xsl:value-of select="date:hour-in-day()"/>
    date:leap-year: <xsl:value-of select="date:leap-year()"/>
    date:minute-in-hour: <xsl:value-of select="date:minute-in-hour()"/>
    date:month-abbreviation: <xsl:value-of select="date:month-abbreviation()"/>
    date:month-in-year: <xsl:value-of select="date:month-in-year()"/>
    date:month-name: <xsl:value-of select="date:month-name()"/>
    date:second-in-minute: <xsl:value-of select="date:second-in-minute()"/>
    date:time: <xsl:value-of select="date:time()"/>
    date:week-in-year: <xsl:value-of select="date:week-in-year()"/>
    date:year: <xsl:value-of select="date:year()"/>

    with parameter of '2003-07-11' passed:
    date:day-name: <xsl:value-of select="date:day-name('2003-07-11')"/>
    date:day-abbreviation: <xsl:value-of select="date:day-abbreviation
     ('2003-07-11')"/>
    date:day-in-week: <xsl:value-of select="date:day-in-week('2003-07-11')"/>
    date:day-in-year: <xsl:value-of select="date:day-in-year('2003-07-11')"/>
    date:day-of-week-in-month: <xsl:value-of select="date:day-of-week-in-month
     ('2003-07-11')"/>
    date:leap-year: <xsl:value-of select="date:leap-year('2003-07-11')"/>
    date:month-abbreviation: <xsl:value-of select="date:month-abbreviation
     ('2003-07-11')"/>
    date:month-name: <xsl:value-of select="date:month-name('2003-07-11')"/>
    date:week-in-year: <xsl:value-of select="date:week-in-year('2003-07-11')"/>

  </xsl:template>

</xsl:stylesheet>

I have the jar files
xalan, xerces (xml-apis.jar (JAXP 1.2 & xercesImpl.jar), jdom in my classpath

Exception thrown
----------------
com.ema.correspondence.util.BasicXsl
javax.xml.transform.TransformerException:
javax.xml.transform.TransformerException: Instance method call to
method date requires an Object instance as first argument

at org.apache.xalan.extensions.ExtensionHandlerJavaPackage.callFunction(ExtensionHandlerJavaPackage.java:395)
0
 
sunray_2003Author Commented:
Any feedback on my last comment ?
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now