2

I want to know how can I generate multiple output (html) from one xml using java and xsl.

For example, having this xml:

<ARTICLE>
  <SECT> 
     <PARA>The First 1st Major Section</PARA>
  </SECT>
  <SECT>
     <PARA>The Second 2nd Major Section</PARA>
  </SECT>
</ARTICLE>

For each child element "SECT" from "ARTICLE" I would like to have one ".html" as an output, example of the output:

sect1.html

<html>
   <body>
      <div>
         <h1>The First 1st Major Section</h1>
      </div>
   </body>
</html>

sect2.html

<html>
   <body>
      <div>
         <h1>The First 2nd Major Section</h1>
      </div>
   </body>
</html>

I've been working in java to transform the .xml document with the next code:

            File stylesheet = new File(argv[0]);
            File datafile = new File(argv[1]);

            DocumentBuilder builder = factory.newDocumentBuilder();
            document = builder.parse(datafile);

            // Use a Transformer for output
            TransformerFactory tFactory = TransformerFactory.newInstance();
            StreamSource stylesource = new StreamSource(stylesheet);
            Transformer transformer = tFactory.newTransformer(stylesource);

            DOMSource source = new DOMSource(document);

            OutputStream result=new FileOutputStream("sections.html");

            transformer.transform(source, new StreamResult(result));

The problem is that I have only one output, Could you help me to write the .xslt document please? and tell me how to get more than 1 output?

1 Answer 1

1

To create more than one result document, you need an XSLT Processor which supports multiple result documents. The feature of multiple result documents was introduced in XSLT 2.0. Some XSLT Processors which do not yet implement XSLT 2.0 or newer feature multiple result documents as a proprietary extension.

Creating multiple result documents is, unlike the primary result document, not controlled directly from the Java source code. Instead, the XSLT code needs to contain the XSLT elements that create the multiple result documents.

In XSLT 2.0 and newer, the <xsl:result-document/> element is used to create multiple result documents. See XSLT 2.0, <xsl:result-document/> for more information and examples.

As far as I am aware, the XSLT Processor shipped with Java is Xalan-J, and Xalan-J does not yet support XSLT 2.0 or newer (according to their website http://xml.apache.org/xalan-j/). You might want to use Saxon instead, which supports XSLT 3.0. Or as described in this previous question Xalan XSLT multiple output files? you could use the Redirect extension.

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.