当前位置: 首页 > 图文教程 > XML家族 > XML > 使用XSL将XML文档中的CDATA注释输出为HTML文本

XML
XML入门教程:元素声明
XML入门教程:文档类型声明
XML入门教程:XML语法
xml入门教程:XML是什么
WAP教程(11):WAP论坛和开放移动联盟与论坛
WAP教程(10):WML参考手册、WML实例和WML DTD
WAP教程(7):WML 计时器
WAP教程(6):WML 任务
WAP教程(5):WML 输入
WAP教程(4):WML 链接和图像
WAP教程(3):WML 格式化
WAP教程(8):WML 变量
WAP教程(2):WAP 基础
WAP教程(9):WML 实例
WAP教程(1):WAP 简介
XML入门教程:XSLT
XMLHTTPRequest对象
XML入门教程:CSS样式表
XML入门教程:XPath
XML入门教程:XLink

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-17   浏览: 41 ::
收藏到网摘: n/a

示例代码

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"