O'Reilly logo

XML Hacks by Michael Fitzgerald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required