This produces the output you want! (Let me know if it doesn't!)
The identity template, copies all elements.
The ampText template, looks for all text containing &
The letters template iterates (recursively) over all letters in the text, and replaces all instances of &
with &
.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
<xsl:template name="identity" match="* | @*">
<xsl:copy>
<xsl:apply-templates select="* | @* | text()" />
</xsl:copy>
</xsl:template>
<xsl:template name="ampText" match="text()[contains(.,'&')]">
<xsl:call-template name="letters">
<xsl:with-param name="text" select="." />
</xsl:call-template>
</xsl:template>
<xsl:template name="letters">
<xsl:param name="text" select="'Some text'" />
<xsl:if test="$text != ''">
<xsl:variable name="letter" select="substring($text, 1, 1)" />
<xsl:choose>
<xsl:when test="$letter = '&'">
<xsl:text disable-output-escaping="yes"><![CDATA[&]]></xsl:text>
</xsl:when>
<xsl:otherwise><xsl:value-of select="$letter" /></xsl:otherwise>
</xsl:choose>
<xsl:call-template name="letters">
<xsl:with-param name="text" select="substring-after($text, $letter)" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>