XSLT 2.0 adds several features that greatly enhance the ability of style sheets to work with data. Apart from the benefits of being built on XPath 2.0 (which I'll discuss in next month's column), XSLT 2.0 provides many other benefits to the XSLT language, including the ability to:
- Group nodes
- Create user-defined functions
- Create multiple result documents from a single style sheet
Let's take a look at each of these features in more detail.
One of the biggest headaches in XSLT 1.0 is the inability of the language to directly perform a
SELECT DISTINCT against a group of nodes. This ability is invaluable when, for example, you want to transform a list of books with their authors (as shown in Listing 1) into a corresponding document that shows the authors and all of their books (as shown in Listing 2).
Listing 1. Books with author names
<book title="Professional XML"> <author name="Kevin Williams" /> <author name="A. Nother Author" /> </book> <book title="XML Antipatterns"> <author name="Kevin Williams" /> </book> <book title="XML for Managers"> <author name="A. Nother Author" /> <author name="Y. A. Author" /> </book> |
Listing 2. Authors with book titles
<author name="A. Nother Author"> <book title="Professional XML" /> <book title="XML for Managers" /> </author> <author name="Kevin Williams"> <book title="Professional XML" /> <book title="XML Antipatterns"/> </author> <author name="Y.A. Author"> <book title="XML for Managers" /> </author> |
While it's possible to do this transformation in XSLT 1.0, it's involved and awkward: You have to select all the author nodes, sort them by name, then use an xsl:if block to determine if the author being processed has the same name as the previous node in the set. For each author found, the code then has to search back up the tree for all the book elements with that particular author name embedded in them. It works, but it's very clumsy.
In XSLT 2.0, the xsl:for-each-group element
is introduced. This element allows the programmer to take a set of
nodes, group it by some criteria, and then process each group formed by
that selection process. This makes it much simpler to perform transforms, as
shown below in Listing 3.
Listing 3. Example of the xsl:for-each-group element
<xsl:for-each-group
select="book/author"
group-by="author/@name">
<xsl:sort select="author/@name">
<author name="{current-group()/@name}">
<xsl:for-each select="current-group()">
<book title="{../@title}" />
</xsl:for-each>
</author>
</xsl:for-each-group>
|
Creating user-defined functions
XSLT 2.0 also includes the new xsl:function
element. This element allows you to define a function that operates
much like a function in any other programming language: You define
strongly typed inputs and a strongly typed output, and manipulate the inputs
to produce the output. After you declare the function in a namespace (typically a namespace
associated with the style sheet), and you can then call it anywhere that you could call a built-in
XSLT function (for instance, in the select
attribute of an xsl:value-of element).
This functionality works well as a recursive application, as shown in the sample factorial
function in Listing 4.
Listing 4. Factorial function using xsl:function
<xsl:function name="local:factorial">
<xsl:param name="value" type="xsd:integer" />
<xsl:result
select="if($value=1)
then 1
else $value * local:factorial($value - 1)" />
</xsl:function>
|
Creating multiple result documents from a single style sheet
In XSLT 1.0, the transformation always resulted in one output document. With XSLT 2.0, you can create multiple result documents from a single style sheet. For example, you can create all the frames of a framed XHTML document or different formats for the same content (such as having a single style sheet that creates HTML, XHTML, WML, and VoiceML interpretations of the same source). Each of the additional documents created by the transformation has a URI associated with it. Use of the URI is application-specific (for instance, it might be used to serialize the secondary documents to a particular location in the file system or simply as a mechanism for software using XSLT processors to access the secondary documents through code). Listing 5 shows you how to create the primary document and two secondary documents.
Listing 5. XSLT 2.0 code used to generate additional documents
<xsl:template match="/">
... primary output generation here ...
<xsd:result-document href="result.wml">
... generation of WML output here...
</xsd:result-document>
<xsd:result-document href="result.voiceml">
... generation of VoiceML output here...
</xsd:result-document>
</xsl:template>
|
One very important feature of XSLT 2.0 is that you can specify that a particular transform's output be serialized as XHTML. The serialization takes particular steps to ensure that the output is compatible with current browsers. Specifically:
- Empty elements have a space preceding their empty tag (/>) markers
- Elements with an empty content model always use the empty tag abbreviation (<br />)
- Elements that do not have an empty content model, but happen to be empty, never use the empty tag abbreviation (<p></p> rather than <p />)
- Non-ASCII values in attributes are properly escaped
Sorting within XPath expressions
With XSLT 2.0, you can also create named sort specifications. You can then use these specifications within XPath expressions to sort data. This makes it possible to sort any arbitrary set of nodes at any level within an XPath expression. For example, Listing 6 first sorts the list of books from the sample XML document in Listing 1 by book title, then by author name.
Listing 6. Using the sort XPath function
<xsl:sort-key name="bookSort">
<xsl:sort select="@title" />
<xsl:sort select="author/@name" />
</xsl:sort>
<xsl:for-each select="sort('bookSort', book)">
... each book is processed, ordered by title and author name...
</xsl:for-each>
|
In this column, I've looked at some of the significant changes being made to XSLT 2.0. There are also significant changes to XPath from version 1.0 to version 2.0, which I'll tackle in my next column. Since XSLT 2.0 is still in working draft form, there probably will not be any stable implementations of it until it is promoted to Proposed Recommendation status. However, if you are aware of the new features now, you can design your systems to take advantage of XSLT 2.0 when it becomes available.
- Get a good grounding in XSLT
and XPath at the W3C references for those technologies.
- Find the current status of the XSLT
2.0 specification and W3C working draft.
- Find more XML resources on the developerWorks
XML technology zone.
- Get Rational Application Developer for WebSphere Software
, an easy-to-use, integrated development environment for building, testing, and deploying J2EE applications, including generating XML documents from DTDs and schemas.
- Find out how you can become an IBM Certified Developer in XML and related technologies.
-
Find other articles in Kevin William's XML for Data column.
Kevin Williams is the CEO of Blue Oxide Technologies, LLC, a company that designs XML and Web service creation software. Visit their Web site at http://www.blueoxide.com. Kevin can be reached for comment at kevin@blueoxide.com.



