xslt apply templates

When you use <xsl:apply-templates/>, it is really short for <xsl:apply-templates select="node()"/> and is being invoked inside of a template match on /, which is the root node of the document and an abstract concept for the top of the file. It’s children include the document element (i.e. <Page>), as well as any top-level comments or processing instructions that may be siblings of the document element.

<xsl:apply-templates> takes a sequence of nodes and goes through them one by one. For each, it locates the template with the highest priority that matches the node, and invokes it. So <xsl:apply-templates> is like a <xsl:for-each> with an <xsl:choose> inside, but more modular.

In contrast, <xsl:call-template> invokes a template by name. There’s no change to the context node (no <xsl:for-each>) and no choice about which template to use.

Templates are applied according to its priority, which range from -0.5 to 0.5.

Templates with more specific xpath expression has high priority.

  • Anything that’s repeated should be in a template (obviously)
  • Path matching should be matchable by a string search. apply-templates and template match should at least look similar. This becomes essential the larger your template becomes.
An XML document

<?xml version="1.0"?>
<db:article xmlns:db="http://ananas.org/2002/docbook/subset">
<db:title>XSLT, JSP and PHP</db:title>
<db:section>
<db:title>Is there a difference?</db:title>
<db:para>Yes there is! XSLT is a pure XML technology that
traces its roots to <db:emphasis>tree manipulation
algorithms</db:emphasis>. JSP and PHP offer an ingenious
solution to combine scripting languages with HTML/XML
tagging.</db:para>
<db:para>The difference may not be obvious when you're first
learning XSLT (after all, it offers tags and instructions),
but understanding the difference will make you a
<db:emphasis role="bold">stronger and better</db:emphasis>
developer.</db:para>
</db:section>
<db:section>
<db:title>How do I learn the difference?</db:title>
<db:para>Interestingly enough, you can code the XSLT algorithm
in XSLT... one cool way to experiment with the
difference.</db:para>
</db:section>
</db:article>

A simple stylesheet for HTML publishing

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:db="http://ananas.org/2002/docbook/subset">

<xsl:output method="html"/>

<xsl:template match="db:article">
   <html>
      <head><title>
          <xsl:value-of select="db:articleinfo/db:title"/>
      </title></head>
      <body>
         <xsl:apply-templates/>
      </body>
   </html>
</xsl:template>

<xsl:template match="db:para">
   <p><xsl:apply-templates/></p>
</xsl:template>

<xsl:template match="db:ulink">
   <a href="{@url}"><xsl:apply-templates/></a>
</xsl:template>

<xsl:template match="db:article/db:title">
   <h1><xsl:apply-templates/></h1>
</xsl:template>

<xsl:template match="db:title">
   <h2><xsl:apply-templates/></h2>
</xsl:template>

<xsl:template match="db:emphasis[@role='bold']">
   <b><xsl:apply-templates/></b>
</xsl:template>

<xsl:template match="db:emphasis">
   <i><xsl:apply-templates/></i>
</xsl:template>

</xsl:stylesheet>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s