Here's a complete stylesheet.
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="list">
<html>
<head>
<title>My Mom's Recipes</title>
</head>
<body>
<h2>Why my mom rules</h2>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="recipe">
<p></p><b>Recipe Name:</b> <xsl:value-of select="@name" /><br/>
<em>Course:</em> <xsl:value-of select="course" />
<xsl:apply-templates />
</xsl:template>
<xsl:template match="ingredients">
<xsl:for-each select="item">
<xsl:value-of select="@measurement"/> <xsl:value-of select="@unit"/>
<xsl:value-of/><br/>
</xsl:for-each>
</xsl:template>
<xsl:template match="directions">
<ol>
<xsl:for-each select="step">
<li><xsl:value-of/></li>
</xsl:for-each>
</ol>
</xsl:template>
</xsl:stylesheet>
The result isn't going to win me any design awards, but the XSLT is functional. Let's look at portions of this page in a bit more detail
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
The "/" in the first line tells the processor that this node applies to the root level of the XML document. Think of the root level as an imaginary pair of tags surrounding the entirety of your XML document which must be addressed before you can get to the actual tags. The apply-templates tag tells the processor to look at everything occurs beneath the current level. In this case it means "examine everything." You'll use these commands in pretty much every XSLT document
<xsl:template match="ingredients">
<xsl:for-each select="item">
<xsl:value-of select="@measurement"/> <xsl:value-of select="@unit"/>
<xsl:value-of/><br/>
</xsl:for-each>
</xsl:template>
Notice that the syntax within the <xsl:value-of> tags
varies depending on what I'm trying to get at. If I want to print the value
of the current node, I use a simple <xsl:value-of />. If I
need the value of an attribute, I use the @ symbol, and if I want a
value somewhere else within the XML source document, I use slashes to
get to the exact element. As you might imagine, addressing specific
elements can get pretty involved. In fact, there's a addressing language
called XPath, which
you'll need to get familiar with if you want to use XSLT.
<xsl:template match="directions">
<ol>
<xsl:for-each select="step">
<li><xsl:value-of/></li>
</xsl:for-each>
</ol>
</xsl:template>
Here we see one of XSLT's several programming-like features. There are also if-then and choose functions. These features allow anyone to manipulate XML content in any way imaginable (or at least practical) very easily.
Other than that, there's not a whole lot to look at in this example. As one would expect, the stylesheet opens and closes with matched tags, and in between there's a set of well-formed units. We've added HTML tags in this example, but were by no means limited to that. You could add whatever XML-compliant markup you see fit.
next page»