Wednesday, September 22, 2004

Defining Entities in Xslt 

I am always forgetting how to define entities in xslt stylesheets. Here is a simple sample.

<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp '&#160;'>
]>

This is particularly relevant when trying to insert a non-breaking space with xslt--a particularly esoteric task. I have heard that setting the output to ASCII instead of UTF-8 will ensure that the above non-breaking space is included in the output. I believe &#A0; is the unicode character.

<xsl:output method="html" encoding="ASCII" omit-xml-declaration="no" version="4.0" />

Another option is this apparently, though I have heard it is not predictable.

<xsl:text>&amp;#160;</xsl:text>




Wednesday, September 15, 2004

Comparing and Replacing Strings with Xsl 

I found a nice function for replacing strings in Xml using Xsl.

As I am sure you know, the Translate function in Xsl does not replace full strings instead it replaces characters.

The following recursive template provides the "missing" functionality.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes" />
<xsl:template name="globalReplace">
<xsl:param name="outputString" />
<xsl:param name="target" />
<xsl:param name="replacement" />
<xsl:choose>
<xsl:when test="contains($outputString,$target)">
<xsl:value-of select="concat(substring-before($outputString,$target),$replacement)" />
<xsl:call-template name="globalReplace">
<xsl:with-param name="outputString" select="substring-after($outputString,$target)" />
<xsl:with-param name="target" select="$target" />
<xsl:with-param name="replacement" select="$replacement" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$outputString" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="text()">
<xsl:call-template name="globalReplace">
<xsl:with-param name="outputString" select="." />
<xsl:with-param name="target" select="'finish'" />
<xsl:with-param name="replacement" select="'FINISH'" />
</xsl:call-template>
</xsl:template>
<xsl:template match="@*|*">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>


See link for more information.
xml.com


This page is powered by Blogger. Isn't yours?