Multiplay Labs

tech hits and tips from Multiplay

Archive for the ‘XSL’ Category

Preventing XSL transforms self closing HTML tags

without comments

Every once in a while you will wan’t to do the something like the following an xsl stylesheet:


Unfortunately as XML is well formed and HTML isn’t the resulting HTML will be invalid e.g


The solution to this is quite simple, all you need to do is to tell the transform engine you are generating HTML using the output tag e.g.

<xsl:output method="html" />

Also note that if your target html is actually xhtml then using html as the output method will have some side effect e.g.

<br />

will be incorrectly replaced with


So what do you do if you want xhtml and empty elements which require a closing tag and not short form?

Well I’ve found two methods:-
1. Use a text node with output escaping disabled

<xsl:text disable-output-escaping="yes">&lt;div&gt;&lt;/div&gt;</xsl:text>

2. Use a xsl comment


The first means you need to escape all of you < / >, which is cumbersome the second adds unneeded comments to your output, so its up to you which downside you prefer 😉

Written by Dilbert

June 19th, 2010 at 11:37 pm

Posted in XSL

Tagged with , ,