首頁  >  文章  >  後端開發  >  XSL將XML中的CDATA註解輸出為HTML文字的程式碼實例

XSL將XML中的CDATA註解輸出為HTML文字的程式碼實例

Y2J
Y2J原創
2017-04-25 16:43:502050瀏覽

要利用DOM 來存取XML 文件,你必須將XML 文件連結到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[]]></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="www.w3.org/1999/XSL/Transform" 和輸出時加上disable-output-escaping="yes"

範例程式碼

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[]]></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文字的程式碼實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn