【问题标题】:XSLT validate CDATA with & characterXSLT 使用 & 字符验证 CDATA
【发布时间】:2019-03-25 14:34:19
【问题描述】:

我有一个 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;amp; 转换为 &amp;amp;

谢谢

【问题讨论】:

  • 这取决于你是否可以使用 XSLT 2.0。
  • 您使用哪种验证方式(XML、HTML5、HTML 4)?你会在输入 XML、XSLT、XSLT 结果上使用它吗?
  • @MartinHonnen 该页面正在验证 XHTML 1.0 过渡
  • @michael.hor257k 我的样式表设置为 2.0 版
  • 您显示的样式表没有创建任何 XHTML,它没有 XHTML 命名空间,也没有生成任何 DOCTYPE。

标签: xml xslt


【解决方案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

【讨论】:

  • 谢谢,我遇到了一个 .Net 错误,所以看起来仅仅更改版本号是不够的,我需要查看 Visual Studio System.Xml.Xsl.XslTransformException 中的升级:' replace()' 是一个未知的 XSLT 函数
  • @Scott 好吧,是的:问题 “你能使用 XSLT 2.0” 的意思是 “你的处理器是否支持 XSLT 2.0” - 不是 “您可以在样式表中输入 2.0 而不是 1.0”。如果您坚持使用 XSLT 1.0,请改用递归模板 - 请参阅此处的示例:stackoverflow.com/questions/30339128/…
  • @MartinHonnen 感谢您的示例。我已经找到/替换 & 与 & - 但原始的 html 标签并没有被这样输出。我的原始示例具有 disable-output-escaping="yes" - 但这是 'xsl:with-param' 元素的无效属性
  • 首先,我不是 Martin Honnen。至于问题,无论何时写入输出都必须使用disable-output-escaping - 即每当模板使用xsl:value-of
  • @Scott 查看我的答案。
【解决方案2】:
    <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.

【讨论】:

  • 我收到以下错误:System.Xml.XmlException: 'xsl' is an undeclared prefix。第 2 行,位置 3
  • 我在氧气编辑器上运行它。它没有给出任何错误
  • 是的,我没有安装 XLST 2.0,只有 v1
  • xsl:character-map 是 xslt 2.0 元素
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-27
  • 2014-01-03
  • 1970-01-01
  • 2014-02-23
  • 2011-06-05
  • 2012-08-20
相关资源
最近更新 更多