Reputation: 59
I have a XML like the following:
<DataSet>
<FirstNode>
<UniqueKey>111</UniqueKey>
</FirstNode>
<FirstNode>
<UniqueKey>123</UniqueKey>
</FirstNode>
<FirstNode>
<UniqueKey>144</UniqueKey>
</FirstNode>
<SecondNode>
<FirstNodeKey>111</FirstNodeKey>
</SecondNode>
<SecondNode>
<FirstNodeKey>123</FirstNodeKey>
</SecondNode>
<SecondNode>
<FirstNodeKey>144</FirstNodeKey>
</SecondNode>
</DataSet>
I want to use XSLT to get:
<DataSet>
<FirstNode>
<UniqueKey>111</UniqueKey>
</FirstNode>
<SecondNode>
<FirstNodeKey>111</FirstNodeKey>
</SecondNode>
</DataSet>
<DataSet>
<FirstNode>
<UniqueKey>123</UniqueKey>
</FirstNode>
<SecondNode>
<FirstNodeKey>123</FirstNodeKey>
</SecondNode>
</DataSet>
<DataSet>
<FirstNode>
<UniqueKey>144</UniqueKey>
</FirstNode>
<SecondNode>
<FirstNodeKey>144</FirstNodeKey>
</SecondNode>
</DataSet>
If the tag is the same, I can use:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="DataSet">
<xsl:for-each-group select="*" group-by="UniqueKey">
<Updates>
<xsl:copy-of select="current-group()"/>
</Updates>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
How can I do this when the tags are different? How can I group on these two different tags? (UniqueKey and FirstNodeKey for my case).
Thanks in advance
Upvotes: 1
Views: 79
Reputation: 167506
As long as there is no element having both of these key elements you can simply use group-by="UniqueKey, FirstNodeKey"
as that forms the sequence of the two elements which, in your case, will simply be a sequence with either UniqueKey
or FirstNodeKey
, as the other selection is empty.
Upvotes: 1