-- 修改后的问题--
已经感谢所有提供潜在解决方案的人,但这些与我已经尝试过的一致,所以我想我应该更清楚。我稍微扩展了 XML 以使问题更加透明。
XML实际上是各种文件的汇编,包含翻译的内容,目的是得到一个统一的文档,只包含唯一的英文字符串,并且(经过人工审查和清理)每个字符串都有一个翻译的,所以它可以用于翻译内存库。这就是为什么它现在是一个包含大量冗余信息的大文件。
每一段行都包含英文母版(在文件中可以重复数十次)和翻译变体。在很多情况下,这很容易,因为所有翻译版本都是相同的,所以我最终会得到一行,但在其他情况下,它可能会更复杂。
所以,假设今天我有 10 行包含相同的英语内容 (#1)、2 种不同的德语变体、3 种不同的法语变体,而其余的语言环境我只需要一个变体:
1 Para 具有:1 EN/2 DE(v1 和 v2)/3 FR(v1、v2 和 v3)/...
这对我列表中的每个分组的唯一英语值重复
修改后的 XML:
<Books>
<!--First English String (#1) with number of potential translations -->
<Para>
<EN>English Content #1</EN>
<DE>German Trans of #1 v1</DE>
<FR>French Trans of #1 v1</FR>
<!-- More locales here -->
</Para>
<Para>
<EN>English Content #1</EN>
<DE>German Trans of #1 v2</DE>
<FR>French Trans of #1 v1</FR>
<!-- More locales here -->
</Para>
<Para>
<EN>English Content #1</EN>
<DE>German Trans of #1 v1</DE>
<FR>French Trans of #1 v2</FR>
<!-- More locales here -->
</Para>
<!--Second English String (#2) with number of potential translations -->
<Para>
<EN>English Content #2</EN>
<DE>German Trans of #2 v1</DE>
<FR>French Trans of #2 v1</FR>
<!-- More locales here -->
</Para>
<Para>
<EN>English Content #2</EN>
<DE>German Trans of #2 v3</DE>
<FR>French Trans of #2 v1</FR>
<!-- More locales here -->
</Para>
<Para>
<EN>English Content #2</EN>
<DE>German Trans of #2 v2</DE>
<FR>French Trans of #2 v1</FR>
<!-- More locales here -->
</Para>
<!--Loads of additional English Strings (#3 ~ #n) with number of potential translations -->
当前的解决方案为我提供了以下输出
<Books>
<Para>
<EN>English Content #1</EN>
<DE>German Trans of #1 v1</DE>
<DE>German Trans of #1 v2</DE>
<DE>German Trans of #2 v1</DE>
<DE>German Trans of #2 v3</DE>
<DE>German Trans of #2 v2</DE>
<FR>French Trans of #1 v1</FR>
<FR>French Trans of #1 v1</FR>
<FR>French Trans of #1 v2</FR>
<FR>French Trans of #2 v1</FR>
</Para>
</Books>
因此,只取第一个 EN 标签,然后将所有其他标签分组,与英文主字符串之间的差异无关。虽然我的目标是获得以下内容:
<Books>
<!-- First Grouped EN string and linked grouped translations -->
<Para>
<EN>English Content #1</EN>
<DE>German Trans of #1 v1</DE>
<DE>German Trans of #1 v2</DE>
<FR>French Trans of #1 v1</FR>
<FR>French Trans of #1 v2</FR>
</Para>
<!-- Second Grouped EN string and linked grouped translations -->
<Para>
<EN>English Content #2</EN>
<DE>German Trans of #2 v1</DE>
<DE>German Trans of #2 v3</DE>
<DE>German Trans of #2 v2</DE>
<FR>French Trans of #2 v1</FR>
</Para>
<!-- 3d to n Grouped EN string and linked grouped translations -->
</Books>
最佳答案
扩展 XSLT 2.0 答案以完成问题请求中的更新
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="Books">
<xsl:copy>
<xsl:for-each-group select="*"
group-by="EN">
<xsl:copy>
<xsl:copy-of select="EN"/>
<xsl:for-each-group select="current-group()/*[not(local-name()='EN')]"
group-by=".">
<xsl:sort select="local-name()"/>
<xsl:copy-of select="."/>
</xsl:for-each-group>
</xsl:copy>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
扩展 XSLT 1.0 答案以完成问题请求中的更新
即使您需要两种不同类型的 key ,您仍然可以使用相同类型的解决方案。这是第一个想到的简单解决方案:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="main" match="Para" use="EN"/>
<xsl:key name="locale" match="Para/*[not(self::EN)]" use="concat(../EN,.)"/>
<xsl:template match="Books">
<xsl:copy>
<xsl:apply-templates select="Para[
generate-id()
= generate-id(key('main',EN)[1])]" mode="EN"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*" mode="EN">
<xsl:copy>
<xsl:copy-of select="EN"/>
<xsl:apply-templates select="../Para/*[
generate-id()
= generate-id(key('locale',concat(current()/EN,.))[1])]" mode="locale">
<xsl:sort select="local-name()"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="*" mode="locale">
<xsl:copy>
<xsl:value-of select="."/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
应用时
n the new provided input, produces:
<Books>
<Para>
<EN>English Content #1</EN>
<DE>German Trans of #1 v1</DE>
<DE>German Trans of #1 v2</DE>
<FR>French Trans of #1 v1</FR>
<FR>French Trans of #1 v2</FR>
</Para>
<Para>
<EN>English Content #2</EN>
<DE>German Trans of #2 v1</DE>
<DE>German Trans of #2 v3</DE>
<DE>German Trans of #2 v2</DE>
<FR>French Trans of #2 v1</FR>
</Para>
</Books>
此 XSLT 1.0 转换完全符合您的要求,如果您愿意,它可以用作创建更有意义的结果树的起点:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="locale" match="Para/*[not(local-name()='EN')]" use="text()"/>
<xsl:template match="Books">
<xsl:copy>
<Para>
<xsl:copy-of select="Para[1]/EN"/>
<xsl:apply-templates select="Para/*[
generate-id()
= generate-id(key('locale',text())[1])]" mode="group">
<xsl:sort select="local-name()"/>
</xsl:apply-templates>
</Para>
</xsl:copy>
</xsl:template>
<xsl:template match="*" mode="group">
<xsl:copy>
<xsl:value-of select="."/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
解释:
xsl:key 用于按内容对所有元素进行分组(但 EN)PARA/EN节点的简单直接复制xsl:sort 输出按要求分组的其他元素(具有相同内容的元素报告一次)当应用于问题中提供的输入时,结果树是:
<Books>
<Para>
<EN>Some English Content</EN>
<DE>German Trans v1</DE>
<DE>German Trans v2</DE>
<FR>French Trans v1</FR>
<FR>French Trans v2</FR>
</Para>
</Books>
与 XSLT 2.0 xsl:for-each-group 相同的结果(和更短的转换):
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="Books">
<xsl:copy>
<Para>
<xsl:copy-of select="Para[1]/EN"/>
<xsl:for-each-group select="Para/*[not(local-name()='EN')]"
group-by=".">
<xsl:sort select="local-name()"/>
<xsl:copy>
<xsl:value-of select="."/>
</xsl:copy>
</xsl:for-each-group>
</Para>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
关于xml - 如何按内容对元素进行分组(XSLT 2.0)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6486761/
我正在学习如何使用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但我想要一些方法来使用
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]