我想使用翻译功能而不是使用替换从字符串中删除单词“和”。
例如:
<xsl:variable name="nme" select="translate(./Name/text(), ',:, '')" />
除了“,:”外,我还要删除单词“和”。请提出建议。
该translate
函数无法执行此操作,它只能删除或替换单个字符,而不能删除或替换多字符字符串。就像XSLT 1.0中的许多东西一样,转义路径是一个递归模板,最简单的版本是:
<xsl:template name="removeWord">
<xsl:param name="word" />
<xsl:param name="text" />
<xsl:choose>
<xsl:when test="contains($text, $word)">
<xsl:value-of select="substring-before($text, $word)" />
<xsl:call-template name="removeWord">
<xsl:with-param name="word" select="$word" />
<xsl:with-param name="text" select="substring-after($text, $word)" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
然后在定义nme
变量时调用此模板。
<xsl:variable name="nme">
<xsl:call-template name="removeWord">
<xsl:with-param name="word" select="'and'" /><!-- note quotes-in-quotes -->
<xsl:with-param name="text" select="translate(Name, ',:', '')" />
</xsl:call-template>
</xsl:variable>
在这里,我translate
用来删除单个字符,然后将结果传递给模板以删除“和”。
尽管正如注释中所指出的那样,这完全取决于您所说的“单词”的意思-这将删除所有出现的字符串“ and”,包括其他单词的中间部分,您可能希望更加保守一些,只删除“和” ”(例如,空格和)。
要删除多个单词,您只需重复调用模板,然后将一个调用的结果作为参数传递给下一个
<xsl:variable name="noEdition">
<xsl:call-template name="removeWord">
<xsl:with-param name="word" select="'Edition'" />
<xsl:with-param name="text" select="translate(Name, ',:', '')" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="nme">
<xsl:call-template name="removeWord">
<xsl:with-param name="word" select="' and'" />
<xsl:with-param name="text" select="$noEdition" />
</xsl:call-template>
</xsl:variable>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句