16.3 Multiple Result Trees

In the last chapter, you used the saxon:output extension element to create more than one result tree from a single stylesheet. XSLT 2.0 has integrated this functionality into the mainstream of the specification with the result-document element. The following example shows you how to use this element to produce three result trees from one source tree.

Example 16-1, the document functions.xml in examples/ch16, describes the new context-related functions from XPath 2.0.

Example 16-1. A document describing XPath 2.0 functions
<?xml version="1.0"?>     <functions type="context">  <function>   <name>fn:context-item(  )</name>  <description>Returns the context item.</description>  </function>  <function>   <name>fn:position(  )</name>   <description>Returns the position of the context item within the sequence of items  currently being processed.</description>  </function>  <function>   <name>fn:last(  )</name>   <description>Returns the number of items in the sequence of items currently being processed.</description>  </function>  <function>   <name>fn:current-dateTime(  )</name>   <description>Returns the current xs:dateTime.</description>  </function>  <function>   <name>fn:current-date(  )</name>   <description>Returns the current xs:date.</description>  </function>  <function>   <name>fn:current-time(  )</name>   <description>Returns the current xs:time.</description>  </function>  <function>   <name>fn:default-collation(  )</name>   <description>Returns the value of the default collation property from the static context.</description>  </function>  <function>   <name>fn:implicit-timezone(  )</name>   <description>Returns the value of the implicit timezone property from the evaluation context.</description>  </function> </functions>

The descriptions of the functions are from the specification. The fn:position( ) and fn:last( ) functions are the same as the position( ) and last( ) functions from XPath 1.0. The fn:context-item( ) function is similar to the current( ) function available from XSLT 1.0 and XSLT 2.0. Usually, a context item is the same as the current item, except when a predicate is involved.

You don't need to worry about the namespace prefix fn: for functions, because you won't need to use it in XSLT. It's there because XPath can be used from other environments besides XSLT, and some may use different function libraries, so it's useful to use namespaces to distinguish the functions as being from different libraries.


Example 16-2, the context.xsl stylesheet, produces four result trees based on functions.xml. The default result tree is text, and the three others are for XML, HTML, and XHTML output, respectively.

Example 16-2. An XSLT 2.0 stylesheet that produces four kinds of output
<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="html" indent="yes"/> <xsl:param name="dir">file:///C:/LearningXSLT/examples/ch16</xsl:param>     <xsl:template match="functions">  <xsl:text>XPath 2.0 Context Functions&#10;</xsl:text>  <xsl:text>Date: </xsl:text>  <xsl:value-of select="current-date(  )"/>  <xsl:text>&#10;</xsl:text>  <xsl:apply-templates select="function" mode="text"/>  <xsl:result-document format="xml" href="{$dir}/context.xml">  <xsl:message terminate="no">Printing text result tree...</xsl:message>   <list>    <description>XPath 2.0 Context Functions</description>    <date><xsl:value-of select="current-date(  )"/></date>     <xsl:message terminate="no">Printing XML result tree in functions.xml...</xsl:message>     <xsl:apply-templates select="function" mode="xml"/>   </list>  </xsl:result-document>  <xsl:result-document format="html" href="{$dir}/context.html">   <xsl:message terminate="no">Printing HTML result tree in functions.html...</xsl:message>   <html>   <body>   <h2>XPath 2.0 Context Functions</h2>   <h3>Date: <xsl:value-of select="current-date(  )"/></h3>   <ul>    <xsl:apply-templates select="function" mode="html"/>   </ul>   </body>   </html>  </xsl:result-document>  <xsl:result-document format="xhtml" href="{$dir}/context-x.html">   <xsl:message terminate="no">Printing XHTML result tree in functions-x.html...</xsl:message>   <html xmlns="http://www.w3.org/1999/xhtml">   <body>   <h2>XPath 2.0 Context Functions</h2>   <h3>Date: <xsl:value-of select="current-date(  )"/></h3>   <ol>    <xsl:apply-templates select="function" mode="xhtml"/>   </ol>   </body>   </html>  </xsl:result-document> </xsl:template>     <xsl:template match="function" mode="text">  <xsl:text> - </xsl:text>  <xsl:value-of select="name"/>  <xsl:text>&#10;</xsl:text> </xsl:template>     <xsl:template match="function" mode="xml">  <function><xsl:value-of select="name"/></function> </xsl:template>     <xsl:template match="function" mode="html">  <li><xsl:value-of select="name"/></li> </xsl:template>     <xsl:template match="function" mode="xhtml">  <li xmlns="http://www.w3.org/1999/xhtml"><xsl:value-of select="name"/></li> </xsl:template>     </xsl:stylesheet>

