首页 > web前端 > 使用XSL将XML文档中的CDATA注释输出为HTML文本

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

2010年5月12日 阅读(191 ) 发表评论 阅读评论

如何使用XSL将XML文档中的CDATA注释输出为HTML文本,在项目中用到,特记录一下:
示例代码:
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”

声明:转载原创文章时请注明:
    文章转载自:快乐心情, 原文地址:使用XSL将XML文档中的CDATA注释输出为HTML文本

分类: web前端 标签:

Related Posts

  1. 2010年5月14日16:23 | #1

    博客更新得挺勤的!

  2. 2010年5月18日11:55 | #2

    一时心血来潮 哈哈@dnfwg

  1. 本文目前尚无任何 trackbacks 和 pingbacks.
icon_wink.gif icon_neutral.gif icon_mad.gif icon_twisted.gif icon_smile.gif icon_eek.gif icon_sad.gif icon_rolleyes.gif icon_razz.gif icon_redface.gif icon_surprised.gif icon_mrgreen.gif icon_lol.gif icon_idea.gif icon_biggrin.gif icon_evil.gif icon_cry.gif icon_cool.gif icon_arrow.gif icon_confused.gif icon_question.gif icon_exclaim.gif