Generate Multiple Output Documents with XSLT 2.0

Unlike XSLT 1.0, XSLT 2.0 allows you to produce more than one result tree from a single transformation.

XSLT 2.0 has added the functionality to serialize more than one result tree when performing a single transformation with the new result-document element. The stylesheet in this hack will show you how to produce four result documents from one source document.

The result-document.xsl stylesheet produces four result trees based on time.xml. The default result tree is output as text, and the remaining three are output as XML, HTML, and XHTML, respectively. The stylesheet is listed in Example 3-28.

Example 3-28. result-document.xsl

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:output name="xml" method="xml" indent="yes"/> <xsl:output name="html" method="html" indent="yes"/> <xsl:output name="xhtml" method="xhtml" indent="yes"/> <xsl:param name="dir">file:///C:/Hacks/examples/</xsl:param> <xsl:template match="/"> <xsl:apply-templates select="time" mode="text"/> <xsl:result-document format="xml" href="{$dir}/rd.xml"> <time> <xsl:message terminate="no">Printing XML result tree in rd.xml...</xsl:message> <xsl:apply-templates select="time" mode="xml"/> </time> </xsl:result-document> <xsl:result-document format="html" href="{$dir}/rd.html"> <xsl:message terminate="no">Printing HTML result tree in rd.html... </xsl:message> <html> <body> <h2>Time</h2> <ul> <xsl:apply-templates ...

Get XML Hacks now with O’Reilly online learning.

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