Advantages of push-style XSLT over pull-style

2008-11-25 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

My new look on old characters

2007-08-30 2 min read Fonts Typography Eddie

I don’t know much about typography. I find the subject absolutely interesting, especially from a historical perspective, but I think I am slightly intimidated by it too. It has thousands of years of history, and more than a few books I have read tell me that “the rules in use today were perfected hundreds of years ago.” I imagine a disciple of Gutenburg, wearing funny little shoes, leaning over and whispering, “yea, hundreds of years… don’t mess it up!” into my ear.

So I try not to mess it up. I take it slowly. I’ve read a few books. I’ve ordered what I hear is the typography bible, but it deserves my undivided attention, so it is still sitting sealed in cellophane on my shelves. The things that I don’t know about typography could undoubtedly fill many, many shelves. That’s why it is always fun to learn something new. And I did today, reading one of the most interesting posts I have seen in a while.

I won’t repeat it as you can read it yourself. However, in this post Mrs. Simmons mentions how common type may be considered almost as a means for wiping out a local dialect. I find that intriguing alone. She goes on to argue, however, that the common symbols further enhance the language of design. Knowing only what I currently do on the subject, I both see her point, and find the thought eye-opening. It is yet another facet of the work that I do day-in-day-out but know virtually nothing about. It is amazing how every large scale object is made up from small atom-like parts. Tomorrow I will go in and look at a word, a sentence, a paragraph, or even a single letter in a new way. Of course, this means I am going to have to unwrap that book sooner than anticipated. But this is useful information, I’ve already thought of an instance where I can apply this new knowledge. But I’m not sharing… I don’t want to mess anything up.

Anyway, thanks to Amber Simmons, who made me think of things differently today, and probably tomorrow too.