XSLT组/合并子级(使用键) [英] XSLT Group/merge childs (using key)
问题描述
我试图了解如何使用我已经编写的代码来推导解决方案.
I am trying to understand how to deduce a solution using a code I already wrote.
为简化起见,我将首先解释我想做的事情和到目前为止所获得的成就.
In order to simplify I will explain first what I want to do and what I got so far.
假设我在XSLT中有一个XML变量,其中包含几个具有相同title属性的节点.
Suppose I have an XML variable in XSLT containing few nodes with the same title attribute.
使用 @Dimitre Novatchev解决方案设法将它们组合成一个节点.
Using @Dimitre Novatchev solution I have managed to combine them into one node.
所以,如果我有:
<t>
<GroupData ID="xxx" Key="4" Temp="yyy">
<ItemData ID="zzz" Value="3"/>
</GroupData>
<GroupData ID="yyy" Key="4" Temp="yyy">
<ItemData ID="abc" Value="3"/>
</GroupData>
<GroupData ID="zzz" Temp="yyy">
<ItemData ID="pqr" Value="1982"/>
</GroupData>
<GroupData ID="xxx" Key="4" Temp="yyy">
<ItemData ID="www" Value="1982"/>
</GroupData>
<GroupData ID="yyy" Key="4" Temp="yyy">
<ItemData ID="def" Value="1982"/>
</GroupData>
<GroupData ID="zzz" Temp="yyy">
<ItemData ID="tuv" Value="1982"/>
</GroupData>
</t>
使用以下键之后
<xsl:key name="kGDByIdKey" match="GroupData" use="concat(@ID, '+', @Key)"/>
我会得到:
<t>
<GroupData ID="xxx" Key="4" Temp="yyy">
<ItemData ID="zzz" Value="3"/>
<ItemData ID="www" Value="1982"/>
</GroupData>
<GroupData ID="yyy" Key="4" Temp="yyy">
<ItemData ID="abc" Value="3"/>
<ItemData ID="def" Value="1982"/>
</GroupData>
<GroupData ID="zzz" Temp="yyy">
<ItemData ID="pqr" Value="1982"/>
<ItemData ID="tuv" Value="1982"/>
</GroupData>
</t>
现在,我想对此做一点修改,我希望能够根据自己的决定来合并/合并标题.考虑到这一点,在上面的示例中,我想将xxx和zzz定义在同一组中,尽管它们使用不同的标题(极端情况-在我的工作空间中,我将它们定义为相同-我可能会遇到更多类似这个).
Now I would like to modify this one a little bit, I would like to be able to merge/combine titles by my decision. In that in mind, in the example above I would like to define xxx and zzz to be in the same group although they are using a different title (Extreme cases - in my workspace I defined them to be identical - I might have more cases like this).
如果您能告诉我一般的操作方向,我将不胜感激(我想我需要修改密钥或使用其他方法-generate-id或其他方法).
I would appreciate if you can tell me what should be the direction to do it generally ( I suppose I need to modify my key or using alternative method - generate-id or something).
我发现自己只能实施需要很多不必要努力的不良解决方案.
I find myself implementing only bad solutions requiring a lot of unnecessary effort.
推荐答案
我猜这是XSLT1,这很可惜,因为它在XSLT2中看起来更好,但是无论如何,您基本上都需要确保要分组的节点结束用相同的键
I would guess this is XSLT1 which is a shame as it would look nicer in XSLT2 but anyway you basically need to ensure that nodes that you want to group together end up with the same key
<xsl:key name="kGDByIdKey" match="GroupData" use="concat(@ID, '+', @Key)"/>
只有具有相同的@ID和@key的事物才具有相同的use属性
things only get the same use attribute if they have the same @ID and @key
如果将其更改为
<xsl:key name="kGDByIdKey" match="GroupData[not(@ID='xxx')]" use="concat(@ID, '+', @Key)"/>
<xsl:key name="kGDByIdKey" match="GroupData[@ID='xxx'] use="concat('zzz', '+', @Key)"/>
然后将使用zzz
为ID为xxx
的节点建立索引(并因此进行分组)(当然,从该节点构造查找值时,您也需要进行类似的更改)
Then nodes with ID xxx
will be indexed (and therefore grouped) with zzz
(or course you need to make a similar change when you construct the lookup value from the node)
如果您使用的是xslt 2,则可以使用更简单的功能样式,将其扩展到多个这样的更改时,该样式可能不太笨拙
If you were using xslt 2you could use a simpler functional style that is perhaps less unweildy when extended to multiple such changes
<xsl:key name="kGDByIdKey" match="GroupData" use="concat(replace(@ID,'^xxx$','zzz'), '+', @Key)"/>
这篇关于XSLT组/合并子级(使用键)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!