将XML嵌入XML以便在Web浏览器中显示:我做错了什么?

所以我试着去研究这个问题,而我几乎已经完成了我的智慧.我发现了一种在 dpawson.co.uk上嵌入XSL的方法,
但我无法弄清楚我做错了什么.我在互联网上搜寻试图找到解决方案和解释,但没有人似乎有答案.

我正在尝试创建一个与服务器无关的文件,因此我可以将一个文件发送给我的同事,希望他们可以通过在Web浏览器中打开文件来查看该文件.

到目前为止,这是我的代码

<?xml version="1.0" encoding="ISO-8859-1"?>

<!--Start XSL-->

<?xml-stylesheet type="text/xml" href="#stylesheet"?>

<!DOCTYPE doc [
<!ATTLIST xsl:stylesheet
id ID #required>
]>
<doc>

<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="list/movie/seen[@value ='Yes']" />
    <xsl:variable name="b" select="list/movie/seen" />
    <xsl:variable name="c" select="sum(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="list/movie/seen[@value ='No']" />
    <xsl:variable name="g" select="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(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)" />
    <xsl:decimal-format name="percent" />
    <xsl:decimal-format name="average" decimal-separator="." />
    <!--End Variables-->
    <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">&#9734;&#9734;&#9734;&#9733;&#9733;</rating>
        <length value="y">136</length>
    </movie>
<!--Rest of XML-->
</list>
</doc>

这是一个电影列表,其中包含我到目前为止看过多少电影,有多少我还没有看到,有多少我在制作列表之前看到的,我看电影的总时间,和每部电影的平均评分.

问题是所有变量都显示为零值,这会导致除以零,这也会在某些输出上产生NaN值.

任何人都可以提供的任何东西将不胜感激!

以下代码
<?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,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')" /> minutes<br />
     Average rating:  <xsl:value-of select="format-number($d,'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">&#9734;&#9734;&#9734;&#9733;&#9733;</rating>
   <length value="y">136</length>
  </movie>
  <!--Rest of XML-->
 </list>
</doc>

保存为XML文件并在FireFox Web浏览器中打开时,生成如下所示的所需输出.此处不显示格式,但是当您在FireFox中打开包含上述代码的XML文件时.

My Movie List

Movies seen so far: 1/4 = 25%  
Movies yet to see: 3/4 = 75%  
Movies seen prior to making list: 0/4 = 0%  
Total time watched: 0 days,2 hours,16 minutes  
Average rating: 2.000 stars out of 5  

# Title         Release Date    Length      Seen    rating  
1 2001: A Space Odyssey 1968    141 minutes No      N/A  
2 28 Days Later         2002    113 minutes No      N/A  
3 28 Weeks Later        2007    100 minutes No      N/A  
4 A Clockwork Orange    1971    136 minutes Yes     ☆☆☆★★

注1:问题是嵌入XML< list>引起的元素嵌套级别的变化.在< doc>内将XML和XSL放入单个文件格式时的元素.该解决方案在所有XPath表达式中添加/ doc之前/ list,以便选择所需的元素,这些元素现在作为< doc>的后代更深层嵌套一层.和< list>而不仅仅是< list>的后代(就像两个文件分开的时候一样).

注2:xml-stylesheet类型也改为“text / xsl”,作为与IE 8兼容的一步.对于“text / xml”类型,IE 8只显示文档树(包括XSL)作为XML,而与“text / xsl”它尝试 – 虽然相当不成功 – 应用转换.在IE 8中正确显示这个显示要做更多的工作.请注意,任何类型的值在FireFox 7中都能正常工作.没有其他浏览器被测试过.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