我从搜索引擎返回XML,其中包含类似
的节点<team>Some team name with &</team>
我需要建立一个团队的链接,虽然它可能不是最佳的工作方式,直到我发现一些团队名称包含一个&符号
我拥有的是(由双引号包围的团队)
<xsl:variable name="teamend" select="concat(team,'%22')"/>
<a href="{concat('http://site/page.aspx?k=team%3D%22', $teamend)}">
<xsl:call-template name="DisplayCustomField">
<xsl:with-param name="customfield" select="team" />
</xsl:call-template>
但如果团队中包含&符号,则链接将被破坏,我该如何才能最好地解决此问题?
提前致谢
答案 0 :(得分:0)
您可以使用此命名模板并在使用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>
并在您的代码中使用它:
<xsl:variable name="teamend" select="concat($string-with-escaped-ampersand,'%22')"/>
正如@Tomalak在评论中指出的那样,由于您正在生成URL,因此您可能需要处理需要编码的其他几个字符。在XSLT 2.0中有相应的功能,但在XSLT 1.0中,您将仅限于模板或扩展。