Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 1st, 2006, 02:53 AM
Authorized User
 
Join Date: Jul 2006
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default xsl transformation performed by various files

Hello all,

          I`m working on a java function that has to perform an xsl transformation.This xsl transformation is defined in 5 xsl files.

My function is not working and I was wondering if I should write all the xsl files in just one xsl file.

This is part of my code:

/*Main function*/
String xslOrigen = new File("C:\\D2L.xsl");//D2L.xsl includes the other 4 files

Source xsltSource = new StreamSource(new File(xslOrigen));

String xmltarget =XslTransform(
xmlSource ,xsltSource));

/*XslTransform function*/

private String XslTransform(String xmlSource , Source xsl) throws Exception
    {
        StringWriter cadenaSalida = new StringWriter();
        Result bufferResultado = new StreamResult(cadenaSalida);

        TransformerFactory factoriaTrans = TransformerFactory.newInstance();
        Transformer transformador = factoriaTrans.newTransformer(xsl);

        transformador.transform(new StreamSource(new StringReader(xmlSource)), bufferResultado);

        return cadenaSalida.toString();
    }

Is there any problem with this code??

Thanks in advance.

Tomi.





 
Old August 1st, 2006, 03:33 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

This line:

String xslOrigen = new File("C:\\D2L.xsl");

is a basic Java type error. If it's a File, then it isn't a String.

If you show the diagnostics, then I might be able to help you understand them. Asking me to find the errors without showing me the symptoms is unhelpful.

The most common cause of problems in this area is failing to call setSystemId() on the Source object, which means that relative URIs can't be resolved.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old August 1st, 2006, 05:31 AM
Authorized User
 
Join Date: Jul 2006
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dear Mr Kay,
            It is clear than the transformation is not being performed because I get the following errror messages:

            ERROR 'the first argument of the Java non-static function date-time is not a valid reference to an object'
            ERROR 'the stylesheet couldn´t be compiled'

Ive tried my xsl files with saxon on the command line and they work fine,anyway...the following file is the only one were I use date-time elements.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:fn="http://www.w3.org/2006/xpath-functions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:gen="General" xmlns:d2lm="D2LogicalModel" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xdt="http://www.w3.org/2005/xpath-datatypes">

<xsl:template name="Hora">
    <xsl:param name="horaInicio"/>

<xsl:variable name="date"
     select="concat($horaInicio, ':00' )"/>

<xsl:variable name="local_time">
<xsl:value-of select="adjust-dateTime-to-timezone(xs:dateTime($date))"/>
</xsl:variable>

<xsl:variable name="gmt_time">
<xsl:value-of select="adjust-dateTime-to-timezone(xs:dateTime($local_time),xs:dayTimeDurati on('PT0H'))"/>
</xsl:variable>

<xsl:variable name= 'temp_time' select='substring( $gmt_time, 0 , 20 )'/>


<xsl:variable name= 'offset' select='substring( $local_time, 20 , 7 )'/>

<xsl:variable name="ok_time"
select="concat($temp_time, $offset )"/>

<gen:inputTime>
    <xsl:value-of select="$ok_time"/>
</gen:inputTime>

<gen:startTime>
    <xsl:value-of select="$ok_time"/>
</gen:startTime>


</xsl:template>

<xsl:template name="HoraAux">
    <xsl:param name="horaInicio"/>


<xsl:variable name="date"
     select="concat($horaInicio, ':00' )"/>


<xsl:variable name="local_time">
<xsl:value-of select="adjust-dateTime-to-timezone(xs:dateTime($date))"/>
</xsl:variable>


<xsl:variable name="gmt_time">
<xsl:value-of select="adjust-dateTime-to-timezone(xs:dateTime($local_time),xs:dayTimeDurati on('PT0H'))"/>
</xsl:variable>

<xsl:variable name= 'temp_time' select='substring( $gmt_time, 0 , 20 )'/>


<xsl:variable name= 'offset' select='substring( $local_time, 20 , 7 )'/>

<xsl:variable name="ok_time"
select="concat($temp_time, $offset )"/>

<xsl:value-of select="$ok_time"/>

</xsl:template>

</xsl:stylesheet>


I maybe be failing to include some library??

Thank you very much for your help.

Tomi.

 
Old August 1st, 2006, 05:33 AM
Authorized User
 
Join Date: Jul 2006
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry, Ive miss-written the first error message,instead of date-time is dateTime.

 
Old August 1st, 2006, 05:40 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If the stylesheet works from the command line but not from your Java application, then there's nothing wrong with the stylesheet and there's something wrong with your Java application - so you've shown the wrong code.

I don't think these are Saxon error messages. I think you're loading the Xalan processor (which won't recognize XSLT 2.0 functions). It's a good idea to do

<xsl:comment>Generated using:
  <xsl:value-of select="system-property('xsl:vendor')"/>
</xsl:comment>

just to be sure.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old August 1st, 2006, 06:13 AM
Authorized User
 
Join Date: Jul 2006
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is the part of my application that deals with the xsl transformation:

protected void processRequest()
    throws IOException
    {

        String fileName2 = "datex2.xml";


        try
        {

            File inputFile = new File("C:\\D2L.xsl");

            //String xmlSystemId = inputFile.toURL().toExternalForm( );

            FileReader in = new FileReader(inputFile);
            StreamSource xsltSource = new StreamSource(in);


            BufferedWriter file_out2 = new BufferedWriter(new FileWriter(fileName2));



//fetchURL function returns a String containing the xml source file
           file_out2.write(XslTransform( fetchURL("http://www1.dgt.es/dtx/dtxml2.xml") ,xsltSource));



           file_out2.close();


        }
        catch (IOException e)
        {

            System.out.println("IOException:");
            e.printStackTrace();

        }



    }

private String XslTransform(String xmlSource , StreamSource xsl) throws Exception
    {
        try
        {
            StringWriter cadenaSalida = new StringWriter();
            Result bufferResultado = new StreamResult(cadenaSalida);

            TransformerFactory factoriaTrans = TransformerFactory.newInstance();

             Transformer transformador = factoriaTrans.newTransformer(xsl);


            transformador.transform(new StreamSource(new StringReader(xmlSource)), bufferResultado);

            return cadenaSalida.toString();
        }
        catch (Exception e)
        {
            System.out.println("Erroroooooo::");
            System.out.println(e.getMessage());
        }

        return "error";

    }

Thanks for you time.


Tomi

 
Old August 1st, 2006, 06:51 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

After calling TransformerFactory.newInstance(), try doing System.err.println(factoriaTrans.getClass()), to find out what processor you have loaded. This depends on settings of system properties and your classpath.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old August 1st, 2006, 07:27 AM
Authorized User
 
Join Date: Jul 2006
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I get the following message:

"class com.sun.org.apache.xalan.internal.xsltc.trax.Trans formerFactoryImpl"

As you said it seems im loading xalan instead of saxon.

How can I change it??

Thanks.

Tomi.


 
Old August 1st, 2006, 07:34 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If you don't mind your application being committed to Saxon at compile-time, the simplest fix is to change the call on

tfactory = TransformerFactory.newInstance()

to

tfactory = new net.sf.saxon.TransformerFactoryImpl()

The alternative is to make sure the system property javax.xml.transform.TransformerFactory is set to the value net.sf.saxon.TransformerFactoryImpl.

In principle you should get Saxon if saxon8.jar is the first thing on the classpath, but that doesn't seem to be reliable.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old August 1st, 2006, 07:53 AM
Authorized User
 
Join Date: Jul 2006
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Where can I find the package "net.sf.saxon"?

Tomi.








Similar Threads
Thread Thread Starter Forum Replies Last Post
xsl transformation ... rajesh_css XSLT 5 September 30th, 2008 12:37 AM
XSL-Transformation atulshin XSLT 4 September 15th, 2008 06:37 AM
WML and XSL transformation. Feodorov XSLT 2 February 13th, 2008 05:00 PM
Transformation of XSL text sg1973 XSLT 5 August 9th, 2006 03:47 PM
XSL transformation Thodoris XML 0 May 20th, 2004 08:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.