Introducing XSLT 2.0
When the XSLT 1.0 standard was released, there was no XML Schema standard. Consequently, stylesheets were extremely limited in the types of transformations and XML operations that they could perform. As of this writing, the XSLT 2.0 standard is in its final stages and will bring significant enhancements along with expanded functionality and support for XML Schema. Because the Oracle Java XSLT processor will support this standard, the following sections provide a brief overview of it.
Note | There is also a companion XPath 2.0 standard that incorporates an extensive set of functions and operators on XML Schema data types. This standard will not be discussed because there is not as yet an Oracle implementation. |
Grouping
In XSLT 1.0, stylesheet writers had great difficulty in creating templates that grouped items. In fact, the most popular method was one developed by Steve Muench of Oracle that involves using <xsl:key> to create keys for grouping. In 2.0, <xsl:for-each-group> has been introduced to perform this function. An example of its usage to subset and sort the booklist.xml example by author is as follows:
<xsl:for-each-group select="book" group-by="author">
<xsl:sort select="current-grouping-key()" />
<author>
<xsl:value-of select="current-grouping-key()" />
<xsl:for-each select="current-group()">
<book>
<xsl:value-of select="title" />
</book>
</xsl:for-each>
</author>
</xsl:for-each-group>
This produces the following XML output:
<book isbn="0-07-213495-X">
<title>Oracle9i XML Handbook</title>
<author>Chang, Scardina and Kiritzov</author>
</book>
<book isbn="1230-23498-2349879">
<title>Emperor's New Mind</title>
<author>Roger Penrose</author>
</book>
Function Definitions
In XSLT 2.0, functions can be defined via the xsl:function declaration, callable from any XPath expression in the stylesheet. The name of the function must be a QName, along with any parameters defined via xsl:param elements. In this manner, stylesheet creators can define their own functions in much the same way that functions are created in other programming or scripting languages. The following example defines a function that takes an integer and returns its lexical name in uppercase:
<xsl:function name="str:numtostr" as="xs:string">
<xsl:param name="value" as="xs:integer"/>
<xsl:number value="$value" format="N"/>
</xsl:function>
This can be used in a template:
<xsl:template match="/">
<output>
<xsl:value-of select="str:numtostr(9)"/>
</output>
</xsl:template>
This template returns the following:
<output>NINE</output>
Multiple Result Documents
In XSLT 2.0, stylesheet writers can invoke <xsl:result-document> to create a result tree that can then be validated. The root node of this tree is the document node. The utility of this is that different output formats can be specified for the result tree and validation can occur on these different formats. Turning once again to booklist.xml, the following template will split it into separate files, book1l and book2l:
<xsl:template match="/">
<xsl:for-each-group select="/booklist/book">
<xsl:result-document href="
format="book-format" validation="strip">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title><xsl:value-of select="./title"/></title></head>
<body>
<xsl:copy-of select="current-group()"/>
</body>
</html>
</xsl:result-document>
</xsl:for-each-group>
</xsl:template>
The output of book1l will be
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Oracle9i XML Handbook</title></head>
<body>
<title>Oracle9i XML Handbook</title>
<author>Chang, Scardina and Kiritzov</author>
<publisher>Osborne</publisher>
<price>49.99</price>
</body>
</html>
Temporary Trees
In XSLT 2.0, temporary trees are available for processing. In this manner, intermediate results of transformations can be accessed and then discarded. These trees can be constructed by evaluating an xsl:variable, xsl:param, or xsl:with-param element that has nonempty content (referred to as the variable-binding element), the value of which becomes the document node of the temporary tree.
The advantage is that now, instead of employing one large template, complex XSLT transformations can be broken down and modularized. Since values can be passed in, lookup tables for various mappings can be employed. The following is example syntax showing how to pass the tree between templates that may apply successive styles:
<xsl:import href=">
<xsl:import href=">
<xsl:variable name="style">
<xsl:apply-templates select="/" mode="modern" />
</xsl:variable>
<xsl:template match="/" />
<xsl:apply-templates select="$style" mode="sophisticated"/>
</xsl:template>