由于这是XSLT 1.0,你将不得不使用递归命名模板依次检查每个字符。
首先,它可能是更灵活地产生一种“查找”在你的XSLT,你可以指定符号列表和所需的元素名称与
<lookup:codes>
<code symbol="®">registeredTrademark</code>
<code symbol="©">copyright</code>
<code symbol="™">trademark</code>
</lookup:codes>
,以取代他们( 'lookup'命名空间实际上可以命名为任何东西,只要它在XSLT中是分层的即可)。
然后,访问此,您可以定义一个变量来访问该查询
<xsl:variable name="lookup" select="document('')/*/lookup:codes"/>
而且,为了查找基于符号实际的代码会做这样的事情(其中$文本)是一个包含正在检查的文本的变量。
<xsl:variable name="char" select="substring($text, 1, 1)"/>
<xsl:variable name="code" select="$lookup/code[@symbol = $char]"/>
所有命名模板要做的,就是检查文本的第一个字符,如果在查找存在与元素替换它,然后递归调用与文本的其余部分的模板。
以下是完整的XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:lookup="lookup" exclude-result-prefixes="lookup">
<xsl:output method="xml" indent="no"/>
<lookup:codes>
<code symbol="®">registeredTrademark</code>
<code symbol="©">copyright</code>
<code symbol="™">trademark</code>
</lookup:codes>
<xsl:variable name="lookup" select="document('')/*/lookup:codes"/>
<xsl:template match="text[text()]">
<text>
<xsl:call-template name="text"/>
</text>
</xsl:template>
<xsl:template name="text">
<xsl:param name="text" select="text()"/>
<xsl:variable name="char" select="substring($text, 1, 1)"/>
<xsl:variable name="code" select="$lookup/code[@symbol = $char]"/>
<xsl:choose>
<xsl:when test="$code"><xsl:element name="{$code}" /></xsl:when>
<xsl:otherwise>
<xsl:value-of select="$char"/>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="string-length($text) > 1">
<xsl:call-template name="text">
<xsl:with-param name="text" select="substring($text, 2, string-length($text) - 1)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
当施加到示例XML,下面是输出
<text>
Please see the registered mark<registeredTrademark /> .
Please see the copy right <copyright />.
Please see the Trade mark<trademark />.
</text>
是否有任何理由,你不能用一个简单的文本做更换XML来源? – Flynn1179 2012-08-08 10:20:28