Recipe6.9.Outputting Multiple Documents


Recipe 6.9. Outputting Multiple Documents

Problem

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

Solution

Although most XSLT 1.0 implementations had extensions to help process multiple documents, they differed quite a bit. XSLT 2.0 provides xsl:result-document.

The xsl:result-document instruction takes the following attributes:


format

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


href

Determines the destination where the output document will be serialized.


validation

Determines the validation to be applied to the result tree.


type

Determines the type that should be used to validate the result tree.

Here is an example that splits an XML document into several documents based on the the 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 you want a stylesheet that outputs more than one format. For example, the default output format may be XML, but the output you send to an alternate destination may be HTML. For this, you need to take advantage of 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">     <!-- Default output format is XML -->   <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>     <!-- Another named output format for 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> <!-- Other templates here -->    </xsl:stylesheet>

Discussion

Although the use of xsl:result-document is straightforward, there is a potential for confusion with another new XSLT 2.0 instruction, xsl:document. This is because the XSLT 1.1 specification (now defunct) also had an instruction called xsl:document that had similar behavior to 2.0's xsl:result-document.

In 2.0, the xsl:document plays a more limited role. Its purpose is to construct a document node, presumably because you want to perform document-level validation without actually serializing the result. Typically, you will 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>

If in later processing you want to output the document, you can use xsl:result-document:

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

See Also

See Recipe 8.6 for information on XSLT 1.0 extensions to support multiple output documents.




XSLT Cookbook
XSLT Cookbook: Solutions and Examples for XML and XSLT Developers, 2nd Edition
ISBN: 0596009747
EAN: 2147483647
Year: 2003
Pages: 208
Authors: Sal Mangano

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net