我有多种类型的 xml 消息,我需要通过将多个节点分组到同一父节点下来“压缩”(同一父节点意味着它们共享相同的节点名称,并且声明的每个属性也相等)。例如:
<TopLevel CodeTL="Something">
<Ratings>
<Rating CodeA="ABC" Start="1-1-2012" End="1-2-2012">
<RatingByNumber Code="X" Rating="10" Number="1">
<RatingByNumber Code="X" Rating="19" Number="2">
</Rating>
</Ratings>
</TopLevel>
<TopLevel CodeTL="Something">
<Ratings>
<Rating CodeA="ABC" Start="1-2-2012" End="1-3-2012">
<RatingByNumber Code="X" Rating="10" Number="1">
<RatingByNumber Code="X" Rating="19" Number="2">
</Rating>
</Ratings>
</TopLevel>
<TopLevel CodeTL="Something">
<Ratings>
<Rating CodeA="XYZ" Start="1-2-2012" End="1-3-2012">
<RatingByNumber Code="X" Rating="10" Number="1">
<RatingByNumber Code="X" Rating="19" Number="2">
</Rating>
</Ratings>
</TopLevel>
<TopLevel CodeTL="Something">
<Ratings>
<Rating CodeA="XYZ" Start="1-2-2012" End="1-3-2012">
<RatingByNumber Code="X" Rating="30" Number="3">
<RatingByNumber Code="X" Rating="39" Number="4">
</Rating>
</Ratings>
</TopLevel>
注意它们如何共享相同的 CodeTL 属性,最后两个共享相同的 CodeA、Start 和 End 属性,所以我需要的是使用 xslt 生成以下输出
<TopLevel CodeTL="Something">
<Ratings>
<Rating CodeA="ABC" Start="1-1-2012" End="1-2-2012">
<RatingByNumber Code="X" Rating="10" Number="1">
<RatingByNumber Code="X" Rating="19" Number="2">
</Rating>
<Rating CodeA="ABC" Start="1-2-2012" End="1-3-2012">
<RatingByNumber Code="X" Rating="10" Number="1">
<RatingByNumber Code="X" Rating="19" Number="2">
</Rating>
<Rating CodeA="XYZ" Start="1-2-2012" End="1-3-2012">
<RatingByNumber Code="X" Rating="10" Number="1">
<RatingByNumber Code="X" Rating="19" Number="2">
<RatingByNumber Code="X" Rating="30" Number="3">
<RatingByNumber Code="X" Rating="39" Number="4">
</Rating>
</Ratings>
</TopLevel>
它更干净,并且根据使用它的应用程序,它可能会节省处理时间和空间。
我遇到的问题是,我有不同类型的 xml 消息,它们具有不同的节点名称和属性(以及属性数量),但它们都共享我在此处显示的相同结构。 这将是一种处理所有这些问题的通用方法,但我很感激 XSLT 可以转换我提供的示例,这样我就可以为我需要发送的每条 xml 消息创建自定义代码。
最佳答案
这个通用的 XSLT 2.0 转换:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:my="my:my" exclude-result-prefixes="xs my">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<t>
<xsl:sequence select="my:grouping(*)"/>
</t>
</xsl:template>
<xsl:function name="my:grouping" as="node()*">
<xsl:param name="pElems" as="element()*"/>
<xsl:if test="$pElems">
<xsl:for-each-group select="$pElems" group-by="my:signature(.)">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:sequence select="my:grouping(current-group()/*)"/>
</xsl:copy>
</xsl:for-each-group>
</xsl:if>
</xsl:function>
<xsl:function name="my:signature" as="xs:string">
<xsl:param name="pElem" as="element()"/>
<xsl:variable name="vsignAttribs" as="xs:string*">
<xsl:for-each select="$pElem/@*">
<xsl:sort select="name()"/>
<xsl:value-of select="concat(name(), '=', .,'|')"/>
</xsl:for-each>
</xsl:variable>
<xsl:sequence select=
"concat(name($pElem), '|', string-join($vsignAttribs, ''))"/>
</xsl:function>
</xsl:stylesheet>
应用于提供的 XML 时(包装到单个顶级元素中以成为格式良好的 XML 文档):
<t>
<TopLevel CodeTL="Something">
<Ratings>
<Rating CodeA="ABC" Start="1-1-2012" End="1-2-2012">
<RatingByNumber Code="X" Rating="10" Number="1"/>
<RatingByNumber Code="X" Rating="19" Number="2"/>
</Rating>
</Ratings>
</TopLevel>
<TopLevel CodeTL="Something">
<Ratings>
<Rating CodeA="ABC" Start="1-2-2012" End="1-3-2012">
<RatingByNumber Code="X" Rating="10" Number="1"/>
<RatingByNumber Code="X" Rating="19" Number="2"/>
</Rating>
</Ratings>
</TopLevel>
<TopLevel CodeTL="Something">
<Ratings>
<Rating CodeA="XYZ" Start="1-2-2012" End="1-3-2012">
<RatingByNumber Code="X" Rating="10" Number="1"/>
<RatingByNumber Code="X" Rating="19" Number="2"/>
</Rating>
</Ratings>
</TopLevel>
<TopLevel CodeTL="Something">
<Ratings>
<Rating CodeA="XYZ" Start="1-2-2012" End="1-3-2012">
<RatingByNumber Code="X" Rating="30" Number="3"/>
<RatingByNumber Code="X" Rating="39" Number="4"/>
</Rating>
</Ratings>
</TopLevel>
</t>
产生想要的、正确的结果:
<t>
<TopLevel CodeTL="Something">
<Ratings>
<Rating CodeA="ABC" Start="1-1-2012" End="1-2-2012">
<RatingByNumber Code="X" Rating="10" Number="1"/>
<RatingByNumber Code="X" Rating="19" Number="2"/>
</Rating>
<Rating CodeA="ABC" Start="1-2-2012" End="1-3-2012">
<RatingByNumber Code="X" Rating="10" Number="1"/>
<RatingByNumber Code="X" Rating="19" Number="2"/>
</Rating>
<Rating CodeA="XYZ" Start="1-2-2012" End="1-3-2012">
<RatingByNumber Code="X" Rating="10" Number="1"/>
<RatingByNumber Code="X" Rating="19" Number="2"/>
<RatingByNumber Code="X" Rating="30" Number="3"/>
<RatingByNumber Code="X" Rating="39" Number="4"/>
</Rating>
</Ratings>
</TopLevel>
</t>
解释:
执行的分组在函数 my:grouping() 中实现,并且是递归的。
顶层元素在其级别上是单一的,除了自身的浅拷贝外不需要任何其他分组。然后在这个浅拷贝的主体内,较低级别的分组由函数 my:grouping() 执行。
my:grouping() 函数有一个参数,即直接上层组中所有元素的所有子元素。它返回当前级别的所有组。
作为参数传递给函数的元素序列根据它们的签名进行分组——元素名称与其属性的所有名称-值对的串联以及它们相应的值,并使用适当的定界符分隔这些值。元素的签名由函数 my:signature() 生成。
二。通用 XSLT 1.0 解决方案:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
xmlns:my="my:my" exclude-result-prefixes="my ext">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="vrtfPass1">
<xsl:apply-templates select="/*"/>
</xsl:variable>
<xsl:variable name="vPass1" select="ext:node-set($vrtfPass1)"/>
<xsl:template match="/">
<xsl:apply-templates select="$vPass1/*" mode="pass2"/>
</xsl:template>
<xsl:template match="/*" mode="pass2">
<xsl:copy>
<xsl:call-template name="my:grouping">
<xsl:with-param name="pElems" select="*"/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="my:grouping">
<xsl:param name="pElems" select="/.."/>
<xsl:if test="$pElems">
<xsl:for-each select="$pElems">
<xsl:variable name="vPos" select="position()"/>
<xsl:if test=
"not(current()/@my:sign
= $pElems[not(position() >= $vPos)]/@my:sign
)">
<xsl:element name="{name()}">
<xsl:copy-of select="namespace::*[not(. = 'my:my')]"/>
<xsl:copy-of select="@*[not(name()='my:sign')]"/>
<xsl:call-template name="my:grouping">
<xsl:with-param name="pElems" select=
"$pElems[@my:sign = current()/@my:sign]/*"/>
</xsl:call-template>
</xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:if>
</xsl:template>
<xsl:template match="/*">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="*/*">
<xsl:variable name="vSignature">
<xsl:call-template name="signature"/>
</xsl:variable>
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="my:sign">
<xsl:value-of select="$vSignature"/>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template name="signature">
<xsl:variable name="vsignAttribs">
<xsl:for-each select="@*">
<xsl:sort select="name()"/>
<xsl:value-of select="concat(name(), '=', .,'|')"/>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select=
"concat(name(), '|', $vsignAttribs)"/>
</xsl:template>
</xsl:stylesheet>
当这个转换应用于相同的 XML 文档时(如上),再次产生相同的正确结果:
<t>
<TopLevel>
<Ratings>
<Rating CodeA="ABC" Start="1-1-2012" End="1-2-2012">
<RatingByNumber Code="X" Rating="10" Number="1"/>
<RatingByNumber Code="X" Rating="19" Number="2"/>
</Rating>
<Rating CodeA="ABC" Start="1-2-2012" End="1-3-2012">
<RatingByNumber Code="X" Rating="10" Number="1"/>
<RatingByNumber Code="X" Rating="19" Number="2"/>
</Rating>
<Rating CodeA="XYZ" Start="1-2-2012" End="1-3-2012">
<RatingByNumber Code="X" Rating="10" Number="1"/>
<RatingByNumber Code="X" Rating="19" Number="2"/>
<RatingByNumber Code="X" Rating="30" Number="3"/>
<RatingByNumber Code="X" Rating="39" Number="4"/>
</Rating>
</Ratings>
</TopLevel>
</t>
解释:
这是一个两次转换。
在第一遍中为每个元素计算一个签名,它成为新属性 my:sign 的值。
使用与 XSLT 2.0 解决方案相同的递归分组算法。
关于xml - xslt 按每个属性分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11403760/
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2
我有这个html标记:我想得到这个:我如何使用Nokogiri做到这一点? 最佳答案 require'nokogiri'doc=Nokogiri::HTML('')您可以通过xpath删除所有属性:doc.xpath('//@*').remove或者,如果您需要做一些更复杂的事情,有时使用以下方法遍历所有元素会更容易:doc.traversedo|node|node.keys.eachdo|attribute|node.deleteattributeendend 关于ruby-Nokog
对于Rails模型,是否可以/建议让一个类的成员不持久保存到数据库中?我想将用户最后选择的类型存储在session变量中。由于我无法从我的模型中设置session变量,我想将值存储在一个“虚拟”类成员中,该成员只是将值传递回Controller。你能有这样的类(class)成员吗? 最佳答案 将非持久属性添加到Rails模型就像任何其他Ruby类一样:classUser扩展解释:在Ruby中,所有实例变量都是私有(private)的,不需要在赋值前定义。attr_accessor创建一个setter和getter方法:classUs
假设我有一个在Ruby中看起来像这样的哈希:{:ie0=>"Hi",:ex0=>"Hey",:eg0=>"Howdy",:ie1=>"Hello",:ex1=>"Greetings",:eg1=>"Goodday"}有什么好的方法可以将它变成如下内容:{"0"=>{"ie"=>"Hi","ex"=>"Hey","eg"=>"Howdy"},"1"=>{"ie"=>"Hello","ex"=>"Greetings","eg"=>"Goodday"}} 最佳答案 您要求一个好的方法来做到这一点,所以答案是:一种您或同事可以在六个月后理解
所以这可能有点令人困惑,但请耐心等待。简而言之,我想遍历具有特定键值的所有属性,然后如果值不为空,则将它们插入到模板中。这是我的代码:属性:#===DefaultfileConfigurations#default['elasticsearch']['default']['ES_USER']=''default['elasticsearch']['default']['ES_GROUP']=''default['elasticsearch']['default']['ES_HEAP_SIZE']=''default['elasticsearch']['default']['MAX_OP
假设我有以下类(class):classPersondefinitialize(name,age)@name=name@age=ageenddefget_agereturn@ageendend我有一组Person对象。是否有一种简洁的、类似于Ruby的方法来获取最小(或最大)年龄的人?如何根据它对它们进行排序? 最佳答案 这样做会:people_array.min_by(&:get_age)people_array.max_by(&:get_age)people_array.sort_by(&:get_age)