O'Reilly logo

XML Hacks by Michael Fitzgerald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Write Push and Pull Stylesheets

Understand the difference between push and pull XSLT stylesheets, and when to use which.

If you spend any time with XSLT, you will often hear or read about push stylesheets and pull stylesheets. This hack explains what push and pull stylesheets are and how to use them.

A pull stylesheet is one that usually has only one template, and it uses XSLT instructions like value-of or for-each to pull nodes from the source document it is processing. It then arranges the pulled nodes in the order presented in the template. The timetext.xsl stylesheet [Hack #50] is an example of a pull stylesheet. It has only one template, whose content uses four value-of instructions to arrange the nodes in a result tree (Example 3-40).

Example 3-40. timetext.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
   
<xsl:template match="time">Time: <xsl:value-of select="hour"/
>:<xsl:value-of select="minute"/>:<xsl:value-of select="second"/>
<xsl:text> </xsl:text><xsl:value-of select="meridiem"/>
</xsl:template>
   
</xsl:stylesheet>

A pull stylesheet is appropriate when you are fairly certain of what your source document will look like. It’s also a good idea if the structure of the result drives the processing; i.e., if you just want to pick certain information out of the source document and place it in the result.

A push stylesheet takes a different approach. Push stylesheets have any number of templates that contain apply-templates ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required