4

I am trying to convert an xml file to html using xsl stylesheets. Please see the code below. I have tried many ways to resolve the issue but somehow cant. If I open the xml file then I am able to see the desired output, but why am i not able to see the same through programming?

Error Message: ERROR: 'Jaxpone.xsl' 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:885) at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:671) at crawler.JAXPExamples.basic(JAXPExamples.java:52) at crawler.JAXPExamples.main(JAXPExamples.java:40)

Please see the code below

package crawler;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.xml.sax.SAXException;


public class JAXPExamples {


    public static void main(String argv[])
        throws TransformerException, TransformerConfigurationException,
               IOException, SAXException, ParserConfigurationException,                 
               FileNotFoundException
        {
        try {
         URL xmlURL = new URL("file://Jaxpone.xml");
         String xmlID = xmlURL.toString();
         URL xslURL = new URL("file://Jaxpone.xsl");


         String xslID = xslURL.toString();
     //
         System.out.println("--- basic ---");
         basic(xmlID, xslID);
         System.out.println();

      } catch(Exception err) {
        err.printStackTrace();
      }
   }
      public static void basic(String xmlID, String xslID)
      throws TransformerException, TransformerConfigurationException
   {
      TransformerFactory tfactory = TransformerFactory.newInstance();
      Transformer transformer = tfactory.newTransformer(new StreamSource(xslID));

      StreamSource source = new StreamSource(xmlID);
      transformer.transform(source, new StreamResult(System.out));
   }

}

XSLT file code

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="title">
        <h2><b><xsl:value-of select="."/></b></h2><br />
    </xsl:template>

    <xsl:template match="pub_date">
        <h5><xsl:value-of select="."/></h5><br />
    </xsl:template>

        <xsl:template match="section">
        <p><b><xsl:value-of select="."/></b></p><br />
    </xsl:template>

        <xsl:template match="author">
        <p><b><xsl:value-of select="."/></b></p><br />
    </xsl:template>

        <xsl:template match="link">
        <p><xsl:value-of select="."/></p><br />
    </xsl:template>

        <xsl:template match="description">
        <p><xsl:value-of select="."/></p><br />
    </xsl:template>

        <xsl:template match="body">
       <p><xsl:value-of select="."/></p><br />
    </xsl:template>

    <xsl:template match="/">
        <html>
        <body>
        <xsl:apply-templates/>
        </body>
        </html>
</xsl:template>

</xsl:stylesheet>
5
  • Does the exception not give any detailed error message telling you exactly what failed? Which statement gives the exception? Commented Jul 18, 2013 at 9:00
  • Give detailed exception and also your xslt file sample code Commented Jul 18, 2013 at 9:08
  • @Martin I have provided error message Commented Jul 18, 2013 at 10:07
  • @Prabhaker XSLT code is uploaded...On compiling I get this error XML Parsing Error: mismatched tag. Expected: </br>. Location: file:///home/priya/result.xml Line Number 89, Column 3: Commented Jul 18, 2013 at 10:18
  • I think your XML file is not well-formed(i.e well-formed xml file should have proper closing tags).Please make sure xml file must be well-formed and try again. Commented Jul 18, 2013 at 11:05

1 Answer 1

2

A TransformerConfigurationException generally means there is an error in your stylesheet. The actual errors will have been notified to your ErrorListener. You didn't supply an ErrorListener so they will go to the default ErrorListener, which will probably write messages to the console, or to some log file.

Try running the stylesheet direcly from the command line or from an IDE until you know the code is correct.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.