XML.com's "Python and XML" columnist Uche Ogbuji provided a nice collection of links to discussions about the push vs. pull styles of XSLT stylesheet development. What do we mean by "push" and "pull"? As a short example of each, let's look at two approaches to converting the following DocBook document to XHTML:
<book>
<title>Beneath the Underdog</title>
<para>In other words, I am three.</para>
<para>"Which one is real?"</para>
<para>"They're all real."</para>
</book>
The first stylesheet below takes a push approach. The XSLT processor "pushes" the source tree nodes through the stylesheet, which has template rules to handle various kinds of nodes as they come through:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="book">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><xsl:value-of select="book/title"/></title>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="para">
<xsl:apply-templates/></p>
</xsl:template>
<xsl:template match="title">
<h1><xsl:apply-templates/></h1>
</xsl:template>
</xsl:stylesheet>
Each xsl:apply-templates instruction is the stylesheet's way of telling the XSLT processor to send along the context node's child nodes to the stylesheet's relevant template rules. (Or, to quote Curtis Mayfield, "Keep On Pushing.")
A pull-style stylesheet minimizes the use of xsl:apply-template instructions. It uses instructions such as xsl:value-of and xsl:for-each to retrieve the nodes it wants and then puts them where it needs them, like this:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><xsl:value-of select="book/title"/></title>
</head>
<body>
<h1><xsl:value-of select="book/title"/></h1>
<xsl:for-each select="book/para">
<xsl:value-of select="."/></p>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Related Reading
XSLT 1.0 Pocket Reference
By Evan Lenz
Few stylesheets rely strictly on push or pull processing. For example, while my first stylesheet above includes a template rule to convert the title element into an h1 element when it comes along, it needs to explicitly go get the title value to plug it into the XHTML head element's title element. The use of such a simple source document also made the pull example a little too clean and simple; in the real third paragraph of the book Beneath the Underdog, the word "all" is emphasized, and a pure pull style approach to handling in-line content would require contortions that doubled the length of the stylesheet.
The pull style can feel more natural to developers intimidated by XSLT's roots in the functional programming style used by its ancestors DSSSL, Scheme, and LISP. A pure pull stylesheet like the one above has one template rule that tells the XSLT processor, "When you find the root node of the source document, do this, then do this, then do this, then do this..." It's a series of steps to perform, as with a typical declarative programming language. Other template rules in such a stylesheet are usually named template rules—that is, template rules with name attributes that get explicitly called with xsl:call-template instructions instead of being called when the XSLT processor finds a node matching the condition described in the template rule's match attribute. (An xsl:template element can have both a match attribute and a name attribute, but typical template rules have one or the other.) These named template rules play the role of the subroutines or procedures of a procedural programming language, adding modularity to a growing program the old-fashioned way.
___________________________________
Web Design Experts
Pay Per Click Affiliate Program