Hack 51 Write Push and Pull Stylesheets

   

figs/moderate.gif figs/hack51.gif

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 instructions. The order of nodes in the output is determined by the order in which they are discovered in the source. mysqldump.xsl is an example of a push stylesheet [Hack #46] . This stylesheet (shown in Example 3-41) has a template for every element node it may encounter in the source document dumped from a MySQL database using mysqldump (for example horses.xml).

Example 3-41. mysqldump.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" encoding="UTF-8"/>     <xsl:template match="mysqldump">  <xsl:apply-templates select="database"/> </xsl:template>     <xsl:template match="database">  <xsl:element name="{@name}">   <xsl:apply-templates select="table"/>  </xsl:element> </xsl:template>     <xsl:template match="table">   <xsl:apply-templates select="row"/> </xsl:template>     <xsl:template match="row">  <xsl:element name="{../@name}">   <xsl:apply-templates select="field"/>  </xsl:element> </xsl:template>     <xsl:template match="field">  <xsl:element name="{@name}">   <xsl:value-of select="."/>  </xsl:element> </xsl:template>     </xsl:stylesheet>

A stylesheet may have a template for nodes that it does not encounter in a source document with little consequence that is, nothing happens and the XSLT processor just keeps rolling along. Push stylesheets may be most appropriate when you are not exactly sure if all the anticipated elements will be in your source document.



XML Hacks
XML Hacks: 100 Industrial-Strength Tips and Tools
ISBN: 0596007116
EAN: 2147483647
Year: 2006
Pages: 156

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net