Streamlined XSLT

Saturday 20 September 2003This is 21 years old. Be careful.

All About Symbian shows a neat trick: using an XSLT stylesheet to style an RSS feed for those that simply click their RSS feed. Very nice, and I may do the same here eventually.

But looking at the stylesheet itself, it looks like they’ve fallen into a common pitfall: writing unusually verbose XSLT.

The same thing happened to me a few years ago at work. A friend asked why the heck XSLT was so great, if it took so much to do so little? I asked to see the XSLT. It looked similar to this snippet of All About Symbian’s stylesheet:

<xsl:element name="form">
  <xsl:attribute name="action"><xsl:value-of select="rss:link"/></xsl:attribute>
  <xsl:attribute name="method">POST</xsl:attribute>
  <center><div class="textinput-form">
    <xsl:element name="input">
      <xsl:attribute name="name"><xsl:value-of select="rss:name"/></xsl:attribute>
      <xsl:attribute name="type">text</xsl:attribute>
    </xsl:element>
    <xsl:text> </xsl:text>
    <xsl:element name="input">
      <xsl:attribute name="value"><xsl:value-of select="rss:title"/></xsl:attribute>
      <xsl:attribute name="type">submit</xsl:attribute>
    </xsl:element>
  </div></center>
</xsl:element>

Why all this rigamarole? You never need to use <xsl:element> unless you are going to compute the name of the element. You never need to use <xsl:attribute> unless you are computing the name of the attribute, or the text of the attribute is some large complex template call or something. For simple cases (as we have here), the whole thing can be drastically simplified:

<form action='{rss:link}' method='POST'>
  <center><div class='textinput-form'>
    <input name='{rss:name}' type='text'/>
    <xsl:text> </xsl:text>
    <input value='{rss:title}' type='submit'/>
  </div></center>
</form>

Now you can read the code, and see what it does.

Comments

[gravatar]
Bloody hell, that's excellent. I'm tattooing that on my eyelids. Why doesn't it say that in massive writing everywhere XSLT is mentioned? I've just written a complicated stylesheet (complicated for me, that is, in that it does a lot of transforming, not that it does actual complex XSL) which uses xsl:attribute all over the place...
[gravatar]
Hell! That first poster is completely right! I'm making my appointment to the tatoo parlor as well. Super great tip, thanks! Hell, that's more than a tip that's a slap upside the head, "Hey dummy. Learn the freakin' spec!" which is even better. :-)

-Russ
[gravatar]
Damn. This is an eye-opener.

I think the problem is all the sample XSLT code I've found is written in such a horrendously verbose manner, and you know what they say -- monkey-see, monkey-do.
[gravatar]
This RSS XSLT thing excites me. Sexually.
[gravatar]

I blame some of the XSLT tutorials out there that demonstrate the "you can create a tag on the fly using xsl:element!"

Jeni Tennison's has several wise things to say about writing XSLT, especially on the use of Named Templates (avoid them in favor of the mode attribute where possible.)

And Sean, you scare me.

[gravatar]
building hyperlinks is fun. like to serve my rss feeds as tables in xhtml. i'm with sean
[gravatar]
It has been 2 years and some months since you posted this, and I really wish I would've read this before. Thanks!

Add a comment:

Ignore this:
Leave this empty:
Name is required. Either email or web are required. Email won't be displayed and I won't spam you. Your web site won't be indexed by search engines.
Don't put anything here:
Leave this empty:
Comment text is Markdown.