Xml XSLT使用&;验证CDATA;性格

Xml XSLT使用&;验证CDATA;性格,xml,xslt,Xml,Xslt,我有一个场景,CDATA返回html标记,因此我必须使用disable output escaping=“yes”将内容正确呈现到html页面。但是,CDATA还可以包含符号和字符,这导致我的页面无法通过w3c验证 以下是我的xml源代码示例: <?xml version="1.0" encoding="UTF-8"?> <jobs> <job> <positionTitle><![CDATA[Health &

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


结果:

   <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元素中的&character未通过验证,如何将
&
转换为
&


感谢在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>

&;
&;amp;

演示


您也可以使用角色映射。

这取决于您是否可以使用XSLT 2.0。您到底使用哪种验证(XML、HTML5、HTML4)?您是否在输入XML、XSLT和XSLT结果上使用了它?@MartinHonnen页面正在验证XHTML1.0transitional@michael.hor257k我的样式表设置为2.0版。您显示的样式表不会创建任何XHTML,没有XHTML命名空间,也不会生成任何DOCTYPE。谢谢,我收到了一个.Net错误,因此看起来仅仅更改版本号是不够的,我需要查看visual studio System.Xml.Xsl.XslTransformException中的升级:“replace()”是未知的XSLTfunction@Scott好的,是的:问题“您能使用XSLT2.0吗”的意思是“您的处理器支持XSLT2.0吗”-不是“您能在样式表中键入2.0而不是1.0吗?”如果您仍然坚持使用XSLT 1.0,那么请使用递归模板-请参见此处的示例:@MartinHonnen谢谢您的示例。我可以找到/替换&;-但原始html标记并没有这样输出。我的原始示例已禁用输出转义=“是”“-但这对于'xsl:with param'元素是无效的属性首先,我不是Martin Honnen。至于这个问题,无论何时写入输出,都必须使用
禁用输出转义
,即无论何时模板使用
xsl:value of
@Scott,请参见我的答案中的添加内容。我得到以下错误:System.Xml.xmleexception:“xsl”是未声明的前缀。第2行,位置3我正在氧气编辑器上运行它。它没有给出任何错误是的,我没有安装XLST2.0,v1 onlyxsl:charactermap是XSLT2.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>
    <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.