Example: Generating Multiple Output Files

The whole point of extensions is to allow you to add new capabilities to the XSLT processor. One of the most common needs is the ability to generate multiple output documents. As we saw earlier, the document() function allows you to have multiple input documents—but XSLT 1.0 doesn’t give us any way to create these. Saxon’s support for XSLT 2.0 includes the <xsl:result-document> element, which lets us generate multiple output documents. Although Xalan doesn’t support XSLT 2.0, it does support an extension element (<redirect:write>) that does the same thing. We’ll look at a stylesheet that uses <xsl:fallback> to generate useful results regardless of the processor we’re using. If we’re using Saxon or Xalan, we’ll get multiple output documents that are hyperlinked together; if we’re using any other processor, we’ll get a single HTML file that contains the same information.

Here’s the source document we’ll use:

<?xml version="1.0"?>
<!-- chapters.xml -->
<book>
  <title>XSLT Topics</title>
  <chapter>
    <title>XPath</title>
    <para>If this chapter had any text, it would appear here.</para>
  </chapter>
  <chapter>
    <title>Stylesheet Basics</title>
    <para>If this chapter had any text, it would appear here.</para>
  </chapter>
...
  <chapter>
    <title>Combining XML Documents</title>
    <para>If this chapter had any text, it would appear here.</para>
  </chapter>
</book>

In addition to the <xsl:fallback> element, our stylesheet also uses the element-available() function to ...

Get XSLT, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.