Simplified Stylesheets

Simplified Stylesheets

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 dont need toand 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, Ill transform planets.xml into planets.html, but this time Ill do it with a simplified stylesheet. In simplified stylesheets, you cant use top-level elements such as <xsl:template> , which allow recursive processing of all elements in the source document. So here, Ill 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.

Ill also need some way of matching all <PLANET> elements in the source document, and you might not think thats possible without several levels of templatesfor 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
 <HTML xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl::version="1.0">      <HEAD>          <TITLE>             The Planets Table          </TITLE>      </HEAD>      <BODY>          <H1>             The Planets Table          </H1>          <TABLE BORDER="2">             <TR>                  <TD>Name</TD>                  <TD>Mass</TD>                  <TD>Radius</TD>                  <TD>Day</TD>             </TR>             <xsl:for-each select="//PLANET">                 <TR>                     <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>                 </TR>              </xsl:for-each>          </TABLE>      </BODY>  </HTML> 

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 theyre only of limited utility. As you can see, you still need to know how to use XSL elements, and the fact that you cant use <xsl:template> has only made the job more difficult here. But you should know that simplified stylesheets exist, and theyre part of the XSLT specification.

Default Handling without an < xsl:stylesheet> Element

If an XSLT processor cant find the <xsl:stylesheet> element in a stylesheet, its supposed to treat the stylesheet as a simplified stylesheet.



Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

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