XML technologies / XSLT / XSLT Cookbook / Exploiting the potential of XSLT 2.0 / Output multiple documents

(Excerpt from “XSLT Kochbuch” by Sal Mangano)

Problem

You need a portable stylesheet that can output more than one document.

Solution

Most XSLT 1.0 implementations had extensions for processing multiple documents, but these differed from one another. XSLT 2.0 provides xsl:result-document.

The instruction xsl:result-document accepts these attributes:

format

Defines the output format name declared by the named xsl:output instruction.

href

Specifies the destination where the output document is serialized.

validation

Specifies the validation applied to the result tree.

type

Specifies the type used to validate the result tree.

This example splits an XML document into multiple documents based on groups extracted by an xsl:for-each-group. Each output document is named using the grouping key as a suffix:

<xsl:template match="products">
  <xsl:for-each-group select="product" group-by="@type">
    <xsl:result-document href="prod-{current-grouping-key( )}.xml">
      <xsl:copy-of select="current-group( )"/>
    </xsl:result-document>
  </xsl:for-each-group>
</xsl:template>

Sometimes a stylesheet needs to output more than one format. For example, the default output could be XML, while output sent to another destination could be HTML. This requires XSLT 2.0’s ability to specify multiple output formats:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <!-- Standardausgabeformat ist XML -->
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <!-- Ein weiteres angegebenes Ausgabeformat für HTML -->
  <xsl:output method="html" encoding="UTF-8" indent="yes" name="html-out"/>
  <xsl:template match="/">
    <xsl:apply-templates/>
    <xsl:result-document href="result.html" format="html-out">
      <xsl:apply-templates mode="html"/>
    </xsl:result-document>
  </xsl:template>
  <!-- Hier kommen weitere Templates hin -->
</xsl:stylesheet>

Discussion

Using xsl:result-document is straightforward, but it can be confused with another new XSLT 2.0 instruction called xsl:document. The now-defunct XSLT 1.1 specification also contained an instruction called xsl:document with behavior similar to the 2.0 instruction xsl:result-document.

In 2.0, xsl:document has a narrower role. It constructs a document node, presumably for document-level validation without actually serializing the result. Typically, you capture the result of xsl:document in a variable:

<xsl:variable name="tempDoc" as="document(element(*, my:document))">
  <xsl:document type="my:document" validation="strict">
    <xsl:apply-templates select="/*"/>
  </xsl:document>
</xsl:variable>

To output the document later in processing, use xsl:result-document:

<xsl:result-document href="doc.xml">
  <xsl:copy-of select="$tempDoc"/>
</xsl:result-document>

See also

See recipe Split documents for information on XSLT 1.0 extensions supporting multiple output documents.

  

 

 

 

back to top