Pushing and Pulling
Push, pull? What's that about?
Push and pull are two different ways to develop with XSLT. So what's the difference and is one way better than the other?
To show the difference between push and and pull, we'll try to transform some XML into HTML.
Here's some XML for William Shakespare's The Tragedy of Hamlet, Prince of Denmark, Act 3, Scene 1. That's where Hamlet gets to do his "To be, or not to be..." bit.
A play has a title and acts.
Acts have a title and scenes.
Scenes have a title, speeches and stage instructions.
Speeches have a character, lines and can have stage instructions too.
Lines can have stage instructions too.
So, to push, or not to push: that is the question.
A pull-style XSLT stylesheet pulls nodes from the source XML, often with <xsl:for-each> and <xsl:value-of> to generate output. A pull stylesheet may only have one template that tries to do everything.
Let's try this pull-style XSLT stylesheet on our XML here.
And this is how the HTML appears after transforming the XML with this pull-style XSLT stylesheet.
A push-style XSLT stylesheet has instructions <xsl:apply-templates select="pattern"> to push selected nodes to matching template rules <xsl:template match="pattern">
Let's try this push-style XSLT stylesheet on our XML here.
And this is how the HTML appears after transforming the XML with this push-style XSLT stylesheet.