Home > Enterprise >  Problem with XSLT to HTML transformation in Java
Problem with XSLT to HTML transformation in Java

Time:10-05

Suppose we have the following XSL document to convert to HTML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><?xml-stylesheet type='text/xsl' href="#"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0">

    <xsl:output doctype-public="-//W3C//DTD HTML 4.01//EN" doctype-system="http://www.w3.org/TR/html4/strict.dtd" encoding="UTF-8" indent="yes" method="html" version="4.01"/>
    
    <xsl:template match="/">
        <head>
            <meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
            <style type="text/css">
                    body { font-family:Arial; }
            </style>
        </head>
        <body>
            <p>Hello world!</p>
        </body>
    </xsl:template>

</xsl:stylesheet>

When I try transform it to HTML using the following code in Java I get an error:

    public static void test3() {
        try {
              InputStream is = new FileInputStream(DataDir   "\\test.xml");
              
              DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
              Document xmlDocument = builder.parse(is);
              Document newDocument = builder.newDocument();
        
              TransformerFactory transformerFactory = TransformerFactory.newInstance();
              Transformer transformer = transformerFactory.newTransformer(new DOMSource(xmlDocument));
              transformer.transform(new DOMSource(newDocument), new StreamResult(DataDir   "\\output.html"));

        } catch (TransformerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

The error I get:

ERROR: 'Could not compile stylesheet' FATAL ERROR: 'Could not find stylesheet target '#'.' :Could not find stylesheet target '#'. javax.xml.transform.TransformerConfigurationException: Could not find stylesheet target '#'.

Even if I rewrite the XSLT document by removing the href attribute, the HTML transform doesn't work.

    public static void test2() {
        try {
              InputStream is = new FileInputStream(DataDir   "\\test.xml");
              
              DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
              Document xmlDocument = builder.parse(is);

              XPath xPath = XPathFactory.newInstance().newXPath();
              String expression = "/processing-instruction()[name() = 'xml-stylesheet']";
              NodeList nodes = (NodeList) xPath.evaluate(expression, xmlDocument, XPathConstants.NODESET);
              for(int NodeNo = 0; NodeNo < nodes.getLength(); NodeNo  ) {
                  ProcessingInstruction pi = (ProcessingInstruction) nodes.item(NodeNo);
                  String nodeValue = pi.getNodeValue();
                  nodeValue = nodeValue.replace("href=\"#\"", "");
                  pi.setNodeValue(nodeValue);
              }

              Document newDocument = builder.newDocument();
        
              TransformerFactory transformerFactory = TransformerFactory.newInstance();
              Transformer transformer = transformerFactory.newTransformer(new DOMSource(xmlDocument));
              transformer.setOutputProperty(OutputKeys.INDENT, "yes");
              transformer.transform(new DOMSource(newDocument), new StreamResult(DataDir   "\\output.html"));

        } catch (TransformerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Any idea ?

CodePudding user response:

I think, to use an embedded stylesheet, you should basically do

    TransformerFactory tf = TransformerFactory.newInstance();

    Source input = new StreamSource(new File("test.xml"));

    Source xslt = tf.getAssociatedStylesheet(input, null, null, null);

    Transformer transformer = tf.newTransformer(xslt);

    transformer.transform(input, new StreamResult("output.html"));
  • Related