Search:
Locator+ Code:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Back to VSLive! Show Daily Home


Master XSLT Template Processing
Understanding how the XSLT processor applies templates is key to designing your templates and their match patterns.

by Yasser Shohoud

VSLive! SF, Day 1, February 12, 2002 — You can think of templates as functions that you call to perform a specific function. Like functions, templates can have parameters and return values. However, you don't always call templates directly. Instead, you may specify certain input for which a template should be called and executed. For example, the XSLT processor will call and execute the following template when it encounters a <data> element in the input:

<xsl:template name="formatData"
    match="data">
<html>
   <body>
      <p><b><xsl:value-
	     of select="text()"/></b></p>
   </body>
</html>
</xsl:template>

Alternatively, you could call this template explicitly from another template using the template name like this:

<xsl:call-
template name="formatData"/>

You use XSLT patterns to specify the input pattern that will cause a template to be executed. An XSLT pattern is a subset of the XML Path language, which I'll discuss later. For now, think of patterns as a way to specify the input node in which you're interested using a hierarchical representation of input document. The simple hello world document—repeated below for your convenience—can be represented as a tree. Each tree level is represented with a forward slash in XSLT patterns. So "/" represents the document root while "/data" represents the data element that's a child of the document's root. Similarly, /data/text() represents the text node that is a child of the data element that is a child of the document's root.

To write a template that matches the data element, you can use the XSLT pattern "data" or "/data". To be effective at writing match patterns, you need to understand how the XSLT processor matches and applies templates. The processor starts by reading the first node from the input document, which will always be the document's root. This node now becomes what's known as the current node. The processor then goes through the stylesheet's templates examining their match attributes and trying to find the template that matches the input node.

In the example stylesheet, there isn't a template with match="/", i.e. there's no template that matches the document's root. Upon failure to locate a template that matches the current input node, the XSLT processor will invoke the built-in template. This is a default template that provides basic processing depending on input type. The template's behavior depends on the current node's type. For text nodes, it'll simply output the node's content. Similarly, for attribute nodes, it'll output the attribute value.

Back to top





Java Pro | .NET Magazine | Visual Studio Magazine | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home