1

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 Answer 1

1

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());

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

2 Comments

Thanks, xsl:result-document was part of the problem, I had to add the saxon library to my project, and that's how it worked
For completeness, this call needs to be made (or its equivalent): System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl"); The Saxon parser supports the result-document element, allowing the transformation to produce multiple output documents.

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.