【发布时间】:2020-10-24 19:10:34
【问题描述】:
我想将动态生成的一段文本中的& 符号替换为其编码值%26,以防止它破坏URL 字符串。我将文本存储在 hrefvalue 变量中。我的目标是将& 替换为%26,以便在浏览器的最终HTML 代码中输出。
例如: “听力和理解”应该变成“听力 %26 理解”
我正在使用<![CDATA[ 来保留%26,但这似乎不起作用。我仍然在浏览器中得到“聆听和理解”。为什么?
<xsl:variable name="hrefvalue" select="./node()" />
<xsl:choose>
<xsl:when test="contains($hrefvalue, '&')">
<xsl:variable name="string-before" select="substring-before($hrefvalue, '&')" />
<xsl:variable name="string-after" select="substring-after($hrefvalue, '&')" />
<xsl:variable name="ampersand"><![CDATA[%26]]></xsl:variable>
<xsl:value-of select="concat($string-before, $ampersand, $string-after)" disable-output-escaping="yes" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$hrefvalue" disable-output-escaping="no" />
</xsl:otherwise>
</xsl:choose>
我正在开发一个使用 XSL 1.0 的系统
【问题讨论】: