首页 > 解决方案 > XSLT 使用 & 字符验证 CDATA

问题描述

我有一个 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>

描述元素中的 & 字符验证失败,如何将其转换&&amp;

谢谢

标签: xmlxslt

解决方案


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


推荐阅读