xml/xsl(2): 将XML/XSL写到一个文件

php中文网
发布: 2016-06-07 15:15:03
原创
1271人浏览过

这被称为数据和显示式分离,这非常好,当我们想要改变风,主要换一个XSL就行了,并不需要重新添加一份数据。 但是有的时候也是有特例的,比如我们就有这个奇怪的需求---将XML/XSL写到一个文件。百度后,没有结果;忽然灵机一动,不是有一个很热门的问答网站


这被称为数据和显示格式分离,这非常好,当我们想要改变风格,主要换一个xsl就行了,并不需要重新添加一份数据。


但是有的时候也是有特例的,比如我们就有这个奇怪的需求---将XML/XSL写到一个文件。百度后,没有结果;忽然灵机一动,不是有一个很热门的问答网站(stackoverflow)吗!


我就去搜索了一下,果然有个仁兄给出了一个很NICE的答案。我先列出来,后面有时间写个自己修改后的版本。

地址:http://stackoverflow.com/questions/9523174/embed-xsl-in-xml-for-display-in-web-browser-what-am-i-doing-wrong/9530410


<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="#stylesheet"?>
<!DOCTYPE doc [
<!ATTLIST xsl:stylesheet
id ID #REQUIRED>
]>
<doc>
 <!--Start XSL-->
 <xsl:stylesheet id="stylesheet"
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

  <xsl:template match="xsl:stylesheet" />

   <!--Variables-->
   <xsl:variable name="a" select="/doc/list/movie/seen[@value ='Yes']" />
   <xsl:variable name="b" select="/doc/list/movie/seen" />
   <xsl:variable name="c" select="sum(/doc/list/movie/rating/@value)" />
   <xsl:variable name="d" select="$c div count($a)" />
   <xsl:variable name="e" select="count($a) div count($b)" />
   <xsl:variable name="f" select="/doc/list/movie/seen[@value ='No']" />
   <xsl:variable name="g" select="/doc/list/movie/seen[@value ='Prior']" />
   <xsl:variable name="h" select="count($f) div count($b)" />
   <xsl:variable name="j" select="count($g) div count($b)" />
   <xsl:variable name="minutes_total" select="sum(/doc/list/movie/length[@value ='y'])" />
   <xsl:variable name="minutes" select="$minutes_total mod 60" />
   <xsl:variable name="hours" select="floor($minutes_total div 60) mod 24" />
   <xsl:variable name="hours2" select="floor($minutes_total div 60)" />
   <xsl:variable name="days" select="floor($hours2 div 24)" />
   <!--End Variables-->

  <xsl:decimal-format name="percent" />
  <xsl:decimal-format name="average" decimal-separator="." />

  <xsl:template match="/doc">
   <html>
    <head>
     <style>
      h2{
      font-family: Courier, Courier New, monospace;
      font-size: 32px;
      text-decoration: underline;
      }
      body{
      font-family: Courier New, monospace;
      }
      p{
      font-size: 16px;
      }
      table{
      font-size: 14px;
      }
      .title{
      text-align:left;
      }
      .release{
      text-align:center;
      }
      .seen{
      text-align:center;
      }
      .rating{
      text-align:right;
      }
      .length{
      text-align:center;
      }
     </style>
    </head>
    <body>
     <h2>My Movie List</h2>
     <p>Movies seen so far: <xsl:value-of select="count($a)" />/<xsl:value-of select="count($b)" /> = <xsl:value-of select="format-number($e, '#%', 'percent')" /><br />
     Movies yet to see: <xsl:value-of select="count($f)" />/<xsl:value-of select="count($b)" /> = <xsl:value-of select="format-number($h, '#%', 'percent')" /><br />
     Movies seen prior to making list: <xsl:value-of select="count($g)" />/<xsl:value-of select="count($b)" /> = <xsl:value-of select="format-number($j, '#%', 'percent')" /><br />
     Total time watched: <xsl:value-of select="format-number($days, '#0')" /> days, <xsl:value-of select="format-number($hours, '#0')" /> hours, <xsl:value-of select="format-number($minutes, '#0')" /> minutes<br />
     Average rating:  <xsl:value-of select="format-number($d, '#.000', 'average')" /> stars out of 5</p>
     <br />
     <table border="1">
      <tr>
       <th>#</th>
       <th>Title</th>
       <th>Release Date</th>
       <th>Length</th>
       <th>Seen</th>
       <th>Rating</th>
      </tr>
      <xsl:for-each select="list/movie">
       <xsl:choose>
        <xsl:when test='seen = "Yes"'>
         <tr style="background-color:#666; color:#fff">
          <td> <xsl:number /></td>
          <td class="title"><xsl:value-of select="title"/></td>
          <td class="release"><xsl:value-of select="release"/></td>
          <td class="length"><xsl:value-of select="length" /> minutes</td>
          <td class="seen"><xsl:value-of select="seen"/></td>
          <td class="rating"><xsl:value-of select="rating"/></td>
         </tr>
        </xsl:when>
        <xsl:when test='seen = "Seen prior to making list"'>
         <tr style="background-color:#999; color:#000">
          <td> <xsl:number /></td>
          <td class="title"><xsl:value-of select="title"/></td>
          <td class="release"><xsl:value-of select="release"/></td>
          <td class="length"><xsl:value-of select="length"/> minutes</td>
          <td class="seen"><xsl:value-of select="seen"/></td>
          <td class="rating"><xsl:value-of select="rating"/></td>
         </tr>
        </xsl:when>
        <xsl:otherwise>
         <tr style="background-color:#fff;">
          <td> <xsl:number /></td>
          <td class="title"><xsl:value-of select="title"/></td>
          <td class="release"><xsl:value-of select="release"/></td>
          <td class="length"><xsl:value-of select="length" /> minutes</td>
          <td class="seen"><xsl:value-of select="seen"/></td>
          <td class="rating"><xsl:value-of select="rating"/></td>
         </tr>
        </xsl:otherwise>
       </xsl:choose>
      </xsl:for-each>
     </table>
    </body>
   </html>
  </xsl:template>
 </xsl:stylesheet>

 <!--Start XML-->
 <list>
  <movie>
   <title>2001: A Space Odyssey</title>
   <release>1968</release>
   <seen value="No">No</seen>
   <rating>N/A</rating>
   <length value="n">141</length>
  </movie>
  <movie>
   <title>28 Days Later</title>
   <release>2002</release>
   <seen value="No">No</seen>
   <rating>N/A</rating>
   <length value="n">113</length>
  </movie>
  <movie>
   <title>28 Weeks Later</title>
   <release>2007</release>
   <seen value="No">No</seen>
   <rating>N/A</rating>
   <length value="n">100</length>
  </movie>
  <movie>
   <title>A Clockwork Orange</title>
   <release>1971</release>
   <seen value="Yes">Yes</seen>
   <rating value="2">☆☆☆★★</rating>
   <length value="y">136</length>
  </movie>
  <!--Rest of XML-->
 </list>
</doc>
登录后复制


最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号