使用XSL将XML文档中的CDATA注释输出为HTML文本

所属分类: 网络编程 / XML/RSS 阅读数: 1769
收藏 0 赞 0 分享
示例代码
1.   test.xml
复制代码 代码如下:

<?xml version="1.0" encoding="gb2312"?>
<?xml-stylesheet href="test.xsl" type="text/xsl"?>
<entry>
 <title>entry with images</title>
 <date>August 09, 2003</date>
 <author>Kevin</author>
 <idnum>000033</idnum>
 <permalink>http://alazanto.org/xml/archives/000033.xml</permalink>
 <body xmlns:html="http://www.w3.org/1999/xhtml"><![CDATA[<p><img
  class="archive" align="right" src="http://alazanto.org/images/sample.jpg"
  alt="photograph of a flower, just for show"/>Mauris felis elit, varius
  quis, pulvinar vel, sodales vehicula, mi. Nunc elementum pharetra elit.
  </p>]]>
 </body>
 <more xmlns:html="http://www.w3.org/1999/xhtml"><![CDATA[]]></more>
 <comment-link>http://alazanto.org/xml/archives/000033_comments.xml</comment-link>
 <comment-count>6</comment-count>
</entry>

2.  test.xsl
复制代码 代码如下:

<?xml version="1.0"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/entry"> 
<html> 
<head> 
</head> 
<body> 
<xsl:value-of select="title" /> 
<xsl:value-of select="body" disable-output-escaping="yes"/> 

</body> 
</html> 
</xsl:template> 
</xsl:stylesheet>


关键之外在于使用的命名空间xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 和输出时加上disable-output-escaping="yes"
更多精彩内容其他人还在看

RSS的语言编码大全

RSS的语言编码大全
收藏 0 赞 0 分享

JavaScript生成xml

JavaScript生成xml
收藏 0 赞 0 分享

XML轻松学习手册(5)XML实例解析

XML轻松学习手册(5)XML实例解析
收藏 0 赞 0 分享

XSL简明教程(1)XSL入门

XSL简明教程(1)XSL入门
收藏 0 赞 0 分享

XSL简明教程(2)XSL转换

XSL简明教程(2)XSL转换
收藏 0 赞 0 分享

XSL简明教程(3)在客户端的实现

XSL简明教程(3)在客户端的实现
收藏 0 赞 0 分享

XSL简明教程(4)在服务器端的实现

XSL简明教程(4)在服务器端的实现
收藏 0 赞 0 分享

XSL简明教程(5)XSL的索引

XSL简明教程(5)XSL的索引
收藏 0 赞 0 分享

XSL简明教程(6)XSL过滤和查询

XSL简明教程(6)XSL过滤和查询
收藏 0 赞 0 分享

XSL简明教程(7)XSL 的控制语句

XSL简明教程(7)XSL 的控制语句
收藏 0 赞 0 分享
查看更多