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

在XML中嵌入XSL以在Web浏览器中显示:我做错了什么?,xml,xslt,embed,Xml,Xslt,Embed,所以我试着研究这个,我几乎不知所措。我找到了一种在XML中嵌入XSL的方法, 但我不知道我做错了什么。我在互联网上搜索,试图找到解决方案和解释,但似乎没有人有答案 我正在尝试创建一个独立于服务器的文件,这样我就可以将一个文件发送给我的同事,希望他们可以通过在web浏览器中打开文件来查看该文件 以下是我目前的代码: <?xml version="1.0" encoding="ISO-8859-1"?> <!--Start XSL--> <?xml-styleshe

所以我试着研究这个,我几乎不知所措。我找到了一种在XML中嵌入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>

氢{
字体系列:Courier、Courier New、monospace;
字体大小:32px;
文字装饰:下划线;
}
身体{
字体系列:Courier New,monospace;
}
p{
字体大小:16px;
}
桌子{
字体大小:14px;
}
.头衔{
文本对齐:左对齐;
}
.释放{
文本对齐:居中;
}
.看见了吗{
文本对齐:居中;
}
.评级{
文本对齐:右对齐;
}
.长度{
文本对齐:居中;
}
我的电影列表
迄今为止看过的电影:/=
尚未观看的电影:/=
上榜前看过的电影://=
观看的总时间:天、小时、分钟
平均评分:5分之星


# 标题 发布日期 长度 看到 评级 会议记录 会议记录 会议记录 2001年:太空漫游 1968 不 不适用 141 28天后 2002 不 不适用 113 28周后 2007 不 不适用 100 发条橙 1971 对 ☆☆☆★★ 136
这是一个电影列表,包含了到目前为止我看过多少部电影,还有多少部我还没看过,在我上榜之前我看过多少部电影,我看电影的总时间,以及每部电影的平均收视率

问题是所有变量都显示为零值,这会导致被零除,从而在某些输出上也给出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, 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>
注1:问题是当您将XML和XSL放入单个文件格式时,将XML嵌入
元素中会导致元素嵌套级别的变化。解决方案在中的所有XPath表达式中添加
/doc
之前的
/list
选择所需的元素,这些元素现在作为
的后代嵌套更深一层,而不仅仅是
的后代(就像两个文件分开时那样)


注2:xml样式表类型也更改为“text/xsl”,作为与IE 8兼容的一个步骤。对于“text/xml”类型,IE 8仅将文档树(包括xsl)显示为xml,而对于“text/xsl”,它尝试应用转换,尽管没有成功。需要做更多的工作才能在IE 8中正确显示。请注意,在Firefox7中,任何一个类型值都可以正常工作。没有测试其他浏览器。

我不太明白您在这里想做什么,但我不认为这是一个样式表。您的标记嵌入了一个非常奇怪的标记中。不幸的是,我的XML太大,无法完全放在问题的主体中。我要做的是让我的变量工作。当我将.xml和.xsl文件分开时,它工作得很好。但是当我嵌入时,xml会显示出来,但是我的变量不起作用。我有很多0和N。你用这个文档做什么。您可以说“xml出现了”。哪里您正在运行什么软件,以及如何运行?我正在尝试将xsl嵌入到xml文档中。我用Safari查看xml文档,用Komodo编辑它。当我说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     ☆☆☆★★