O'Reilly logo

XML Hacks by Michael Fitzgerald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required