The Pull Model with XSLT
In a pull model stylesheet, the XML source document acts
purely as a data source and its structure is largely irrelevant. The stylesheet
itself provides the structure of
the output document.
Let's look at a simple example based on the full source of
the play, Hamlet.xml,
which is provided in the code download for the book. Here is our stylesheet, count.xsl:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template
match="PLAY">
<HTML>
<HEAD>
<TITLE>Counting</TITLE>
</HEAD>
<BODY>
<P>There are
<xsl:value-of
select="count(//PERSONA)"/> individual
characters in
Hamlet.</P>
<P>
<xsl:for-each
select="ACT">
<xsl:value-of
select="TITLE"/> has <xsl:value-of
select="count(SCENE)"/>
scenes,
</xsl:for-each>
making a total of
<xsl:value-of
select="count(//SCENE)"/>.
</P>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
This is the result of applying the stylesheet to the play
(for example, by using XT to apply the stylesheet to Hamlet.xml
and produce the HTML result):
This bears little resemblance to anything in the play.
Instead, the stylesheet has provided the structure of the output, pulling in
data as it is needed. You will also notice the use of the built-in count()
function in this example. We will meet this later when we look at the elements
and functions in detail.
The pull model is
characterized by a few large templates and use of the <xsl:value-of> element so that the stylesheet controls the order
of items in the output. Compare this to the push model, where we had more
and smaller templates with the output largely following the structure of the
XML source document.
I mentioned earlier that XSLT is often thought of as a
declarative language. However, it also contains the flow control and looping
instructions typical of a procedural language. Typically, a push model
stylesheet emphasizes the declarative aspects of the language, while the pull
model
emphasizes the procedural aspects.
Of course, these definitions are not absolute. Most
stylesheets
will contain elements of the push and elements of the pull models. However, it
is useful to keep the two models in mind as it can make your stylesheet
development simpler.
We have now looked at how XSLT stylesheets are created and
seen examples of both push and pull models. In doing this, we have come across
several of the XSLT elements and their attributes, and the built-in count()
function. We will look at these and other elements in more detail, shortly.