Home > Articles > Web Services > XML

  • Print
  • + Share This
From the author of

Multiple Templates

So far, we've been styling entire sections, but we can also use templates to style individual items within sections:

...
<xsl:template match="content/blurb">
  <div class="blurb">
   <xsl:value-of select="." />
  </div>
</xsl:template>

<xsl:template match="content/gossip">
  <h3 class="headline">The Unknown Gossip Columnist Rides Again</h3>
  <xsl:for-each select="teaser">
   <xsl:apply-templates />
   <hr />
  </xsl:for-each>
</xsl:template>

<xsl:template match="movie">"<xsl:value-of select="." />"</xsl:template>

<xsl:template match="tvshow"><i><xsl:value-of select="." /></i></xsl:template>

<xsl:template match="person"><b><xsl:value-of select="." /></b></xsl:template>

<xsl:template match="content/admin">
  <div class="admin">
   <xsl:value-of select="." />
  </div>
</xsl:template>
...

Notice two new techniques in this section. First, we are explicitly looping through each of the teaser child elements of the gossip element. Each time the loop executes, the context node is set to be that specific teaser element.

Within the loop, the xsl:apply-templates element sends the children of the teaser node off in search of other templates. These children include the movie, tvshow, and person elements, which have their own templates, as well as individual text nodes that are simply output to the page by the built-in templates. The results are shown in Figure 6 .

Figure 6 The xsl:apply-templates element allows more than one template to process content.

  • + Share This
  • 🔖 Save To Your Account