XML CDATA 内的 HTML 正在用 << 进行转换和>括号

发布于 2024-10-05 00:39:08 字数 839 浏览 0 评论 0 原文

我有一些示例 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&lt;br /&gt;Line 2&lt;br /&gt;Line 3

我可以看到 ASP 正在转换代码中的括号,但我不知道为什么。有什么想法吗?

I have some sample XML:

<sample><![CDATA[Line 1<br />Line 2<br />Line 3<br />]]></sample>

I'm using ASP to output this XML using a stylesheet like so:

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)

However, once this is getting written, the HTML output is showing up as:

Line 1<br />Line 2<br />Line 3

I can see that ASP is converting the brackets in the code, but I'm not sure why. Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紧拥背影 2024-10-12 00:39:08

我有一些示例 XML:

>第 2 行
>第 3 行
]]>

这是一个带有 文本节点 子节点的 sample 元素。

假设您应用恒等变换。那么结果就是:

<sample>Line 1<br />Line 2<br />Line 3<br /></sample>

为什么?由于文本节点和属性值具有特殊字符&,因此<>转义为字符实体。

编辑:当然,您可以使用DOE...但是,除此之外它是一个可选功能,无论如何结果都将是一个文本节点(没有编码字符实体)。您将需要其他解析器 fase(当将 HTML 片段输出和编码为 (X)HTML 文档(例如在 feed 中)时,这可能很有用,但存在输出格式错误的风险...)。

此样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="sample">
        <p>
            <xsl:value-of select="." disable-output-escaping="yes"/>
        </p>
    </xsl:template>
</xsl:stylesheet>

输出:

<p>Line 1<br />Line 2<br />Line 3<br /></p>

渲染为(实际标记):

第 1 行
第 2 行
第 3 行

I have some sample XML:

<sample><![CDATA[Line 1<br />Line 2<br />Line 3<br />]]></sample>

This is a sample element with a text node child.

Suppose you apply an identity transform. Then the result will be:

<sample>Line 1<br />Line 2<br />Line 3<br /></sample>

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:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="sample">
        <p>
            <xsl:value-of select="." disable-output-escaping="yes"/>
        </p>
    </xsl:template>
</xsl:stylesheet>

Output:

<p>Line 1<br />Line 2<br />Line 3<br /></p>

Render as (actual markup):

Line 1
Line 2
Line 3

扬花落满肩 2024-10-12 00:39:08

除了 @Alejandro 的解释之外,这里是最好的解决方案:

永远不要将标记放在文本 (CDATA) 节点中

而不是:

<sample><![CDATA[Line 1<br />Line 2<br />Line 3<br />]]></sample>

总是创建:

<sample>Line 1<br />Line 2<br />Line 3<br /></sample>

记住:将标记放入 CDATA 中会丢失它。

In addition to @Alejandro's explanation, here is the best possible solution:

Never put markup in a text (CDATA) node.

Instead of:

<sample><![CDATA[Line 1<br />Line 2<br />Line 3<br />]]></sample>

always create:

<sample>Line 1<br />Line 2<br />Line 3<br /></sample>

Remember: Putting markup inside of CDATA is losing it.

白龙吟 2024-10-12 00:39:08

认为是 XSL 转换给您带来了问题。您应该能够编辑 .xsl 文档来更正此问题:

<xsl:template match=".">
  <xsl:value-of select="." disable-output-escaping="yes" />
  <!-- ... other XSL business here ... -->
</xsl:template>

我正在窃取 此页面有关禁用输出转义

根据记录,我讨厌 XML/XSL——寻找问题的解决方案。一般来说,如果您需要处理标记,我发现 XML/XSL 只会引入问题,因为您经常需要处理标记片段,而这些标记片段通常不是有效的 XML,因此您将 CDATA 包裹在其周围,然后当您正在经历。

更新

好的,所以上面的方法不起作用。当然,在添加对问题的评论之前,我不知道 XSL 是什么样子。以下内容确实有效(来自此论坛帖子的想法):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" />
    <xsl:template match=".">
        <xsl:value-of select="sample"  />
    </xsl:template>
</xsl:stylesheet>

关键是 <代码>

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:

<xsl:template match=".">
  <xsl:value-of select="." disable-output-escaping="yes" />
  <!-- ... other XSL business here ... -->
</xsl:template>

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):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" />
    <xsl:template match=".">
        <xsl:value-of select="sample"  />
    </xsl:template>
</xsl:stylesheet>

Key is the <xsl:output method="text" />.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文