XSLT使用&字符验证CDATA

时间:2019-03-25 14:34:20

标签: xml xslt

我有一个方案,其中CDATA返回html标记,所以我必须使用disable-output-escaping =“ yes”才能将内容正确呈现到html页面。但是,CDATA也可能包含&字符,这导致我的页面无法通过w3c验证。

这是我的xml来源的示例:

<?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>

结果:

   <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>

description元素中的&字符验证失败,如何将&转换为&amp;

谢谢

2 个答案:

答案 0 :(得分:1)

XSLT 2.0 中,您可以使用replace()函数来逃离&符:

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

XSLT 1.0 中,您需要使用递归模板:

<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>

演示https://xsltfiddle.liberty-development.net/6r5Gh3a

答案 1 :(得分: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.