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

Gri*_*are 11 xml xslt cdata

我的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>
Run Code Online (Sandbox Code Playgroud)

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

<p class="smartText">
    <xsl:copy-of select="marketSummaryModuleData/smartText"/>                                    
</p>    
Run Code Online (Sandbox Code Playgroud)

不幸的是,页面上的输出显示为纯文本,而不是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>.
Run Code Online (Sandbox Code Playgroud)

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

谢谢

Tom*_*lak 11

<p class="smartText">
  <xsl:value-of 
    select="marketSummaryModuleData/smartText" 
    disable-output-escaping="yes"
  />
</p>
Run Code Online (Sandbox Code Playgroud)

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


Dim*_*hev 6

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

任何CDATA部分都只是text()节点的一部分,而XSLT处理器就是这样处理的.

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

DOE(disable-output-escaping)是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."