替换&;amp;在XSLT1.0中使用%26
我从搜索引擎返回了XML,其中包含这样的节点替换&;amp;在XSLT1.0中使用%26,xslt,xslt-1.0,Xslt,Xslt 1.0,我从搜索引擎返回了XML,其中包含这样的节点 <team>Some team name with &</team> 带有&; 我需要建立一个团队的链接,虽然这可能不是最佳的工作方式,但直到我发现一些团队名称包括一个符号 我拥有的是(被双引号包围的团队) 但如果团队包含一个符号,链接将被破坏,我如何才能最好地修复这个问题 提前感谢您可以使用此命名模板并在使用团队中的字符串之前调用它: <xsl:template name="replace
<team>Some team name with &</team>
带有&;
我需要建立一个团队的链接,虽然这可能不是最佳的工作方式,但直到我发现一些团队名称包括一个符号
我拥有的是(被双引号包围的团队)
但如果团队包含一个符号,链接将被破坏,我如何才能最好地修复这个问题
提前感谢您可以使用此命名模板并在使用
团队中的字符串之前调用它
:
<xsl:template name="replace">
<xsl:param name="string"/>
<xsl:param name="substring"/>
<xsl:param name="replacement"/>
<xsl:choose>
<xsl:when test="contains($string, $substring)">
<xsl:value-of select="substring-before($string, $substring)"/>
<xsl:value-of select="$replacement"/>
<xsl:call-template name="replace">
<xsl:with-param name="substring" select="$substring"/>
<xsl:with-param name="replacement" select="$replacement"/>
<xsl:with-param name="string" select="substring-after($string, $substring)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
您可以这样调用它,并用另一个子字符串替换任何子字符串:
<xsl:variable name="string-with-escaped-ampersand">
<xsl:call-template name="replace">
<xsl:with-param name="string" select="team"/>
<xsl:with-param name="substring">&</xsl:with-param>
<xsl:with-param name="replacement">%26</xsl:with-param>
</xsl:call-template>
</xsl:variable>
&;
%26
并在代码中使用它:
<xsl:variable name="teamend" select="concat($string-with-escaped-ampersand,'%22')"/>
正如@Tomalak在评论中指出的,由于您正在生成一个URL,您可能需要处理其他几个需要编码的字符。XSLT2.0中有相应的函数,但XSLT1.0中仅限于模板或扩展 有(不幸的是,没有内置函数)。哪种方法对您有效取决于XSLT处理器。在这种情况下,关于正确URL编码的建议可能更有用。
<xsl:variable name="teamend" select="concat($string-with-escaped-ampersand,'%22')"/>