在xml/xslt中分组/合并相同节点的子节点 [英] Group/merge childs of same nodes in xml/xslt
问题描述
我是XSLT的新手,手动更改它会花费很多时间.
I am new to XSLT and changing it manually will take a lot of time.
<GroupData ID="xxx" Key="4" Temp="yyy">
<ItemData ID="zzz" Value="3"/>
</GroupData>
<GroupData ID="xxx" Key="4" Temp="yyy">
<ItemData ID="www" Value="1982"/>
</GroupData>
我想在同一组中拥有这些多个GroupData节点的子节点,即
I want to have the childs of these multiple GroupData nodes within the same group, i.e.,
<GroupData ID="xxx" Key="4" Temp="yyy">
<ItemData ID="zzz" Value="3"/>
<ItemData ID="www" Value="1982"/>
</GroupData>
因此,我需要在GroupData的ID和Key属性上进行合并/组合/匹配(它们在文件中有所不同).还有一些没有Key属性.我怎样才能做到这一点?我读了其他一些线程(例如,在C#中,但我没有这个线程),我检查了W3学校,但这是非常基本的示例.我正在为Notepad ++使用最新的XML Tools 2.3.2 r908 unicode(beta4)来应用可能的转换(不知道它是否支持XSLT2.0或XSLT1.0).
So I need to merge/combine/match them on both GroupData's ID and Key attributes (these vary within the file). Also some do not have a Key attribute. How can I do that? I read some other threads (for example, in C# but I don't have that at my disposal) and I checked W3 schools but these are very basic examples. I am using the latest XML Tools 2.3.2 r908 unicode (beta4) for Notepad++ to apply possible transformations (do not know whether it supports XSLT2.0 or XSLT1.0).
在尝试了以下建议和各种建议之后,我陷入了困境,因为它具有多个级别,并且可能没有唯一的ID:
...
After trying the suggestions below and various things I am stuck since it has multiple levels and possiblly does not have unique IDs:
...
推荐答案
此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="kGDByIdKey" match="GroupData"
use="concat(@ID, '+', @Key)"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match=
"GroupData
[generate-id()
=
generate-id(key('kGDByIdKey', concat(@ID, '+', @Key))[1])
]">
<xsl:copy>
<xsl:apply-templates select=
"@*|key('kGDByIdKey', concat(@ID, '+', @Key))/node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="GroupData"/>
</xsl:stylesheet>
应用于此XML文档时:
<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>
产生想要的正确结果:
<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>
说明:
正确使用 Muenchian分组方法 和 身份规则 .
Proper use of the Muenchian grouping method and the identity rule.
这篇关于在xml/xslt中分组/合并相同节点的子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!