0

I have a scenario where CDATA is returning html tags, so I have to use disable-output-escaping="yes" to render the content correctly to a html page. However, the CDATA can also include ampersand characters, this is causing my pages to fail w3c validation.

Here's an example of my xml source:

<?xml version="1.0" encoding="UTF-8"?>
<jobs>
    <job>
        <positionTitle><![CDATA[Health & Safety Officer]]></positionTitle>
        <description1><![CDATA[<p>You must have experience of working in a health & safety team.</p>]]></description1>
    </job>
</jobs>

XSLT

<?xml version="1.0" encoding="UTF-8"?>
   <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:content="http://purl.org/rss/1.0/modules/content/" >
  <xsl:output method="html"/>
  <xsl:template match="/">


            <xsl:for-each select="jobs/job">
                    <div class="job">
                        <div class="title"><h3><xsl:value-of select="positionTitle"/></h3></div>                    
                        <div class="description"><xsl:value-of select="description1" disable-output-escaping="yes"/></div>
                    </div>
            </xsl:for-each>     


 </xsl:template>
 </xsl:stylesheet>

Results in:

   <div class="title">
        <h3>Health &amp; Safety Officer</h3>
   </div>
    <div class="description">
        <p>You must have experience of working in a health & safety team.</p>
    </div>

The & character within the description element is failing validation, how do i convert the & to &amp; ?

thanks

Scott
  • 1,280
  • 4
  • 20
  • 35

2 Answers2

1

In XSLT 2.0 you can use the replace() function to escape the ampersands:

<xsl:value-of select="replace(description1, '&amp;', '&amp;amp;')" disable-output-escaping="yes"/>

In XSLT 1.0, you need to use a recursive template instead:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>

<xsl:template match="/">
    <xsl:for-each select="jobs/job">
        <div class="job">
            <div class="title">
                <h3>
                    <xsl:value-of select="positionTitle"/>
                </h3>
            </div>                    
            <div class="description">
                <xsl:call-template name="replace">
                    <xsl:with-param name="text" select="description1"/>
                </xsl:call-template>
            </div>
        </div>
    </xsl:for-each>     
 </xsl:template>

<xsl:template name="replace">
    <xsl:param name="text"/>
    <xsl:param name="searchString">&amp;</xsl:param>
    <xsl:param name="replaceString">&amp;amp;</xsl:param>
    <xsl:choose>
        <xsl:when test="contains($text,$searchString)">
            <xsl:value-of select="substring-before($text,$searchString)" disable-output-escaping="yes"/>
            <xsl:value-of select="$replaceString" disable-output-escaping="yes"/>
           <!--  recursive call -->
            <xsl:call-template name="replace">
                <xsl:with-param name="text" select="substring-after($text,$searchString)"/>
                <xsl:with-param name="searchString" select="$searchString"/>
                <xsl:with-param name="replaceString" select="$replaceString"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text" disable-output-escaping="yes"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

Demo: https://xsltfiddle.liberty-development.net/6r5Gh3a

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • Thanks, i'm getting a .Net errors so looks like simply changing the version number is not enough, i'll need to look at upgrading in visual studio System.Xml.Xsl.XslTransformException: 'replace()' is an unknown XSLT function – Scott Mar 25 '19 at 14:58
  • @Scott Well, yes: the question *"can you use XSLT 2.0"* means *"does your processor support XSLT 2.0"* - not *"can you type 2.0 instead of 1.0 in your stylesheet"*. If you're stuck with XSLT 1.0, then use a recursive template instead - see an example here: https://stackoverflow.com/questions/30339128/how-to-replace-single-quote-to-double-single-quote-in-xslt/30339654#30339654 – michael.hor257k Mar 25 '19 at 15:02
  • @MartinHonnen thanks for the example. I've got it to find/replace & with & - but the original html tags are not being output as such. My original example has disable-output-escaping="yes" - but this is an invalid attribute for the 'xsl:with-param' element – Scott Mar 25 '19 at 16:15
  • First, I am not Martin Honnen. As for the problem, you must use `disable-output-escaping` whenever you write to the output - i.e. whenever the template uses `xsl:value-of`. – michael.hor257k Mar 25 '19 at 17:08
  • Sorry, ok so i'm unclear if your linked solution will work in this scenario then... The CDATA has html tags within it, which are being escaped when they should be real elements. I only want & escaped – Scott Mar 26 '19 at 08:36
  • 1
    @Scott See the addition to my answer. – michael.hor257k Mar 28 '19 at 00:12
  • thanks for helping, XSLT1.0 version is working great – Scott Apr 05 '19 at 11:07
0
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:character-map name="a">
        <xsl:output-character character="&amp;" string="&amp;amp;"/>
        <xsl:output-character character="&lt;" string="&lt;"/>
        <xsl:output-character character="&gt;" string="&gt;"/>
    </xsl:character-map>
    <xsl:output method="html" use-character-maps="a"/>
    <xsl:template match="/">


        <xsl:for-each select="jobs/job">
            <div class="job">
                <div class="title"><h3><xsl:value-of select="positionTitle"/></h3></div>                    
                <div class="description"><xsl:value-of select="description1"/></div>
            </div>
        </xsl:for-each>     

    </xsl:template>
</xsl:stylesheet>
    You may also use character map.
imran
  • 461
  • 4
  • 8