1
votes

I want to write an xsl document to generate motre than 1 output in .html according to the xml

xml :

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

<book>
    <title>Libro</title>
    <chapter>
        <title>Chapter 1</title>
        <content></content>
    </chapter>
    <chapter>
        <title>Chapter 2</title>
        <content></content>
    </chapter>
</book>

I want to get one html for each "chapter" in the xml, in this case, the result will have to be 2 html document

xsl :

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:saxon="http://saxon.sf.net/" extension-element-prefixes="saxon"
                version="1.1">

<xsl:output method="html" indent="yes"/>

<xsl:template match="./book/">
    <xsl:for-each select="./chapter">
        <xsl:variable name="filename" select="concat(title,'.html')"/>
        <xsl:document href="{$filename}">
            <html>
                <head>
                    <title>
                        <xsl:value-of select="title" />
                    </title>
                </head>
                <body>
                    <xsl:value-of select="body" />
                </body>
            </html>
        </xsl:document>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

java code :

DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(input);
// Use a Transformer for output
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new StreamSource(xslt));

DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);

I've some questions

1.- I ran the program but I had this error:

java.lang.RuntimeException: Elemento 'http://www.w3.org/1999/XSL/Transform:document' de XSL no soportado

I dont know why, I've add the path in my xsl:stylesheet but doesn't seem to work correclty

should I change for ?

2.- After I add to my java project the saxon library I have this error:

Attribute @href is not allowed on element

How do I solve this problem ?

1

1 Answers

1
votes

I see two problems:

  1. In your XSL file you should be using xsl:result-document instead of xsl:document.

  2. When you invoke transform on the Transformer object the result should be an empty document: transformer.transform(source, new DOMResult());