XML CDATA 内的 HTML 正在用 << 进行转换和>括号
我有一些示例 XML:
<sample><![CDATA[Line 1<br />Line 2<br />Line 3<br />]]></sample>
我正在使用 ASP 使用样式表输出此 XML,如下所示:
Set xmlHttp = Server.CreateObject("Microsoft.XMLHTTP")
xmlHttp.open "GET", URLxml, false
xmlHttp.send()
Set xslHttp = Server.CreateObject("Microsoft.XMLHTTP")
xslHttp.open "GET", xXsl, false
xslHttp.send()
Set xmlDoc = Server.CreateObject("MICROSOFT.XMLDOM")
Set xslDoc = Server.CreateObject("MICROSOFT.XMLDOM")
xmlDoc.async = false
xslDoc.async = false
xmlDoc.Load xmlHttp.responseXML
xslDoc.Load xslHttp.responseXML
Response.Write xmlDoc.transformNode(xslDoc)
但是,一旦编写完毕,HTML 输出将显示为:
Line 1<br />Line 2<br />Line 3
我可以看到 ASP 正在转换代码中的括号,但我不知道为什么。有什么想法吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个带有 文本节点 子节点的
sample
元素。假设您应用恒等变换。那么结果就是:
为什么?由于文本节点和属性值具有特殊字符
&
,因此<
和>
转义为字符实体。编辑:当然,您可以使用DOE...但是,除此之外它是一个可选功能,无论如何结果都将是一个文本节点(没有编码字符实体)。您将需要其他解析器 fase(当将 HTML 片段输出和编码为 (X)HTML 文档(例如在 feed 中)时,这可能很有用,但存在输出格式错误的风险...)。
此样式表:
输出:
渲染为(实际标记):
第 1 行
第 2 行
第 3 行
This is a
sample
element with a text node child.Suppose you apply an identity transform. Then the result will be:
Why? Because text nodes and attribute values have the special character
&
,<
and>
escape as character entities.EDIT: Of course, you could use DOE... But, besides that it's an optional feature, the result will be a text node no matter what (without the encode character entities). You will need other parser fase (this may be useful when output and encode HTML fragment to a (X)HTML document like in feeds, with the risk of malformed output...).
This stylesheet:
Output:
Render as (actual markup):
Line 1
Line 2
Line 3
除了 @Alejandro 的解释之外,这里是最好的解决方案:
永远不要将标记放在文本 (CDATA) 节点中。
而不是:
总是创建:
记住:将标记放入 CDATA 中会丢失它。
In addition to @Alejandro's explanation, here is the best possible solution:
Never put markup in a text (CDATA) node.
Instead of:
always create:
Remember: Putting markup inside of CDATA is losing it.
认为是 XSL 转换给您带来了问题。您应该能够编辑 .xsl 文档来更正此问题:
我正在窃取 此页面有关禁用输出转义。
根据记录,我讨厌 XML/XSL——寻找问题的解决方案。一般来说,如果您需要处理标记,我发现 XML/XSL 只会引入问题,因为您经常需要处理标记片段,而这些标记片段通常不是有效的 XML,因此您将 CDATA 包裹在其周围,然后当您正在经历。
更新
好的,所以上面的方法不起作用。当然,在添加对问题的评论之前,我不知道 XSL 是什么样子。以下内容确实有效(来自此论坛帖子的想法):
关键是 <代码>。
Think it's the XSL transformation that's causing you problems. You should be able to edit your .xsl document to correct this as such:
I'm stealing from this page about disable output escaping.
For the record I hate XML/XSL - a solution in search of a problem. Generally speaking if you need to deal with markup I've found XML/XSL only introduces problems because frequently you want to deal with markup fragments, which are often not valid XML, so you wrap CDATA around it and then hilarity ensues as you're experiencing.
Update
OK, so the above didn't work. Of course didn't know what XSL looked like until comment on question was added. The following does work (idea from this forum thread):
Key is the
<xsl:output method="text" />
.