Push vs pull XSLT

I often have to explain or refer to this distinction in XSLT programming style, and I've wanted a sort of all-in-one index of useful writing on the matter.

  • The discussion I most often refer people to is "2. Getting started with XSLT and XPath (cont'd)" by XSL tutor extraordinaire Ken Holman of Crane Softwrights Ltd.. It's part of his long article "What is XSLT?". In this article subsection he starts by discussing the difference between "explicit" and "implicit" stylesheets. The latter are what the XSLT 1.0 spec calls "literal result element as root", and it's such a rare pattern in my observation that you might want to just skip to section 2.2.6, which is the relevant part for push vs. pull.
  • Dave Pawson's XSLT FAQ has one of the earliest attempts to gather notes on the push/pull distinction
  • R. Alexander Milowski has a nice slide set, "How Not to Use XSLT". There are some other nice nuggest here besides illustration fo the push/pull distinction.
  • Hack #51 in XML Hacks is "Write Push and Pull Stylesheets", and is a decent discussion of the topic.
  • I reluctantly include "XML for Data: XSL style sheets: push or pull?", by Kevin Williams in this round-up. I personally am heavily biased towards push-style XSLT (as are most of my XSLT expert colleagues) and Kevin seems to be heavily biased towards pull. I think his reasoning about the readability of pull is quite shallow, and might make sense to readers when dealing with simplistic transforms, but not with ones of more typical size and complexity.

Way back in 2001 or so I wrote my reasons for preferring push over pull. Reviewing what I wrote I think it still covers my point of view:

Pull is a bad idea from the didactic POV. If one wants people to learn how to generate HTML and other simple documents as quickly as possible, there is no doubt that most people with any background in the more popular computer languages would catch on to pull more quickly than push.

But it's a false simplicity. Pull is easy when the problem space is simple, as is the case with so many toy examples necessary when teaching beginners. But programming difficulty scales at an alarming rate with the complexity of the problem space. It doesn't take long to run into real-world examples where pull is nearly impossible to program correctly.

Push on the other hand, while for some people more difficult at first, is a much more powerful approach for solving complex problems. And in almost all cases it is less prone to defect and easier to maintain.

This is not functional programming bigotry for its own sake. Since the invasion of webmasters and amateurs of scripting, it is easy to forget that document processing is one of the most delicate areas of inquiry in computer science, and it has called for elegant solutions from Knuth's TeX to Clark & co's DSSSL, to XSLT. As Paul Tchistopolskii explained here. XSLT at its best is about pipes and filters. XSLT's weakest points are where this model breaks down.

Whether your favorite conceptual module is pipes and filters, tuple spaces, or just good ol' lambdas, a fundamental understanding of push techniques is essential if you want to ever do any serious development in XSLT. New arrivals to this field take short-cuts only to get lost later. From a purely practical point of view, I think it's important to teach apply-templates, modes and friends well before for-each, and bitchin' value-of tricks.

If anyone wants to incorporate this stuff into Wikipedia, XSLT FAQ, or whatever, go right ahead: as with almost everything on Copia, it's CC attribution-sharealike (main task this weekend is to actually assert that properly in the templates).

[Uche Ogbuji]

via Copia
2 responses
So after having a bear of a time trying to get a concise definition of the terms out of the links you posted, this is what I conclude:



&#8220;Pull&#8221; means the stylesheet looks like the result, containing loops and &#8220;procedure calls&#8221; where appropriate, and it &#8220;pulls&#8221; data from the input as needed. This is what&#8217;s called a &#8220;template language&#8221; most anywhere else, in the style of PHP or Template Toolkit or such.



&#8220;Push&#8221; means the stylesheet is written in a more event-driven fashion, using apply-templates to do let input trickle down to &#8220;handlers&#8221; which define appropriate reactions for particular things and are selected using matching. The resulting structure is dictated (largely) by the input document. It&#8217;s called &#8220;push&#8221; in the sense used with SAX or other callback <abbr title="Application Programmer&#8217;s Interface">API</abbr>s.



Does that sound about right?
Aristotle, sorry about the formatting of your message.  Let me know if you want me to tweak it for you.

I don't really like the "push" and "pull" terminology, but I think the difference in style is pretty clear (I think it's becomes clearer the more XSLT you deal with).  Probably everyone would describe the distinction differently, as you've proved with your (IMO unique) attempt.

I think that pull does often look more like the result for simple cases, but it's hard to maintain this similitude in more complex cases.

Overall, I'd say you have the right idea on the distinction.