?
Solved

EXSLT and Java  example

Posted on 2005-03-26
9
Medium Priority
?
3,656 Views
Last Modified: 2013-11-23
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
Comment
Question by:sunray_2003
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
9 Comments
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 13636278
Try to remove below line from your xsl file

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

It works

Regards
Sudhakar Chavali
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 13636288
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 13636309
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 49

Author Comment

by:sunray_2003
ID: 13637691
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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 13638772
have you placed xalan.jar,xmlApis.jar,xsltc.jar under <JAVA_HOME>/jre/lib/endorsed folder ??
0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 1400 total points
ID: 13638774
You should look into this also

http://xml.apache.org/xalan-j/xsltc_usage.html
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 13638777
And also May I see your Samp;le XML file?

0
 
LVL 49

Author Comment

by:sunray_2003
ID: 13646051
//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
 
LVL 49

Author Comment

by:sunray_2003
ID: 13752369
Any feedback on my last comment ?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month12 days, 15 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question