How to cook tag soup with XSLT
Wednesday, 17. September 2008, 14:51:57
The XSLT markup/programming language is widely used to transform one sort of DOM into another - for example turning the DOM of a generic XML file into valid XHTML. Much of the benefit is that you're working on DOM trees - making it hard or impossible to create syntactically invalid pages.
Diving into the source code shows that the JavaScript coders working on the Rail site were asleep during their education's "what's the point of XSLT" lesson. The coding is unbelievable. It's more like an XML parser/serializer stress test than a production site. Now, I don't really know XSLT and trying to debug this confirms my impression that it must be one of the worse programming languages mankind has invented - but the point of this script is to generate HTML with XSLT *string concatenation*?!?? Look at this:
<xsl:value-of select="$attribute-name"/>="<xsl:call-template name="inner-attribute-text-value"><xsl:with-param name="attribute-value" select="$attribute-value"/></xsl:call-template>"
or
<xsl:template name="inner-text-tag-open"><xsl:text disable-output-escaping="yes"><</xsl:text></xsl:template> <xsl:template name="inner-text-element-close"> <xsl:param name="element-name"/><xsl:call-template name="inner-text-tag-open"/>/<xsl:value-of select="$element-name"/><xsl:call-template name="inner-text-tag-close"/></xsl:template> <xsl:template name="inner-text-tag-close"><xsl:text disable-output-escaping="yes">></xsl:text></xsl:template>
Yes, all that to create a text node containing e.g.
</div>in a DOM they will serialize only to parse it again by setting innerHTML on some poor element..
When they in their wisdom chose to generate markup inside text nodes with their XSLT they run into the familiar problem: when is < going to start a tag and when is it going to live in a text node? Hence, < is sometimes escaped as an 'lt' entity to create proper text nodes with HTML source-as-text in them (see for example the instance of
<in the code above). Now, of course when they set innerHTML they do not want this entity to appear as a literal < so they do some pre-processing: all entities they want to change into proper < and > before setting innerHTML have a comment node next to them:
<!--nwlt--><TR class="nw-2r"><!--nwgt--><!--nwlt--><TD class="nw-2c"><!--nwgt-->
and their pre-processing is a simple string replace:
sHtml = sHtml.replace(/\<!--nwlt--\></g,"<").replace(/>\<!--nwgt--\>/g,">").replace(/\<[\/]?tbody\>/gi,"");
(Why they hate the poor TBODY so much they must strip it from the markup even though the browser will re-generate them in the DOM as soon as innerHTML is parsed I can't even begin to imagine.)
If you thought XML-based toolchains and processes were going to make the Web a saner place, think again. We have now seen that in the right hands, XSLT is just another recipe for tag soup.










hallvors # 17. September 2008, 14:54
This story is also sent to the Daily WFT. We'll see if it is found worthy
Haruka aka Seremel # 17. September 2008, 17:39
Well, it's not *that* bad language, it's just you'll need to make some things outside of XSLT. I use it client-side on my static site, though it's kinda hard for now — IE don't understand real XHTML (using xsl:copy-of + xhtml in proper namespace = garbage), Fx don't understand disable-output-escaping attribute, so...
Hades32 # 17. September 2008, 18:44
theoddbod # 17. September 2008, 21:27
fearphage # 18. September 2008, 00:20
xErath # 18. September 2008, 07:42
johnnysaucepn # 18. September 2008, 08:41
XenoFur # 19. September 2008, 12:55
johnnysaucepn # 19. September 2008, 13:19
Originally posted by XenoFur:
Really? Looks fine to me?
XenoFur # 19. September 2008, 13:48
Haruka aka Seremel # 19. September 2008, 13:50
johnnysaucepn # 19. September 2008, 15:07
Haruka aka Seremel # 19. September 2008, 15:14
nickfitz # 19. September 2008, 17:34
XSLT is a wonderful language once you understand the principles of declarative programming.
Not only do these bozos lack such understanding, they appear to have not the faintest grasp of any part of the language whatsoever
I've worked with XSLT since it first appeared, I've been active on the XSLT mailing list, I've helped other people in client companies come to terms with it - but I can honestly say that I have never seen anything as appalling as this. Many thanks for sharing it - I now have the definitive example of how not to use the language
XenoFur # 19. September 2008, 18:37
Haruka aka Seremel # 19. September 2008, 18:58
Content Model
RSS 2.0 may contain either plain text or escaped HTML as a payload, with no way to indicate which of the two is provided. Atom, on the other hand, provides a mechanism to explicitly and unambiguously label the type of content being provided by the entry, and allows for a broad variety of payload types including plain text, escaped HTML, XHTML, XML, Base64-encoded binary, and references to external content such as documents, video, audio streams, and so forth.
http://en.wikipedia.org/wiki/ATOM#Atom_compared_to_RSS_2.0
Well, since I write them by hand sometimes I choosed Atom.
hallvors # 19. September 2008, 20:02
Guess I'll have to bother somebody about this invalid RSS feed. Reading the post again I also realised that the point where it refers to the entity in the code above makes no sense since at some point during preview/re-editing that entity was itself turned into an angle bracket. Shall we suggest My Opera implements a live preview during blog editing, powered by XSLT string gymnastics?
Well, maybe not..
Robin_reala # 29. September 2008, 15:12
OmegaJunior # 18. November 2008, 23:50
Ah, ignorance precedes judgement in such interesting ways.