请建议 xpath 以检查元素“mo”是否在“mfrac”的第一个子元素中以“文本节点”开头。目前的 XSLT 代码对于所有不应嵌套在另一个“mfrac”中的“mfrac”都成功运行(数学 1 和 2 运行成功,但数学 3 不成功)。如果 'mfrac' 被另一个 'mfrac' 嵌套,则会出现一些错误消息。如果“mo”在“mfrac/child::*”(“mo”作为第一个文本节点)中没有位于文本节点之前,则需要输出“mo”应该获得属性“form=prefix”。请建议如何避免错误消息。忽略结果文本中的评论。
XML:
<article>
<body>
<p>
<math id="eq1"><mi>i</mi>
<mfrac>
<mrow><mo>+</mo></mrow>
<mrow><mi>t</mi></mrow>
</mfrac>
</math>
<math id="eq2">
<mfrac>
<mrow><mi>i</mi><mo>+</mo></mrow>
<mrow><mi>t</mi></mrow>
</mfrac>
</math>
<math id="eq3"><mi>i</mi><mfrac><mrow><mfrac><mo>+</mo><mi>n</mi></mfrac></mrow><mrow><mi>t</mi></mrow></mfrac></math>
</p>
</body>
</article>
XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>
<xsl:template match="mo">
<xsl:choose>
<xsl:when test="string-length(preceding::text()[1]
[normalize-space(.)!='']
[generate-id(ancestor-or-self::*[count(preceding-sibling::*)=0]/parent::*[name()='mfrac'])=generate-id(current()/ancestor-or-self::*[count(preceding-sibling::*)=0]/parent::*[name()='mfrac'])]) eq 0">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:attribute name="form" select="'prefix'"/>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
要求的输出:
<article>
<body>
<p>
<mmlmath id="eq1"><mi>i</mi>
<mfrac>
<mrow><mo form="prefix">+</mo></mrow><!--Here prefix attribute required because MO found as first text node within MFRAC's first child-->
<mrow><mi>t</mi></mrow>
</mfrac>
</mmlmath>
<mmlmath id="eq2">
<mfrac>
<mrow><mi>i</mi><mo>+</mo></mrow><!--Here attribute not required because 'MO' is not a first text node-->
<mrow><mi>t</mi></mrow>
</mfrac>
</mmlmath>
<mmlmath id="eq3"><mi>i</mi><mfrac><mrow><mfrac><!--Nested MFRAC--><mo form="prefix">+</mo><!--Attribute required here--><mi>n</mi></mfrac></mrow><mrow><mi>t</mi></mrow></mfrac></mmlmath>
</p>
</body>
</article>
错误信息:
XPTY0004: A sequence of more than one item is not allowed as the first argument of generate-id() (<mfrac/>, <mfrac/>)
最佳答案
您应该能够测试当前的 mo 是否是包含 text() 的第一个祖先 mfrac 的第一个后代> 节点。
希望这是有道理的。 :-)
这是一个应该有助于...的示例
XML 输入
<article>
<body>
<p>
<math id="eq1">
<mi>i</mi>
<mfrac>
<mrow>
<mo>+</mo>
</mrow>
<mrow>
<mi>t</mi>
</mrow>
</mfrac>
</math>
<math id="eq2">
<mfrac>
<mrow>
<mi>i</mi>
<mo>+</mo>
</mrow>
<mrow>
<mi>t</mi>
</mrow>
</mfrac>
</math>
<math id="eq3">
<mi>i</mi>
<mfrac>
<mrow>
<mfrac>
<mo>+</mo>
<mi>n</mi>
</mfrac>
</mrow>
<mrow>
<mi>t</mi>
</mrow>
</mfrac>
</math>
</p>
</body>
</article>
XSLT 2.0
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="mo[. is (ancestor::mfrac[1]//*[text()])[1]]">
<xsl:copy>
<xsl:attribute name="form" select="'prefix'"/>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
XML 输出
<article>
<body>
<p>
<math id="eq1">
<mi>i</mi>
<mfrac>
<mrow>
<mo form="prefix">+</mo>
</mrow>
<mrow>
<mi>t</mi>
</mrow>
</mfrac>
</math>
<math id="eq2">
<mfrac>
<mrow>
<mi>i</mi>
<mo>+</mo>
</mrow>
<mrow>
<mi>t</mi>
</mrow>
</mfrac>
</math>
<math id="eq3">
<mi>i</mi>
<mfrac>
<mrow>
<mfrac>
<mo form="prefix">+</mo>
<mi>n</mi>
</mfrac>
</mrow>
<mrow>
<mi>t</mi>
</mrow>
</mfrac>
</math>
</p>
</body>
</article>
关于xml - 建议 xpath 检查相同类型的嵌套元素中的第一个 'text' 节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32252018/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何