Advantages of push-style XSLT over pull-style

November 25, 2008 3 min read Programming Xslt Eddie

Working with more than a few new-hires over the last few weeks, I’ve noticed that new XSLT developers often write pull-style XSLTs by default. However, this tends to defy XSLT’s functional heritage, and is not as useful as the opposite form, push-style XSLTs.

Pull-style XSLTs reach into the source document and pull out the data they need to transform. The pull-style is similar to template systems like those found in Rails or Django, or inserting PHP commands between HTML elements. For example, given the trivial input:

<?xml version="1.0" encoding="UTF-8"?>
<books>
    <book>
        <title>The Scheme Programming Language</title>
        <author>R. Kent Dybvig</author>
    </book>
    <book>
        <title>Essentials of Programming Languages</title>
        <author>Daniel P. Friedman</author>
    </book>
    <book>
        <title>An Introduction to Information Theory</title>
        <author>John R. Pierce</author>
    </book>
</books>

an XSLT novice will produce a stylesheet like the following (note lines 11 and 12 which reach into the source and grab the data):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <html>
            <head>
                <title>books</title>
            </head>
            <body>
                <dl>
                    <xsl:for-each select="books/book">
                        <dt><xsl:value-of select="title"/></dt>
                        <dd><xsl:value-of select="author"/></dd>
                    </xsl:for-each>
                </dl>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

which transforms into:

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <title>books</title>
   </head>
   <body>
      <dl>
         <dt>The Scheme Programming Language</dt>
         <dd>R. Kent Dybvig</dd>
         <dt>Essentials of Programming Languages</dt>
         <dd>Daniel P. Friedman</dd>
         <dt>An Introduction to Information Theory</dt>
         <dd>John R. Pierce</dd>
      </dl>
   </body>
</html>

The real power of XSLT, however, is defining templates for the elements found within the source document. These are push-style XSLTs. They have two main advantages. First, push-style gracefully handles complex source structures, including recursively nested elements. It would be near impossible to handle the following source document using pull-style,

<pre lang="xml">
<div><div><div>a</div></div></div>

if you didn’t know how deep the recursive divs would go. A push-style solution, though, is incredibly simple.

<pre lang="xml">
<template match="div">
     * <apply-templates></apply-templates> *
</template>

Will transform the previous source into the following.

* * * a * * *

In addition to handling complex source structures, push-style allows code reuse. This is of course an ideal of any programming language. Push-style XSLTs have a greater ability to be reused, because the individual templates can be reused. When you only have one template, it is quite difficult to make it general without resorting to numerous choose-when statements. Here is an example of code reuse, where we extend a previously written template with the xsl:apply-imports rule.

Given the input,

<images>
    <image>
        <url>http://www.filmjunkie.com/drinks/blixa/blixa.jpg</url>
        <alt>Blixa!</alt>
    </image>
</images>

and the XSLTs,

    <xsl:import href="imageformat.xsl"/>
 
    <xsl:template match="image">
        <div class="wrapper">
            <xsl:apply-imports/>
        </div>
    </xsl:template>

and the rule in “imageformat.xsl” (the template being extended in this case),

Continue reading

XSLT Class

October 23, 2007 3 min read Xslt Eddie

Last Monday through last Wednesday I attended Mulberry Technologies’ XSLT course. I had a fun time, and learned a bunch.

To prepare, I looked through a few books. I looked through Jeni Tennison’s Beginning XSLT 2.0 book, but it wasn’t for me. I didn’t like how wordy it was… I got lost in the dialogue, and it distracted me from the points and the examples. On the otherhand, Doug Tidwell’s book, XSLT-Mastering XML Tranformations was precisely what I was looking for. It was concise and organized very methodically. PLUS, it only cost me $7 with shipping from Amazon. Heh, gotta love that!

[note: because we only use 1.0 on the job, older books were better, as I didn’t have to figure out what was a 2.0 feature and what was a 1.0 feature… and again, the books were cheaper]

Fun stuff aside, XSLT is a pretty cool language. I’m too new to it to debate if it is a functional programming language or simply based on those attributes, but I’m happy to get to play with its functional aspects. I am kinda thrilled to dust off my recursive-function-writing skills. I hope to use it early and often, so I can get a good amount of practice in.

The class was useful because it showed me things I didn’t pick up from the books. First was the concept that the root node is placed above what I called the XML root node. The class called the XML root node the “document node.” Solved one problem that I had been having. The other really enlightening aspect was the axes. When I read the book and saw that there 13, I simply skimmed it. It was good to sit down with , as the following:: and preceding:: axes were not what I thought.

Debbie Lapeyre and Wendell Piez, the instructors, were great and worked with us to answer all of our questions… even my weird set theory questions. Wendell, in particular (because he led the third day) was helpful, showing me some of the common “hacks” people use to make 1.0 do things it wasn’t intended to do [despite my incredibly poorly phrased question!]. Now that I can understand XPath a little better, I can break down the pieces of the hacks… so things like the Muenchian method actually make sense now.

Hopefully I will get to keep working with XSLT’s, I’m definitely not an expert from three days. But since they’re core to the portal system I work with, I expect to get a lot more practice in. If I get good enough, I’d love to post some information that may be useful to others. Stay tuned!

Continue reading