所以我试图研究这个,但我几乎束手无策。我在 dpawson.co.uk 上找到了一种将 XSL 嵌入 XML 的方法, 但我不知道我做错了什么。我在互联网上搜索,试图找到解决方案和解释,但似乎没有人有答案。
我正在尝试创建一个独立于服务器的文件,这样我就可以将一个文件发送给我的同事,希望他们可以通过在 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">☆☆☆★★</rating>
<length value="y">136</length>
</movie>
<!--Rest of XML-->
</list>
</doc>
这是一个电影列表,其中包含我到目前为止看过的电影数量、我尚未看到的电影数量、我在列 list 之前看过的电影数量以及我投入的总时间看电影,以及每部电影的平均评分。
问题是所有变量都显示为零值,这会导致除以零,从而在某些输出上也给出 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">☆☆☆★★</rating>
<length value="y">136</length>
</movie>
<!--Rest of XML-->
</list>
</doc>
当另存为 XML 文件并在 FireFox 网络浏览器中打开时,生成如下所示的所需输出。此处不显示格式,但当您打开包含上述代码的 XML 文件时会显示在 FireFox 中。
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 放入单个文件格式时的元素。解决方案添加了 /doc之前/list在所有 XPath 表达式中,以便选择所需的元素,这些元素现在作为 <doc> 的后代嵌套得更深一层。 和 <list>而不仅仅是 <list> 的后代(就像两个文件分开时一样)。
注意 2:为了与 IE 8 兼容,xml-stylesheet 类型也更改为“text/xsl”。对于“text/xml”类型,IE 8 仅显示文档树(包括 XSL)作为 XML,而对于“text/xsl”,它尝试——尽管相当不成功——应用转换。需要做更多工作才能在 IE 8 中正确显示此内容。请注意,任一类型值在 FireFox 7 中都可以正常工作。未测试其他浏览器。
关于xml - 在 XML 中嵌入 XSL 以在 Web 浏览器中显示 : What am I doing wrong?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9523174/
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
我正在尝试在Ruby中制作一个cli应用程序,它接受一个给定的数组,然后将其显示为一个列表,我可以使用箭头键浏览它。我觉得我已经在Ruby中看到一个库已经这样做了,但我记不起它的名字了。我正在尝试对soundcloud2000中的代码进行逆向工程做类似的事情,但他的代码与SoundcloudAPI的使用紧密耦合。我知道cursesgem,我正在考虑更抽象的东西。广告有没有人见过可以做到这一点的库或一些概念证明的Ruby代码可以做到这一点? 最佳答案 我不知道这是否是您正在寻找的,但也许您可以使用我的想法。由于我没有关于您要完成的工作
我的主要目标是能够完全理解我正在使用的库/gem。我尝试在Github上从头到尾阅读源代码,但这真的很难。我认为更有趣、更温和的踏脚石就是在使用时阅读每个库/gem方法的源代码。例如,我想知道RubyonRails中的redirect_to方法是如何工作的:如何查找redirect_to方法的源代码?我知道在pry中我可以执行类似show-methodmethod的操作,但我如何才能对Rails框架中的方法执行此操作?您对我如何更好地理解Gem及其API有什么建议吗?仅仅阅读源代码似乎真的很难,尤其是对于框架。谢谢! 最佳答案 Ru
s=Socket.new(Socket::AF_INET,Socket::SOCK_STREAM,0)s.connect(Socket.pack_sockaddr_in('port','hostname'))ssl=OpenSSL::SSL::SSLSocket.new(s,sslcert)ssl.connect从这里开始,如果ssl连接和底层套接字仍然是ESTABLISHED,或者它是否在默认值7200之后进入CLOSE_WAIT,我想检查一个线程几秒钟甚至更糟的是在实际上不需要.write()或.read()的情况下关闭。是用select()、IO.select()还是其他方法完成
我最喜欢的Google文档功能之一是它会在我工作时不断自动保存我的文档版本。这意味着即使我在进行关键更改之前忘记在某个点进行保存,也很有可能会自动创建一个保存点。至少,我可以将文档恢复到错误更改之前的状态,并从该点继续工作。对于在MacOS(或UNIX)上运行的Ruby编码器,是否有具有等效功能的工具?例如,一个工具会每隔几分钟自动将Gitcheckin我的本地存储库以获取我正在处理的文件。也许我有点偏执,但这点小保险可以让我在日常工作中安心。 最佳答案 虚拟机有些人可能讨厌我对此的回应,但我在编码时经常使用VIM,它具有自动保存功
我正在使用Ruby/Mechanize编写一个“自动填写表格”应用程序。它几乎可以工作。我可以使用精彩CharlesWeb代理以查看服务器和我的Firefox浏览器之间的交换。现在我想使用Charles查看服务器和我的应用程序之间的交换。Charles在端口8888上代理。假设服务器位于https://my.host.com。.一件不起作用的事情是:@agent||=Mechanize.newdo|agent|agent.set_proxy("my.host.com",8888)end这会导致Net::HTTP::Persistent::Error:...lib/net/http/pe
我要下载http://foobar.com/song.mp3作为song.mp3,而不是让Chrome在其native中打开它浏览器中的播放器。我怎样才能做到这一点? 最佳答案 您只需要确保发送这些header:Content-Disposition:attachment;filename=song.mp3;Content-Type:application/octet-streamContent-Transfer-Encoding:binarysend_file方法为您完成:get'/:file'do|file|file=File.
有没有人得到Logstash在Rails上使用ruby?我的客户告诉我将Logstash用于日志收集器等。我正在使用rubyonrails技术。大部分都快完成了。但要求是将日志记录到logstash中。请让我知道这可能吗? 最佳答案 我为此编写了一个gem-logstasher.它将Rails日志写入一个单独的文件,采用纯json格式,无需任何处理即可由logstash使用。查看我的blog有关如何设置Logstash和Kibana的完整说明 关于ruby-on-rails-Lo
我使用“newapp_name”创建了一个新的Rails应用程序,我正在尝试编辑.gitignore文件,但在我的应用程序文件夹中找不到它。我在哪里可以找到它?我安装了Git。 最佳答案 .gitignore位于项目的root中,而不是app子目录中。首先打开终端并进入您的目录。您需要使用ls-a来显示stash文件。然后使用打开.gitignore 关于ruby-on-rails-尝试打开.gitignore以在文本编辑器中对其进行编辑,但在OSXMountainLion上找不到文件位
我在这方面尝试了很多URL,在我遇到这个特定的之前,它们似乎都很好:require'rubygems'require'nokogiri'require'open-uri'doc=Nokogiri::HTML(open("http://www.moxyst.com/fashion/men-clothing/underwear.html"))putsdoc这是结果:/Users/macbookair/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/open-uri.rb:353:in`open_http':404NotFound(OpenURI::HT