从 XSL 中的 CDATA 标记内呈现 HTML 标记

发布于 2024-07-15 12:27:50 字数 1221 浏览 7 评论 0原文

我的 XML 代码中有一个 CDATA 标记,其中包含一些超链接。

<smartText><![CDATA[
Among individual stocks, the top percentage gainers in the S.&P. 500 are
<a href ='http://investing.domain.com/research/stocks/snapshot
/snapshot.asp?ric=LNC'>Lincoln National Corp</a> and 
<a href ='http://investing.domain.com/research/stocks/snapshot
/snapshot.asp?ric=PLD'>ProLogis</a>.]]>
</smartText>

我正在尝试将其转换为 HTML 页面,如下所示...

<p class="smartText">
    <xsl:copy-of select="marketSummaryModuleData/smartText"/>                                    
</p>    

不幸的是,页面上的输出以纯文本形式显示,而不是 html。

Among individual stocks, the top percentage gainers in the S.&P. 500 are <a href ='http://investing.businessweek.com/research/stocks/snapshot/snapshot.asp?ric=PLD'>ProLogis</a> and <a href ='http://investing.businessweek.com/research/stocks/snapshot/snapshot.asp?ric=LNC'>Lincoln National Corp</a>.

CDATA 部分是从经典 ASP 页创建的,因此实际的 XML 输出不包含 CDATA 部分。 这可能是问题的一部分吗? 我似乎无法获取要在页面上呈现的信息。 我尝试过Google搜索提供的多种解决方案,例如disable-escape-tags、xsl:copy-of、xsl:value-of等等。

谢谢

I have a CDATA tag within my XML code which contains some hyperlinks.

<smartText><![CDATA[
Among individual stocks, the top percentage gainers in the S.&P. 500 are
<a href ='http://investing.domain.com/research/stocks/snapshot
/snapshot.asp?ric=LNC'>Lincoln National Corp</a> and 
<a href ='http://investing.domain.com/research/stocks/snapshot
/snapshot.asp?ric=PLD'>ProLogis</a>.]]>
</smartText>

I am trying to transform it into an HTML page as follows...

<p class="smartText">
    <xsl:copy-of select="marketSummaryModuleData/smartText"/>                                    
</p>    

Unfortunately the output onto the page shows up in pure text, not as html.

Among individual stocks, the top percentage gainers in the S.&P. 500 are <a href ='http://investing.businessweek.com/research/stocks/snapshot/snapshot.asp?ric=PLD'>ProLogis</a> and <a href ='http://investing.businessweek.com/research/stocks/snapshot/snapshot.asp?ric=LNC'>Lincoln National Corp</a>.

The CDATA section is being created from a classic ASP page, so the actual XML output does not contain the CDATA section. Could that be part of the problem? I cannot seem to get the information to render on the page. I have tried multiple solutions offered up by Google searches, such as disable-escape-tags, xsl:copy-of, xsl:value-of and more.

Thank you

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

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

发布评论

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

评论(3

李不 2024-07-22 12:27:50
<p class="smartText">
  <xsl:value-of 
    select="marketSummaryModuleData/smartText" 
    disable-output-escaping="yes"
  />
</p>

编辑:正如 @Randell 在评论中指出的那样,并非所有 XSLT 处理器中都存在 disable-output-escaping 。 例如,Firefox 中的不支持该属性。 上述内容不适用于这些处理器。 不过,我所知道的所有独立 XSLT 处理器都支持它。

<p class="smartText">
  <xsl:value-of 
    select="marketSummaryModuleData/smartText" 
    disable-output-escaping="yes"
  />
</p>

EDIT: As @Randell points out in the comments, disable-output-escaping is not present in all XSLT processors. For instance, the one in Firefox does not support this attribute. The above won't work for these processors. All stand-alone XSLT processors I know support it, though.

雪花飘飘的天空 2024-07-22 12:27:50

您必须更正 XML,以便所需的 HTML(并且必须是格式良好的 XML)不包含在 CDATA 部分中

任何 CDATA 部分只是 text() 节点的一部分,XSLT 处理器也如此对待它。

将标记放入 CDATA 中被普遍认为是不好的做法,所报告的问题就是一个典型的结果。

DOE(禁用输出转义)是 XSLT 中的可选功能,不保证在不同的 XSLT 处理器上实现并产生相同的预期结果。

引用 W3C XSLT 规范。 :

“XSLT 处理器不需要支持禁用输出转义。如果 xsl:value-of 或 xsl:text 指定应禁用输出转义,而 XSLT 处理器不支持此操作,则 XSLT 处理器可能会发出一个信号错误;如果它没有发出错误信号,则必须通过不禁用输出转义来恢复。

和:

“由于禁用输出转义可能不适用于所有 XSLT 处理器,并且可能导致 XML 格式不正确,因此只有在没有其他选择时才应使用它。”

You have to correct the XML so that the desired HTML (and it needs to be well-formed XML) is not contained within a CDATA section.

Any CDATA section is just part of a text() node and the XSLT processor treats it as such.

Putting markup within CDATA is universally acknowledged as bad practice and the reported issue is one typical result.

DOE (disable-output-escaping) is an optional feature in XSLT and is not guaranteed to be implemented and producing the same expected results on different XSLT processors.

To quote the W3C XSLT Spec.:

"An XSLT processor is not required to support disabling output escaping. If an xsl:value-of or xsl:text specifies that output escaping should be disabled and the XSLT processor does not support this, the XSLT processor may signal an error; if it does not signal an error, it must recover by not disabling output escaping.
"

and:

"Since disabling output escaping may not work with all XSLT processors and can result in XML that is not well-formed, it should be used only when there is no alternative."

〃安静 2024-07-22 12:27:50
<xsl:for-each select="marketSummaryModuleData/smartText">
    <xsl:copy-of select="node()"/>
</xsl:for-each>

<smartText>
Among individual stocks, the top percentage gainers in the S.&P. 500 are
<a href ='http://investing.domain.com/research/stocks/snapshot
/snapshot.asp?ric=LNC'>Lincoln National Corp</a> and 
<a href ='http://investing.domain.com/research/stocks/snapshot
/snapshot.asp?ric=PLD'>ProLogis</a>.
</smartText>
<xsl:for-each select="marketSummaryModuleData/smartText">
    <xsl:copy-of select="node()"/>
</xsl:for-each>

<smartText>
Among individual stocks, the top percentage gainers in the S.&P. 500 are
<a href ='http://investing.domain.com/research/stocks/snapshot
/snapshot.asp?ric=LNC'>Lincoln National Corp</a> and 
<a href ='http://investing.domain.com/research/stocks/snapshot
/snapshot.asp?ric=PLD'>ProLogis</a>.
</smartText>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文