The version attribute on stylesheet shows the 2.0 version number. There are four output elements, three of which are named. This allows a result-document element to reference an output element by name, hence to use the information in it. A global parameter named dir holds the name of the directory where three of the result trees are written as files. This information is referenced by the attribute value template {$dir} in the href attributes on the result-document elements. You could pass in a new value for the dir parameter if you want to change the destination of the output.

The template matching functions creates a text result tree, plus three other result trees inside result-document elements. Each result tree issues its own message using the message element. Each result tree also applies templates to a template matching function, though each in a different mode (text, xml, html, and xhtml). The different modes for each result help create an appropriate tree for each of the given formats. The new current-date( ) function is called in each result tree, too.

To get this to work, you need to use a full Java version of Saxon, preferably Version 7.7 or later, available from http://saxon.sourceforge.net or in the examples/ch16 directory as saxon7-7.zip (the JAR file saxon7.jar has already been extracted from saxon7-7.zip). For specific instructions on how to download, install, and use Saxon with the Java interpreter, see the appendix.

Once everything is installed and working, you can type this command:

java -jar saxon7.jar functions.xml context.xsl

and you will get the following text result tree, plus messages about the other three:

Printing text result tree... Printing XML result tree in context.xml... Printing HTML result tree in context.html... Printing XHTML result tree in context-x.html... XPath 2.0 Context Functions Date: 2003-08-26  - fn:context-item(  )  - fn:position(  )  - fn:last(  )  - fn:current-dateTime(  )  - fn:current-date(  )  - fn:current-time(  )  - fn:default-collation(  )  - fn:implicit-timezone(  )

The files that the three result-document elements produced contain the other result trees. The first one is context.xml:

<?xml version="1.0" encoding="UTF-8"?> <list>    <description>XPath 2.0 Context Functions</description>    <date>2003-10-03</date>    <function>fn:context-item(  )</function>    <function>fn:position(  )</function>    <function>fn:last(  )</function>    <function>fn:current-dateTime(  )</function>    <function>fn:current-date(  )</function>    <function>fn:current-time(  )</function>    <function>fn:default-collation(  )</function>    <function>fn:implicit-timezone(  )</function> </list>

The second is context.html, an HTML document that uses an unordered (bulleted) list:

<html>    <body>       <h2>XPath 2.0 Context Functions</h2>       <h3>Date: 2003-10-03</h3>       <ul>          <li>fn:context-item(  )</li>          <li>fn:position(  )</li>          <li>fn:last(  )</li>          <li>fn:current-dateTime(  )</li>          <li>fn:current-date(  )</li>          <li>fn:current-time(  )</li>          <li>fn:default-collation(  )</li>          <li>fn:implicit-timezone(  )</li>       </ul>    </body> </html>

And the third is context-x.html, an XHTML document that uses an ordered (numbered) list:

<html xmlns="http://www.w3.org/1999/xhtml">    <body>       <h2>XPath 2.0 Context Functions</h2>       <h3>Date: 2003-10-03</h3>       <ol>          <li>fn:context-item(  )</li>          <li>fn:position(  )</li>          <li>fn:last(  )</li>          <li>fn:current-dateTime(  )</li>          <li>fn:current-date(  )</li>          <li>fn:current-time(  )</li>          <li>fn:default-collation(  )</li>          <li>fn:implicit-timezone(  )</li>       </ol>    </body> </html>

As you can see, result-document provides a great convenience creating more than one result tree from just one stylesheet. Next is an example that uses regular expressions.



Learning XSLT
Learning XSLT
ISBN: 0596003277
EAN: 2147483647
Year: 2003
Pages: 164

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