在 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 学校,但这些是非常基本的示例.我正在使用最新的 XML Tools 2.3.2 r908 unicode (beta4) for Notepad++ 来应用可能的转换(不知道它是否支持 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>
说明:
Proper use of the Muenchian grouping method and the identity rule.
这篇关于在 xml/xslt 中分组/合并相同节点的子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!