我试图弄清楚 XSLT 如何处理 namespace 前缀并有以下示例: XML:
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:zno="http://feed.zinio.com/atom"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/2005/Atom
http://www.w3.org/1999/xhtml
http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd
http://feed.zinio.com/atom" >
<entry>
<author>
<name>By Sheila F. Buckmaster</name>
</author>
<category xml:lang="en" term="TRAVEL"/>
<content>
<h2 class="hl2">One of the world’s most entrancing cities becomes even more captivating when costumed revelers fill its tiny streets and grand piazzas during Carnevale. It is here that a star of the silent screen comes alive, antics and all</h2>
<div class="byline">By Sheila F. Buckmaster</div>
</content>
</entry>
</feed>
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xslt:stylesheet version="1.0" xmlns:xslt="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="urn:my-scripts"
xmlns:x="http://www.w3.org/1999/xhtml"
xmlns:AP="http://www.w3.org/2005/Atom"
exclude-result-prefixes="xslt msxsl user">
<xslt:output method="xml" indent="yes"/>
<xslt:template match="/">
<xslt:apply-templates select="/AP:feed//AP:entry"/>
</xslt:template>
<xslt:template match="AP:entry">
<xslt:text>Hello from entry</xslt:text>
<xslt:apply-templates select="AP:content"/>
</xslt:template>
<xslt:template match="AP:content">
<xslt:text>Hello from content</xslt:text>
<xslt:apply-templates select="x:div[@class='byline']"/>
</xslt:template>
<xslt:template match="x:div[@class='byline']">
<xslt:copy-of select="."/>
</xslt:template>
</xslt:stylesheet>
我想做的是访问我的“div”。 “条目”和“内容”模板工作正常,因为我明确指定了命名空间。但是当我尝试使用 XHTML 前缀(在我的例子中是“x”)访问“div”时——XSLT 看不到它。仅当我在“div”元素前加上“AP”命名空间时它才有效:
<xslt:template match="AP:content">
<xslt:text>Hello from content</xslt:text>
<xslt:apply-templates select="AP:div[@class='byline']"/>
</xslt:template>
<xslt:template match="AP:div[@class='byline']">
<xslt:copy-of select="."/>
</xslt:template>
但这对我来说不合适,因为 DIV 元素应该在 XHTML 命名空间中。我在这里做错了什么?
最佳答案
Atom 提要在没有 namespace 前缀的根元素上声明了 Atom namespace 。 <div/>和其他 XHTML 元素正在继承 Atom 命名空间,因为它们没有显式声明 XHTML 命名空间。
如果您希望将 XHTML 元素绑定(bind)到 XHTML namespace ,那么您需要更改 <div>在 Atom 提要中:
<div xmlns:xhtml="http://www.w3.org/1999/xhtml" class="byline">By Sheila F. Buckmaster</div>
或:
<xhtml:div class="byline">By Sheila F. Buckmaster</xhtml:div>
如果您保持 Atom 提要不变但仍想生成 XHTML 元素,那么您将需要调整样式表以匹配 AP:div然后在输出中构建 XHTML 元素。
例如,修改样式表 I apply-templates在匹配的 AP:div 上在名为 xhtml 的模式中.在该模式下有一个匹配任何元素的模板(因此它也适用于 AP:h2 ),它使用 local-name() 构造 XHTML 元素。匹配元素的。
<?xml version="1.0" encoding="UTF-8"?>
<xslt:stylesheet version="1.0" xmlns:xslt="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="urn:my-scripts"
xmlns:x="http://www.w3.org/1999/xhtml"
xmlns:AP="http://www.w3.org/2005/Atom"
exclude-result-prefixes="xslt msxsl user">
<xslt:output method="xml" indent="yes"/>
<xslt:template match="/">
<xslt:apply-templates select="/AP:feed//AP:entry"/>
</xslt:template>
<xslt:template match="AP:entry">
<xslt:text>Hello from entry</xslt:text>
<xslt:apply-templates select="AP:content"/>
</xslt:template>
<xslt:template match="AP:content">
<xslt:text>Hello from content</xslt:text>
<xslt:apply-templates select="AP:div[@class='byline']"/>
</xslt:template>
<xslt:template match="AP:div[@class='byline']">
<xslt:apply-templates select="." mode="xhtml"/>
</xslt:template>
<!--create an XHTML element with the same name as the context element -->
<xslt:template match="*" mode="xhtml">
<xslt:element name="{local-name()}" namespace="http://www.w3.org/1999/xhtml">
<xslt:apply-templates select="@*|node()" mode="xhtml"/>
</xslt:element>
</xslt:template>
<!--attributes, comments, and processing-instructions simply copied -->
<xslt:template match="@*|text()|comment()|processing-instruction()">
<xslt:copy-of select="."/>
</xslt:template>
</xslt:stylesheet>
关于xml - XSLT 和 XHTML 输出的默认命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9762005/
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
这是在Ruby中设置默认值的常用方法:classQuietByDefaultdefinitialize(opts={})@verbose=opts[:verbose]endend这是一个容易落入的陷阱:classVerboseNoMatterWhatdefinitialize(opts={})@verbose=opts[:verbose]||trueendend正确的做法是:classVerboseByDefaultdefinitialize(opts={})@verbose=opts.include?(:verbose)?opts[:verbose]:trueendend编写Verb
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
这是一道面试题,我没有答对,但还是很好奇怎么解。你有N个人的大家庭,分别是1,2,3,...,N岁。你想给你的大家庭拍张照片。所有的家庭成员都排成一排。“我是家里的friend,建议家庭成员安排如下:”1岁的家庭成员坐在这一排的最左边。每两个坐在一起的家庭成员的年龄相差不得超过2岁。输入:整数N,1≤N≤55。输出:摄影师可以拍摄的照片数量。示例->输入:4,输出:4符合条件的数组:[1,2,3,4][1,2,4,3][1,3,2,4][1,3,4,2]另一个例子:输入:5输出:6符合条件的数组:[1,2,3,4,5][1,2,3,5,4][1,2,4,3,5][1,2,4,5,3][
两者都可以defsetup(options={})options.reverse_merge:size=>25,:velocity=>10end和defsetup(options={}){:size=>25,:velocity=>10}.merge(options)end在方法的参数中分配默认值。问题是:哪个更好?您更愿意使用哪一个?在性能、代码可读性或其他方面有什么不同吗?编辑:我无意中添加了bang(!)...并不是要询问nobang方法与bang方法之间的区别 最佳答案 我倾向于使用reverse_merge方法:option
当我在我的Rails应用程序根目录中运行rakedoc:app时,API文档是使用/doc/README_FOR_APP作为主页生成的。我想向该文件添加.rdoc扩展名,以便它在GitHub上正确呈现。更好的是,我想将它移动到应用程序根目录(/README.rdoc)。有没有办法通过修改包含的rake/rdoctask任务在我的Rakefile中执行此操作?是否有某个地方可以查找可以修改的主页文件的名称?还是我必须编写一个新的Rake任务?额外的问题:Rails应用程序的两个单独文件/README和/doc/README_FOR_APP背后的逻辑是什么?为什么不只有一个?
我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use
我想使用spawn(针对多个并发子进程)在Ruby中执行一个外部进程,并将标准输出或标准错误收集到一个字符串中,其方式类似于使用Python的子进程Popen.communicate()可以完成的操作。我尝试将:out/:err重定向到一个新的StringIO对象,但这会生成一个ArgumentError,并且临时重新定义$stdxxx会混淆子进程的输出。 最佳答案 如果你不喜欢popen,这是我的方法:r,w=IO.pipepid=Process.spawn(command,:out=>w,:err=>[:child,:out])