找回密码
 注册
搜索
查看: 3303|回复: 0

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

[复制链接]
发表于 2010-10-3 19:57:04 | 显示全部楼层 |阅读模式
示例代码

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


2. test.xsl
  1. <?xml version="1.0"?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  3. <xsl:template match="/entry">
  4. <html>
  5. <head>
  6. </head>
  7. <body>
  8. <xsl:value-of select="title" />
  9. <xsl:value-of select="body" disable-output-escaping="yes"/>

  10. </body>
  11. </html>
  12. </xsl:template>
  13. </xsl:stylesheet>
复制代码


关键之外在于使用的命名空间xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 和输出时加上disable-output-escaping="yes"
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|宁德市腾云网络科技有限公司 ( 闽ICP备2022007940号-5|闽公网安备 35092202000206号 )

GMT+8, 2025-5-4 22:03 , Processed in 0.014311 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表