12

我的 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 等。

谢谢

4

3 回答 3

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

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

于 2009-03-31T16:07:03.807 回答
6

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

任何 CDATA 部分都只是 text() 节点的一部分,XSLT 处理器将其视为这样。

在 CDATA 中添加标记被普遍认为是不好的做法,报告的问题是一个典型的结果。

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

引用W3C XSLT 规范

"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. "

和:

"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."

于 2009-03-31T16:09:58.043 回答
0
<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>
于 2015-10-22T09:12:11.737 回答