Erich Schubert: When XSLT gets really messy...
XSLT is an outright ugly language. Thats why I often just hate it.
In some (rare) cases it's very elegant, but most often incredibly clumsy.
I've been redoing my homepage completely, and I'm using XSLT to render my
data XML files to the XHTML output. That works quite well so far, but I hit
more and more special cases.
First I wanted to add a language chooser. This required finding out which
lanuages my document supports (read: blocks with that language attribute exist)
and check for uniqueness.
Some things where rather easy to solve (with some experience in XSLT), so
where extremely ugly.
First of all, I want my template file to be a valid XHTML file, I don't want
to have any layout parts in XSLT. Then I have a separate sitemap, needed for
generating the menu. So I have to process three documents at the same time
in XSLT. Ouch. But doable, using variables.
Basically I have three modes - in one mode, the template is mostly copied
over, unless I've specified some special rule, e.g. to insert the page
title, meta information or content. A second mode uses the current chunk of
the template document as template for the contents a couple of times to
iterate over data chunks from the data source file (read: put text blocks
from my source files into the output template format). The third mode does
the same for the sitemap.
Unfortunately, it started to get even messier...
Big problem number one: Make a list of the available languages for the current
page. Make this information reuseable throughout the page transformation.
Part one: make a list of available languages by finding unique (!) @lang
attributes.
(usual approach: grab all nodes with @lang attributes, check if they have the
same node id like the first occurence with this @lang value for uniquness,
then output. Ugly as hell, but string magic sucks as much.)
Big issue number two: Assume we now have a variable with en in
there, access these nodes to transform them. Unfortunately, the generated nodes
are "result nodes", and you cannot iterate over them with for-each. Luckily
the exslt node-set extension to solve this.
But now came the third big issue along... this is where I give up.
I want my pages to have a nice modification date... my data pages are stored
in subversion... Subversion has keywords expansion like CVS, which I can use
nicely to automatically update the last modification date of the file.
Unfortunately, the date will look like this:
$Date: 2006-02-15 21:29:10 +0100 (Mi, 15 Feb 2006) $
Whereas in a web page meta information it's recommended to use ISO8601.
Now if I only had regular expressions and proper date handling functions...
I guess I'll stop writing ugly, hackish XSLT code and chose pretty Python
code instead.
Maybe use TAL or
KID for XMLish templating...