Skip to main content

If you don't have an IBM ID and password, register here.

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. This profile includes the first name, last name, and display name you identified when you registered with developerWorks. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

XML for Data: XSLT 2.0: An early look

A preview of some of the planned features for XSLT 2.0

Kevin Williams (kevin@blueoxide.com), CEO, Blue Oxide Technologies, LLC
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.

Summary:  In this column, Kevin Williams takes a look at the latest working draft for XSLT 2.0 and provides some code samples that show just how useful the new version of the XML styling language can be.

View more content in this series

Date:  01 Jul 2002
Level:  Intermediate

Comments:  

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.

Grouping nodes

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>


XHTML support

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>


Conclusion

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.


Resources

About the author

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.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in

If you don't have an IBM ID and password, register here.


Forgot your IBM ID?


Forgot your password?
Change your password


By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. This profile includes the first name, last name, and display name you identified when you registered with developerWorks. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)


By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML
ArticleID=12134
ArticleTitle=XML for Data: XSLT 2.0: An early look
publish-date=07012002
author1-email=kevin@blueoxide.com
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).