【发布时间】: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 & Safety Officer</h3>
</div>
<div class="description">
<p>You must have experience of working in a health & safety team.</p>
</div>
描述元素中的 & 字符未通过验证,如何将 &amp; 转换为 &amp;?
谢谢
【问题讨论】:
-
这取决于你是否可以使用 XSLT 2.0。
-
您使用哪种验证方式(XML、HTML5、HTML 4)?你会在输入 XML、XSLT、XSLT 结果上使用它吗?
-
@MartinHonnen 该页面正在验证 XHTML 1.0 过渡
-
@michael.hor257k 我的样式表设置为 2.0 版
-
您显示的样式表没有创建任何 XHTML,它没有 XHTML 命名空间,也没有生成任何 DOCTYPE。