02 March 2012

Fix for: XSL Transform Removing Empty Tags

I’m using some XSL to transform into html including ‘empty’ <script> tags which are written with an src element but no content e.g. <script src="somescript.js"></script>

The Problem

Widgets from Google Amazon or others are often out of your control but the trouble is depending on your XSLT settings the XSL transformer might remove the </script> and transform it to <script src="" /> or even worse I’ve seen examples of it try to put the closing tag in for a self closing tag but in the wrong place.

The Fix

I found this question on stackoverflow.com with an answer which hacked fixed it for me. Reposting here in case the original gets (re)moved. Thanks Jasso!
<!-- Identity template for empty elements -->
<xsl:template match="*[not(node())]">
  <!-- Define a dummy variable with empty content -->
  <xsl:variable name="empty" select="''"/>
    <xsl:apply-templates select="@* | node()" />
    <xsl:value-of select="$empty"/>

No comments:

Post a Comment