将CDATA中的HTML代码输出
我按照http://hi.baidu.com/lanxmail/blog/item/a4e8bc8f20e931fb513d9296.html所说的测试过,IE可行,Opera不行
我将他的测试代码改了改,加入了xsl:copy和xsl:copy-of,IE表现还行
xml文件
------------------------------
<?xml version= "1.0 " encoding= "utf-8 "?>
<?xml-stylesheet type= 'text/xsl ' href= 'a.xsl '?>
<test>
<html1>
<b>
<![CDATA[
<hr/> <div> <font color= "red "> 111 </font> </div>
<div style= "border:1px solid blue;width:100px "> hhhh </div>
<div> qqq </div>
]]>
</b>
</html1>
<html2>
<![ CDATA [
<hr/> <div> <font color= "red "> 222 </font> </div>
<div style= "border:1px solid blue;width:100px "> hhhh </div>
<div> qqq </div>
]]>
</html2>
</test>
=====================
xsl文件
--------------------------------------
<?xml version= '1.0 ' encoding= 'utf-8 '?>
<xsl:stylesheet
version= "1.0 "
xmlns:xsl= "http://www.w3.org/1999/XSL/Transform ">
<xsl:template match= "test ">
<html>
<title> test cdata </title>
<body>
<xsl:apply-templates/>
</body> </html>
</xsl:template>
<xsl:template match= "test ">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match= "html1 ">
方法一:
<div style= "border:1px solid #ccc ">
使用了node(): <xsl:copy-of select= "node() "/>
<br/>
使用了text(): <xsl:copy-of select= "text() "/>
<br/>
使用了./node(): <xsl:copy-of select= "./node() "/>
<br/>
使用了./text(): <xsl:copy-of select= "./text() "/>
<br/>
</div>
</xsl:template>
<xsl:template match= "html2 ">
方法2:
<div style= "border:1px solid #ccc ">
<xsl:value-of select= "node() " disable-output-escaping= "yes "/>
</div>
</xsl:template>
</xsl:stylesheet>
==========================
名字为a.xml,a.xsl,在IE中测试,只有第二种方法行。xsl:copy-of 好像不起作用,xsl:copy也不起作用,错误在哪里?
cdata中的html通过xsl:copy和xsl:copy-of输出html都被编码了
===============
你使用GreenBrowser或者Maxthon查看XML文件(带XSL)的,运行页面分析插件,点源码就可以看到xsl格式化xml后生成的html
[解决办法]
上面BLOG中的代码是我写的,源地址:http://blog.csdn.net/cds27/archive/2006/05/02/705529.aspx
要让IE和FF都兼容,那就要用COPY-OF。并且这也是W3C建议的。
但是毕竟它只是一种规范,浏览器对规范的执行程度如何,那就很难说了。
所以我当初测试的时候,OPERA没通过。
使用copy-of,就不要用CDATA。直接将HTML写出来即可。如:
<html1>
<b>
<hr/> <div> <font color= "red "> 111 </font> </div>
<div style= "border:1px solid blue;width:100px "> hhhh </div>
<div> qqq </div>
</b>
</html1>
------解决方案--------------------
用脚本:
divID.innerHTML = divID.outerText;