使用XSL將XML文檔中的CDATA注釋輸出為HTML文本
更新時(shí)間:2006年09月06日 00:00:00 作者:
示例代碼
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>
關(guān)鍵之外在于使用的命名空間xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 和輸出時(shí)加上disable-output-escaping="yes"
1. test.xml
復(fù)制代碼 代碼如下:
<?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
復(fù)制代碼 代碼如下:
<?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>
關(guān)鍵之外在于使用的命名空間xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 和輸出時(shí)加上disable-output-escaping="yes"
相關(guān)文章
javascript xml xsl取值及數(shù)據(jù)修改
前幾天公司要用xml快速方便的存取屬性N多的數(shù)據(jù),跟sinoly合作研究了一下.做了一個(gè)小例子.2008-06-06氣象 XML 數(shù)據(jù)源應(yīng)用程序開發(fā)指南-操作檢查列表
氣象 XML 數(shù)據(jù)源應(yīng)用程序開發(fā)指南-操作檢查列表...2007-02-02xpath簡(jiǎn)介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
xpath就是選擇XML文件中節(jié)點(diǎn)的方法。下面通過本文給大家分享xpath的相關(guān)知識(shí),感興趣的朋友一起學(xué)習(xí)吧2017-08-08