You do like XSLT? Use it proper!

One of the most annoying behaviours for a programmer is when someone is using a

language or a method which he isn’t familiar and he directly finds out it’s done in a ugly way. So don’t leave the trash behind you for maintainers bu write proper, nice solutions. Some examples for XSLT, simple stuff to do, but common forgotten are listed below:

  1. xsl:output should be configured correctly
    By default, on creation, the xsL:ouput line, look like this:
    <xsl:output method=”html” indent=”no” encoding=”UTF-8″ />
    When building a valid xhtml you should change the output-method to xml. And do not forget to change the encoding when you create chinese or vietnamese sites ;-). But when you change the encoding, do not forget the encoding in the first line(<?xml ?>)…
    For western sites, this following output line is the most likeliest:
    <xsl:output method=”xml” indent=”no” encoding=”iso-8859-1″ />
  2. Remove unused variables
    By default Sitecore delivers the ‘$home’. When it is unused, remove it. Same for unused parameters. Altough the 4 default Sitecore parameters can be left  there.
  3. Avoid comments in templates
    As templates are most of the time already full with advanced presentation logic, tests, etc, leave comments out of it. Just describe your template above the method.
  4. Avoid templates longer then 10-12 lines
    Xslt is itself more then complex. Guess nearly everybody agrees on that vision. But, as you are able to read here, in some circumstances, it is definitelly usefull. Still, and this one also applies to C#/VB  methods, try to create simple templates, with the simpel guideline that between 10 and 12 lines are the absolute maximum for a template.

Hope this will help you guys a little while using,debugging, documenting and maintaining XSLT-renderings.

2 thoughts on “You do like XSLT? Use it proper!”

  1. I really don’t like bloated xslt, but most of all it’s the logic bloat.

    Some really tricky xslt combos are trivial to do in .net, so I’d say use extensions object where applicable.

  2. First I love XSLT.
    1. Agree, I think Sitecore should do xml output in default template. But I always use UTF-8 insted country specific encoding. Once I had problems with dtSearch (Sitecore 4.1) when I didn’t use UTF-8.
    2. I always make my own default template with global variables to labels and frontpage or other.
    3. I use comments, comments are good.
    4. I don’t really agree on having to write 10-20 lines code in XSL, just write comments 🙂

    Personally I don’t like web controls/user controls so I use code behind only for building XSL helpers. All my presentation is in XSLT’s. I really like XSL but that is probably because I come from different perspective than you. Before I discover XML I worked as a designer and therefore I found XSL great language of templating. I didn’t approach it as a programmer. I do agree if you want to do “programming” in XSL it is a lousy language for that.

Comments are closed.