[lxml-dev] Overriding whitespace normalization under XSLT

Nathan R. Yergler nathan at yergler.net
Sat Mar 17 01:31:13 CET 2007


I'm not sure this is really the right list for this, but we're using
lxml, and I'm hoping that someone will have the requisite expertise to
enlighten me.

Creative Commons has an XSLT transformation we use for generating
license engine output; it's divided into a few templates, and one
final template that assembles the pieces.  The problem is with respect
to the "img" tag in the human readable copy-n-paste output.  Here's an
excerpt from the transform:

	<xsl:template name="html">
		<xsl:param name="license-uri"/>
		<xsl:param name="license-name"/>
		<xsl:param name="rdf"/>
		<xsl:variable name="license-button">
			<xsl:choose>
				<xsl:when test="contains($license-uri,'GPL')">http://i.creativecommons.org/l<xsl:value-of
select="substring-after($license-uri,'http://creativecommons.org/licenses')"/>88x62.png</xsl:when>
				<xsl:otherwise>http://i.creativecommons.org/l<xsl:value-of
select="substring-after($license-uri,'http://creativecommons.org/licenses')"/>88x31.png</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<a rel="license" href="{$license-uri}"><img alt="Creative Commons
License" style="border-width:0" src="{$license-button}" /></a><br/>
		<xsl:call-template name="thiswork">
			<xsl:with-param name="license_name"
					select="$license-name" />
			<xsl:with-param name="license_url"
					select="$license-uri" />
		</xsl:call-template>

	</xsl:template>

The part that's problematic is this line:

		<a rel="license" href="{$license-uri}"><img alt="Creative Commons
License" style="border-width:0" src="{$license-button}" /></a><br/>

Note that there is a space between the closing quote of the src
attribute on the image tag and the "/>" closing bracket.  When we
process the transform, we consistently end up with

		<a rel="license" href="..."><img alt="Creative Commons License"
style="border-width:0" src="..."/></a><br/>

(note the space has been removed)

I've done some experiments and it seems any amount of whitespace
(including carriage returns) is collapsed during processing.  This may
be valid markup (I'm not certain), but some consumers of our
copy-and-paste markup barf when that space doesn't exist.

I feel like I've reached the end of my XSLT expertise, so does anyone
have any idea a) how to maintain that whitespace or b) whom to ask who
might know?

Thanks, and thanks for lxml,

Nathan

-----
Nathan R. Yergler
Senior Software Engineer
Creative Commons

http://wiki.creativecommons.org/User:NathanYergler


More information about the lxml-dev mailing list