XML.com: XML From the Inside Out

XML.comWebServices.XML.comO'Reilly Networkoreilly.com
  Articles | Weblogs | Newsletter | Safari Bookshelf
advertisement

Article:
 Push, Pull, Next!
Subject: other benefits from push style
Date: 2005-07-07 03:09:49
From: Avander_be

I agree with you guys ( Bob and M.Kay) that the power of a template base approach is often underestimated.


Another benefit of the push style is that your stylesheet is far more resilient to changes, the xml source drives you're stylesheet and subtile changes in the xml won't break your stylesheet, using a pull style will break it for sure...



Previous Message Previous Message   Next Message Next Message


Titles Only Titles Only Newest First
  • other benefits from push style
    2005-07-07 09:32:08 afettes [Reply]

    Hi Bob,


    First of all, this is an excellent article. For me personally this article would have been very helpful about 5 years ago! Unfortunately I've had to come across these rules by trial and error and some tutelage.


    Another great benefit of the push style of XSLT programming is on debugging the final application. You mentioned this briefly in your article ("If they just rewrote it with more template rules to handle the different source node types, this would be easy to fix."). However, I do not feel that you stressed this importance enough!


    Pull-style XSLT programming is an absolute horror to debug. Tracing the paths through a (pull) XSLT program can be a difficult task, and knowing where the problem occurs is a skill unto itself. Context problems can also occur when performing pull XSLT programming. Often it can be difficult to figure out what the context node is at a particular point in processing. Finally, in pull-style XSLT, any changes that you make could have serious side effects throughout the system. This is not what a good software engineer looks for in a programming practice.


    Push-style XSLT programming is quite the opposite. With numerous short, simple templates you gain a number of advantages: the program becomes easy to understand, finding the location of bugs becomes trivial, and knowing the context node is also trivial. Shorter templates that perform one (or few) tasks are immediately much more easy to understand and conceptualize in ones head. With well-crafted match attributes, knowing the exact context node at the time of processing should be a trivial matter. Finally, changes to the system have a very limited scope. Side effects are not common in systems developed in this fashion. If an error is occurring, it is generally a simple matter to find the offending template and make the appropriate adjustments. This solution of course scales very well.


    In my personal opinion, a mixture of the two styles is appropriate to most situations. A mixture of ~75% push and ~25% pull should suit any development. An example of this from your docbook example could look like this:


    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">


    <xsl:template match="book">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title><xsl:value-of select="book/title"/></title>
    </head>
    <body>
    <xsl:apply-templates select="title"/>
    <xsl:apply-templates select="para"/>
    </body>
    </html>
    </xsl:template>


    <xsl:template match="para">

    <xsl:apply-templates/>


    </xsl:template>


    <xsl:template match="title">
    <h1><xsl:apply-templates/></h1>
    </xsl:template>
    </xsl:stylesheet>


    This example would not work with multiple title elements followed by paragraphs, but it gives a good indication of the flexibility in program execution. Using the programming style (as in the match="book" template) I have found to be very beneficial at high levels in the XML document. Once you have delved down a level or two in the tree the pure push style tends to be more appropriate.


    One last comment: it is my understanding that XSLT processors (such as Saxon) have been optimized for template matching. I.e. <xsl:apply-templates match="para"/> would perform faster than <xsl:for-each select="para"></xsl:for-each>. Can anyone comment on this?


    Cheers,
    Alastair Fettes

    • other benefits from push style
      2005-07-08 04:12:03 ajwelch [Reply]


      Remeber that using a select attribute on the xsl:apply-templates element (eg <xsl:apply-templates select="..."/>) is in fact the pull style of processing - the only way to instigate push processing is with a no-select apply-templates (eg <xsl:apply-templates/>). The select attribute is driving the context node, not the document order of the source.


      Therefore, your example uses more pull processing than push.


      cheers
      andrew

      • other benefits from push style
        2005-07-09 00:16:53 afettes [Reply]

        Hi Andrew,


        The point of the example was a combination of push/pull styles. Both are visible in it. Also, it was just a simple example at best.


        Cheers,
        Alastair

Sponsored By:


Contact Us | Our Mission | Privacy Policy | Advertise With Us | | Submissions Guidelines
Copyright © 2008 O'Reilly Media, Inc. | (707) 827-7000 / (800) 998-9938