我有以下 xml 代码:
<p>
<media id="pc300220-scpwr.gif" print-rights="no"
rights="licensed" type="photo">
<title>Louis Pasteur</title>
<credit>Granger Collection</credit>
</media>
<b>Louis</b>
<pronunciation>
<word-term>
<b>Pasteur</b>
</word-term>
</pronunciation> (1822–1895) was a French chemist. He made major contributions
to chemistry, medicine, and industry. His work has greatly benefited people. For
example, he discovered that diseases spread through
<definition>
<word-term>bacteria </word-term>
<word-definition>tiny living things</word-definition>
</definition>. This discovery has saved many millions of lives.
</p>
和以下 XSLT 段:
<xsl:template match="p|b|i">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
实际上会生成类似的输出
(18221895)
但是我想要
(1822-1895)
那么有人可以帮助解释为什么没有将 &ndash 复制到生成的 XML 中吗?
最佳答案
the following XSLT segment:
<xsl:template match="p|b|i">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
will actually generate the output like
(18221895)
But I want
(1822-1895)
我无法重现问题。
使用此 XML 文档(已更正以确保格式正确):
<!DOCTYPE p [
<!ENTITY ndash "–">
]>
<p>
<media id="pc300220-scpwr.gif" print-rights="no"
rights="licensed" type="photo">
<title>Louis Pasteur</title>
<credit>Granger Collection</credit>
</media>
<b>Louis</b>
<pronunciation>
<word-term>
<b>Pasteur</b>
</word-term>
</pronunciation> (1822–1895) was a French chemist. He made major contributions to chemistry, medicine, and industry. His work has greatly benefited people. For example, he discovered that diseases spread through
<definition>
<word-term>bacteria </word-term>
<word-definition>tiny living things</word-definition>
</definition>. This discovery has saved many millions of lives.
</p>
以及应用此转换时:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="p|b|i">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
结果是:
Louis Pasteur
Granger Collection
<b>Louis</b>
<b>Pasteur</b>
(1822–1895) was a French chemist. He made major contributions to chemistry, medicine, and industry. His work has greatly benefited people. For example, he discovered that diseases spread through
bacteria
tiny living things
. This discovery has saved many millions of lives.
关于xml - XSLT 不复制 HTML 实体的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5034225/
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat
我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c
我正在使用Rails构建一个简单的聊天应用程序。当用户输入url时,我希望将其输出为html链接(即“url”)。我想知道在Ruby中是否有任何库或众所周知的方法可以做到这一点。如果没有,我有一些不错的正则表达式示例代码可以使用... 最佳答案 查看auto_linkRails提供的辅助方法。这会将所有URL和电子邮件地址变成可点击的链接(htmlanchor标记)。这是文档中的代码示例。auto_link("Gotohttp://www.rubyonrails.organdsayhellotodavid@loudthinking.
我正在学习http://ruby.railstutorial.org/chapters/static-pages上的RubyonRails教程并遇到以下错误StaticPagesHomepageshouldhavethecontent'SampleApp'Failure/Error:page.shouldhave_content('SampleApp')Capybara::ElementNotFound:Unabletofindxpath"/html"#(eval):2:in`text'#./spec/requests/static_pages_spec.rb:7:in`(root)'
我正在尝试将一个简单的CSV文件读入HTML表格以在浏览器中显示,但我遇到了麻烦。这就是我正在尝试的:Controller:defshow@csv=CSV.open("file.csv",:headers=>true)end查看:输出:NameStartDateEndDateQuantityPostalCode基本上我只获取标题,而不会读取和呈现CSV正文。 最佳答案 这最终成为最终解决方案:Controller:defshow#OpenaCSVfile,andthenreaditintoaCSV::Tableobjectforda