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

示例代码

1. test.xml

 1<entry>
 2<title>entry with images</title>
 3<date>August 09, 2003</date>
 4<author>Kevin</author>
 5<idnum>000033</idnum>
 6<permalink>http://alazanto.org/xml/archives/000033.xml</permalink>
 7<body xmlns:html=" http://www.w3.org/1999/xhtml"><![CDATA[<p><img   
 8class="archive" align="right" src=" http://alazanto.org/images/sample.jpg "   
 9alt="photograph of a flower, just for show"/>Mauris felis elit, varius   
10quis, pulvinar vel, sodales vehicula, mi. Nunc elementum pharetra elit.   
11</p>]]>
12</body>
13<more xmlns:html=" http://www.w3.org/1999/xhtml"><![CDATA[ ]]></more>
14<comment-link>http://alazanto.org/xml/archives/000033_comments.xml</comment-link>
15<comment-count>6</comment-count>
16</entry>

2. test.xsl

 1<xsl:stylesheet **="" **xmlns:xsl="** **http://www.w3.org/1999/XSL/Transform** **" version="1.0">
 2<xsl:template match="/entry">
 3<html>
 4<head>
 5</head>
 6<body>
 7<xsl:value-of select="title"></xsl:value-of>
 8<xsl:value-of **="" **disable-output-escaping="yes" select="body"></xsl:value-of>
 9</body>
10</html>
11</xsl:template>
12</xsl:stylesheet>

关键之外在于使用的命名空间 xmlns:xsl=" http://www.w3.org/1999/XSL/Transform " 和输出时加上 **disable-output-escaping="yes"
**

Published At
Categories with Web编程
Tagged with
comments powered by Disqus