WebReference.com - Excerpt from Inside XSLT, Chapter 2, Part 6 (1/5) | WebReference

WebReference.com - Excerpt from Inside XSLT, Chapter 2, Part 6 (1/5)

current pageTo page 2To page 3To page 4To page 5

Inside XSLT

Simplified Stylesheets

[Editor's note: the following is the conclusion of our series of excerpts from chapter 2 of the New Riders publication, "Inside XSLT."]

As you can see from the material covered so far, it can take some thought to create XSLT stylesheets. The W3C has tried to make things easier by introducing simplified stylesheets, where you don't need to-and in fact cannot-include the <xsl:stylesheet> element or any other top-level elements.

In fact, a simplified stylesheet is simply the result document with a few non-top-level XSL elements in it. W3C calls this a "literal result element as stylesheet."

In Listing 2.7, I'll transform planets.xml into planets.html, but this time I'll do it with a simplified stylesheet. In simplified stylesheets, you can't use top-level elements such as <xsl:template>, which allow recursive processing of all elements in the source document. So here, I'll look ahead a little and use the <xsl:for-each> element (covered in Chapter 5), which is not a top-level element, but which enables you to loop over a number of nodes at once.

I'll also need some way of matching all <PLANET> elements in the source document, and you might not think that's possible without several levels of templates-for example, one for the root node, then one to match the next level down, which is the <PLANETS> root element, and then another level down for the <PLANET> elements themselves. In fact, using XPath, you can use the expression "//PLANET" to match any <PLANET> element node that is descended from the root node (see Chapter 4). This means that I can write the simplified stylesheet as follows:

Listing 2.7. Simplified Stylesheet

      The Planets Table
      The Planets Table
    <TABLE BORDER="2">
      <xsl:for-each select="//PLANET">
          <TD><xsl:value-of select="NAME"/></TD>
          <TD><xsl:value-of select="MASS"/></TD>
          <TD><xsl:value-of select="RADIUS"/></TD>
          <TD><xsl:value-of select="DAY"/></TD>

This version works just as the previous version of planets.xsl did, and without any top-level elements at all. Simplified stylesheets such as this one were introduced to help HTML authors make the transition to XSL, but the fact is that they're only of limited utility. As you can see, you still need to know how to use XSL elements, and the fact that you can't use <xsl:template> has only made the job more difficult here. But you should know that simplified stylesheets exist, and they're part of the XSLT specification.

Default Handling without an <xsl:stylesheet> Element
If an XSLT processor can't find the <xsl:stylesheet> element in a stylesheet, it's supposed to treat the stylesheet as a simplified stylesheet.

current pageTo page 2To page 3To page 4To page 5

Created: October 16, 2001
Revised: October 16, 2001

URL: http://webreference.com/authoring/languages/xml/insidexslt/chap2/6/