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

前端之家收集整理的这篇文章主要介绍了将XML嵌入XML以便在Web浏览器中显示:我做错了什么?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我试着去研究这个问题,而我几乎已经完成了我的智慧.我发现了一种在 dpawson.co.uk上嵌入XSL的方法,
但我无法弄清楚我做错了什么.我在互联网上搜寻试图找到解决方案和解释,但没有人似乎有答案.

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

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

  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2.  
  3. <!--Start XSL-->
  4.  
  5. <?xml-stylesheet type="text/xml" href="#stylesheet"?>
  6.  
  7. <!DOCTYPE doc [
  8. <!ATTLIST xsl:stylesheet
  9. id ID #required>
  10. ]>
  11. <doc>
  12.  
  13. <xsl:stylesheet id="stylesheet"
  14. version="1.0"
  15. xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
  16. <xsl:template match="xsl:stylesheet" />
  17. <!--Variables-->
  18. <xsl:variable name="a" select="list/movie/seen[@value ='Yes']" />
  19. <xsl:variable name="b" select="list/movie/seen" />
  20. <xsl:variable name="c" select="sum(list/movie/rating/@value)" />
  21. <xsl:variable name="d" select="$c div count($a)" />
  22. <xsl:variable name="e" select="count($a) div count($b)" />
  23. <xsl:variable name="f" select="list/movie/seen[@value ='No']" />
  24. <xsl:variable name="g" select="list/movie/seen[@value ='Prior']" />
  25. <xsl:variable name="h" select="count($f) div count($b)" />
  26. <xsl:variable name="j" select="count($g) div count($b)" />
  27. <xsl:variable name="minutes_total" select="sum(list/movie/length[@value ='y'])" />
  28. <xsl:variable name="minutes" select="$minutes_total mod 60" />
  29. <xsl:variable name="hours" select="floor($minutes_total div 60) mod 24" />
  30. <xsl:variable name="hours2" select="floor($minutes_total div 60)" />
  31. <xsl:variable name="days" select="floor($hours2 div 24)" />
  32. <xsl:decimal-format name="percent" />
  33. <xsl:decimal-format name="average" decimal-separator="." />
  34. <!--End Variables-->
  35. <xsl:template match="/doc">
  36. <html>
  37. <head>
  38. <style>
  39. h2{
  40. font-family: Courier,Courier New,monospace;
  41. font-size: 32px;
  42. text-decoration: underline;
  43. }
  44. body{
  45. font-family: Courier New,monospace;
  46. }
  47. p{
  48. font-size: 16px;
  49. }
  50. table{
  51. font-size: 14px;
  52. }
  53. .title{
  54. text-align:left;
  55. }
  56. .release{
  57. text-align:center;
  58. }
  59. .seen{
  60. text-align:center;
  61. }
  62. .rating{
  63. text-align:right;
  64. }
  65. .length{
  66. text-align:center;
  67. }
  68. </style>
  69. </head>
  70. <body>
  71. <h2>My Movie List</h2>
  72. <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 />
  73. 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 />
  74. 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 />
  75. 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 />
  76. Average rating: <xsl:value-of select="format-number($d,'#.000','average')" /> stars out of 5</p>
  77. <br />
  78. <table border="1">
  79. <tr>
  80. <th>#</th>
  81. <th>Title</th>
  82. <th>Release Date</th>
  83. <th>Length</th>
  84. <th>Seen</th>
  85. <th>Rating</th>
  86. </tr>
  87. <xsl:for-each select="list/movie">
  88. <xsl:choose>
  89. <xsl:when test='seen = "Yes"'>
  90. <tr style="background-color:#666; color:#fff">
  91. <td> <xsl:number /></td>
  92. <td class="title"><xsl:value-of select="title"/></td>
  93. <td class="release"><xsl:value-of select="release"/></td>
  94. <td class="length"><xsl:value-of select="length" /> minutes</td>
  95. <td class="seen"><xsl:value-of select="seen"/></td>
  96. <td class="rating"><xsl:value-of select="rating"/></td>
  97. </tr>
  98. </xsl:when>
  99. <xsl:when test='seen = "Seen prior to making list"'>
  100. <tr style="background-color:#999; color:#000">
  101. <td> <xsl:number /></td>
  102. <td class="title"><xsl:value-of select="title"/></td>
  103. <td class="release"><xsl:value-of select="release"/></td>
  104. <td class="length"><xsl:value-of select="length"/> minutes</td>
  105. <td class="seen"><xsl:value-of select="seen"/></td>
  106. <td class="rating"><xsl:value-of select="rating"/></td>
  107. </tr>
  108. </xsl:when>
  109. <xsl:otherwise>
  110. <tr style="background-color:#fff;">
  111. <td> <xsl:number /></td>
  112. <td class="title"><xsl:value-of select="title"/></td>
  113. <td class="release"><xsl:value-of select="release"/></td>
  114. <td class="length"><xsl:value-of select="length" /> minutes</td>
  115. <td class="seen"><xsl:value-of select="seen"/></td>
  116. <td class="rating"><xsl:value-of select="rating"/></td>
  117. </tr>
  118. </xsl:otherwise>
  119. </xsl:choose>
  120. </xsl:for-each>
  121. </table>
  122. </body>
  123. </html>
  124. </xsl:template>
  125. </xsl:stylesheet>
  126. <!--Start XML-->
  127. <list>
  128. <movie>
  129. <title>2001: A Space Odyssey</title>
  130. <release>1968</release>
  131. <seen value="No">No</seen>
  132. <rating>N/A</rating>
  133. <length value="n">141</length>
  134. </movie>
  135. <movie>
  136. <title>28 Days Later</title>
  137. <release>2002</release>
  138. <seen value="No">No</seen>
  139. <rating>N/A</rating>
  140. <length value="n">113</length>
  141. </movie>
  142. <movie>
  143. <title>28 Weeks Later</title>
  144. <release>2007</release>
  145. <seen value="No">No</seen>
  146. <rating>N/A</rating>
  147. <length value="n">100</length>
  148. </movie>
  149. <movie>
  150. <title>A Clockwork Orange</title>
  151. <release>1971</release>
  152. <seen value="Yes">Yes</seen>
  153. <rating value="2">&#9734;&#9734;&#9734;&#9733;&#9733;</rating>
  154. <length value="y">136</length>
  155. </movie>
  156. <!--Rest of XML-->
  157. </list>
  158. </doc>

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

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

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

