Understand the Anatomy of an XSLT Stylesheet

Get acquainted with the basic elements of an XSLT stylesheet.

You had a close brush with XSLT in the hack that discussed converting text to XML [Hack #18] , but you didn’t see much more than the stylesheet’s name. Here is the complete stylesheet, time.xsl :

<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   
<xsl:template match="Import">
 <xsl:apply-templates select="Row"/>
</xsl:template>
   
<xsl:template match="Row">
 <time timezone="{@timezone}">
  <xsl:copy-of select="hour|minute|second"/>
  <xsl:apply-templates select="atomic"/>
 </time>
</xsl:template>
   
<xsl:template match="atomic">
 <xsl:copy>
  <xsl:attribute name="signal">true</xsl:attribute>
 </xsl:copy>
</xsl:template>
   
</xsl:stylesheet>

The Document Element

An XSLT stylesheet has two possible document elements, stylesheet and transform (see line 1). Both have identical attributes; in fact, the only difference between the two is that they have different names, but stylesheet seems more popular with developers than transform.

The version attribute is required. Some XSLT processors support a value of 1.1 or 2.0, in support of those versions of the XSLT specs, but 1.0 is the most commonly used value. (Version 1.1 of XSLT is not and will never be a W3C recommendation, so any support for it is processor-dependent.) The namespace declaration is also required if you want an XSLT processor to recognize the XSLT markup. The namespace name for XSLT 1.0 is http://www.w3.org/1999/XSL/Transform ...

Get XML Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.