假设您有一个具有如下内容模型的元素:
<!ELEMENT wrapper (a*,b*,c*,d*,e*,f*,g*,h*,i*,j*,k*,l*,m*,n*,o*,p*,q*,r*,s*,t*,u*,v*,w*,x*,y*,z*)>
换句话说,在一个包装元素中,有一个指定顺序的子元素,这些子元素可以任意存在。
您需要在包装器中创建一个新元素(例如 m),同时保留已经存在的元素并确保输出符合内容模型。
这是一种解决方案:
<xsl:template match="@*|node()">
<xsl:sequence select="."/>
</xsl:template>
<xsl:template match="wrapper">
<xsl:copy>
<xsl:apply-templates select="a,b,c,d,e,f,g,h,i,j,k,l,m"/>
<m>This is new</m>
<xsl:apply-templates select="n,o,p,q,r,s,t,u,v,w,x,y,z"/>
</xsl:copy>
</xsl:template>
但是,此解决方案将删除包装器元素中的所有空格、注释或处理指令。我提出了一些不会放弃这些东西的解决方案,但没有一个让我满意。
解决此问题且不会丢弃节点的最优雅的解决方案是什么? XSLT 3 和模式感知解决方案很好。
以下是一些示例输入和输出:
<!-- Input -->
<wrapper/>
<!-- Output -->
<wrapper><m>This is new</m></wrapper>
<!-- Input -->
<wrapper><a/><z/></wrapper>
<!-- Output -->
<wrapper><a/><m>This is new</m><z/></wrapper>
<!-- Input -->
<wrapper><m/></wrapper>
<!-- Output -->
<wrapper><m/><m>This is new</m></wrapper>
<!-- Input -->
<wrapper>
<a/>
<!-- put m here -->
<z/>
</wrapper>
<!-- Output -->
<!-- NB: The comment and whitespace could come after the inserted element instead. This case is ambiguous -->
<wrapper>
<a/>
<!-- put m here --><m>This is new</m>
<z/>
</wrapper>
<!-- Input -->
<wrapper>
<a/>
<b/>
<c/>
<n/>
<o/>
<!-- p is not here -->
<?do not drop this?>
</wrapper>
<!-- Output -->
<wrapper>
<a/>
<b/>
<c/><m>This is new</m>
<n/>
<o/>
<!-- p is not here -->
<?do not drop this?>
</wrapper>
插入元素周围的非元素节点出现在之前或之后并不重要,只是它们不会被删除并且它们相对于原始元素的顺序得以保留。
最佳答案
这是您可以查看的一种方式:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="wrapper">
<xsl:variable name="all" select="node()"/>
<xsl:variable name="middle" select="(n,o,p,q,r,s,t,u,v,w,x,y,z)[1]"/>
<xsl:variable name="i" select="if($middle) then count($all[. << $middle]) else count($all)"/>
<xsl:variable name="new">
<m>This is new</m>
</xsl:variable>
<xsl:copy>
<xsl:apply-templates select="insert-before($all, $i+1, $new) "/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
或者,如果您愿意:
<xsl:template match="wrapper">
<xsl:variable name="all" select="node()"/>
<xsl:variable name="middle" select="(a,b,c,d,e,f,g,h,i,j,k,l,m)[last()]"/>
<xsl:variable name="i" select="if($middle) then index-of($all/generate-id(), generate-id($middle)) else 0"/>
<xsl:variable name="new">
<m>This is new</m>
</xsl:variable>
<xsl:copy>
<xsl:apply-templates select="insert-before($all, $i+1, $new) "/>
</xsl:copy>
</xsl:template>
关于xml - 当兄弟元素任意存在时,XSLT 按顺序创建元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45578164/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
如何使用RSpec::Core::RakeTask初始化RSpecRake任务?require'rspec/core/rake_task'RSpec::Core::RakeTask.newdo|t|#whatdoIputinhere?endInitialize函数记录在http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/RakeTask#initialize-instance_method没有很好的记录;它只是说:-(RakeTask)initialize(*args,&task_block)AnewinstanceofRake
我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?
我正在阅读SandiMetz的POODR,并且遇到了一个我不太了解的编码原则。这是代码:classBicycleattr_reader:size,:chain,:tire_sizedefinitialize(args={})@size=args[:size]||1@chain=args[:chain]||2@tire_size=args[:tire_size]||3post_initialize(args)endendclassMountainBike此代码将为其各自的属性输出1,2,3,4,5。我不明白的是查找方法。当一辆山地自行车被实例化时,因为它没有自己的initialize方法