Reputation: 138
I want to replace the &
symbol inside of a piece of text that is generated dynamically with its encoded value %26
to prevent it from breaking the URL string. I am storing the text inside hrefvalue
variable. My goal is to replace &
with %26
to be output in the final HTML code in the browser.
For example: "listening & comprehension" should become "listening %26 comprehension"
I am using <![CDATA[
to preserve %26
but this seems not to be working. I still end up with "listening & comprehension" in the browser. Why?
<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>
I am working on a system that uses XSL 1.0
Upvotes: 0
Views: 40
Reputation: 163625
Firstly, the CDATA is completely unnecessary and irrelevant. The only purpose of CDATA is to allow &
and <
to be written without escaping, and if those special characters are not present, the CDATA tag makes no difference.
Also, if you're generating an attribute (which seems likely if it's a URL) then disable-output-escaping has no effect. But again, it's probably not needed.
Your code only deals with one ampersand in a string.
But your code looks fine. If there's a problem, it's in the part of the code that you haven't shown us. Try to construct a complete reproducible example: a complete stylesheet and source document that we can actually run to see if we can reproduce the problem.
Upvotes: 3