以下代码
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <?xml-stylesheet type="text/xsl" href="#stylesheet"?>
  3. <!DOCTYPE doc [
  4. <!ATTLIST xsl:stylesheet
  5. id ID #required>
  6. ]>
  7. <doc>
  8. <!--Start XSL-->
  9. <xsl:stylesheet id="stylesheet"
  10. version="1.0"
  11. xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
  12.  
  13. <xsl:template match="xsl:stylesheet" />
  14.  
  15. <!--Variables-->
  16. <xsl:variable name="a" select="/doc/list/movie/seen[@value ='Yes']" />
  17. <xsl:variable name="b" select="/doc/list/movie/seen" />
  18. <xsl:variable name="c" select="sum(/doc/list/movie/rating/@value)" />
  19. <xsl:variable name="d" select="$c div count($a)" />
  20. <xsl:variable name="e" select="count($a) div count($b)" />
  21. <xsl:variable name="f" select="/doc/list/movie/seen[@value ='No']" />
  22. <xsl:variable name="g" select="/doc/list/movie/seen[@value ='Prior']" />
  23. <xsl:variable name="h" select="count($f) div count($b)" />
  24. <xsl:variable name="j" select="count($g) div count($b)" />
  25. <xsl:variable name="minutes_total" select="sum(/doc/list/movie/length[@value ='y'])" />
  26. <xsl:variable name="minutes" select="$minutes_total mod 60" />
  27. <xsl:variable name="hours" select="floor($minutes_total div 60) mod 24" />
  28. <xsl:variable name="hours2" select="floor($minutes_total div 60)" />
  29. <xsl:variable name="days" select="floor($hours2 div 24)" />
  30. <!--End Variables-->
  31.  
  32. <xsl:decimal-format name="percent" />
  33. <xsl:decimal-format name="average" decimal-separator="." />
  34.  
  35. <xsl:template match="/doc">
  36. <html>
  37. <head>
  38. <style>
  39. h2{
  40. font-family: Courier,monospace;
  41. font-size: 32px;
  42. text-decoration: underline;
  43. }
  44. body{
  45. font-family: Courier New,monospace;
  46. }
  47. p{
  48. font-size: 16px;
  49. }
  50. table{
  51. font-size: 14px;
  52. }
  53. .title{
  54. text-align:left;
  55. }
  56. .release{
  57. text-align:center;
  58. }
  59. .seen{
  60. text-align:center;
  61. }
  62. .rating{
  63. text-align:right;
  64. }
  65. .length{
  66. text-align:center;
  67. }
  68. </style>
  69. </head>
  70. <body>
  71. <h2>My Movie List</h2>
  72. <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 />
  73. 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 />
  74. 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 />
  75. Total time watched: <xsl:value-of select="format-number($days,'#0')" /> minutes<br />
  76. Average rating: <xsl:value-of select="format-number($d,'average')" /> stars out of 5</p>
  77. <br />
  78. <table border="1">
  79. <tr>
  80. <th>#</th>
  81. <th>Title</th>
  82. <th>Release Date</th>
  83. <th>Length</th>
  84. <th>Seen</th>
  85. <th>Rating</th>
  86. </tr>
  87. <xsl:for-each select="list/movie">
  88. <xsl:choose>
  89. <xsl:when test='seen = "Yes"'>
  90. <tr style="background-color:#666; color:#fff">
  91. <td> <xsl:number /></td>
  92. <td class="title"><xsl:value-of select="title"/></td>
  93. <td class="release"><xsl:value-of select="release"/></td>
  94. <td class="length"><xsl:value-of select="length" /> minutes</td>
  95. <td class="seen"><xsl:value-of select="seen"/></td>
  96. <td class="rating"><xsl:value-of select="rating"/></td>
  97. </tr>
  98. </xsl:when>
  99. <xsl:when test='seen = "Seen prior to making list"'>
  100. <tr style="background-color:#999; color:#000">
  101. <td> <xsl:number /></td>
  102. <td class="title"><xsl:value-of select="title"/></td>
  103. <td class="release"><xsl:value-of select="release"/></td>
  104. <td class="length"><xsl:value-of select="length"/> minutes</td>
  105. <td class="seen"><xsl:value-of select="seen"/></td>
  106. <td class="rating"><xsl:value-of select="rating"/></td>
  107. </tr>
  108. </xsl:when>
  109. <xsl:otherwise>
  110. <tr style="background-color:#fff;">
  111. <td> <xsl:number /></td>
  112. <td class="title"><xsl:value-of select="title"/></td>
  113. <td class="release"><xsl:value-of select="release"/></td>
  114. <td class="length"><xsl:value-of select="length" /> minutes</td>
  115. <td class="seen"><xsl:value-of select="seen"/></td>
  116. <td class="rating"><xsl:value-of select="rating"/></td>
  117. </tr>
  118. </xsl:otherwise>
  119. </xsl:choose>
  120. </xsl:for-each>
  121. </table>
  122. </body>
  123. </html>
  124. </xsl:template>
  125. </xsl:stylesheet>
  126.  
  127. <!--Start XML-->
  128. <list>
  129. <movie>
  130. <title>2001: A Space Odyssey</title>
  131. <release>1968</release>
  132. <seen value="No">No</seen>
  133. <rating>N/A</rating>
  134. <length value="n">141</length>
  135. </movie>
  136. <movie>
  137. <title>28 Days Later</title>
  138. <release>2002</release>
  139. <seen value="No">No</seen>
  140. <rating>N/A</rating>
  141. <length value="n">113</length>
  142. </movie>
  143. <movie>
  144. <title>28 Weeks Later</title>
  145. <release>2007</release>
  146. <seen value="No">No</seen>
  147. <rating>N/A</rating>
  148. <length value="n">100</length>
  149. </movie>
  150. <movie>
  151. <title>A Clockwork Orange</title>
  152. <release>1971</release>
  153. <seen value="Yes">Yes</seen>
  154. <rating value="2">&#9734;&#9734;&#9734;&#9733;&#9733;</rating>
  155. <length value="y">136</length>
  156. </movie>
  157. <!--Rest of XML-->
  158. </list>
  159. </doc>

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

  1. My Movie List
  2.  
  3. Movies seen so far: 1/4 = 25%
  4. Movies yet to see: 3/4 = 75%
  5. Movies seen prior to making list: 0/4 = 0%
  6. Total time watched: 0 days,2 hours,16 minutes
  7. Average rating: 2.000 stars out of 5
  8.  
  9. # Title Release Date Length Seen Rating
  10. 1 2001: A Space Odyssey 1968 141 minutes No N/A
  11. 2 28 Days Later 2002 113 minutes No N/A
  12. 3 28 Weeks Later 2007 100 minutes No N/A
  13. 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中都能正常工作.没有其他浏览器被测试过.

猜你在找的XML相关文章