Reputation: 967
I know nothing about XSLT and I need a quick headsup with this matter:
I have an xml structure like this:
<node>
<sub_node_1> value_1 </sub_node_1>
...
<sub_node_n> value_n </sub_node_n>
<repeatable_node> another_value_1 </repeatable_node>
</node>
...
<node>
<sub_node_1> value_1 </sub_node_1>
...
<sub_node_n> value_n </sub_node_n>
<repeatable_node> another_value_m </repeatable_node>
</node>
and I want something that aggregates the repeatable nodes with different values under the same element, i.e. something like this
<node>
<sub_node_1> value_1 </sub_node_1>
...
<sub_node_n> value_n </sub_node_n>
<repeatable_node> another_value_1 </repeatable_node>
...
<repeatable_node> another_value_m </repeatable_node>
</node>
Upvotes: 1
Views: 2187
Reputation: 2585
Here's a simple XSLT 1.0 -compatible solution:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- Identity transform -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- Keep only the first <node> element -->
<xsl:template match="node[1]">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
<!-- Apply all repeatable_node children of following <node> siblings. -->
<xsl:apply-templates select="following-sibling::node/repeatable_node"/>
</xsl:copy>
</xsl:template>
<!-- Drop all <node> elements following the first <node> element -->
<xsl:template match="node[position() > 1]"/>
</xsl:stylesheet>
<nodes>
<node>
<sub_node_1> value_1 </sub_node_1>
<sub_node_n> value_n </sub_node_n>
<repeatable_node> another_value_1 </repeatable_node>
</node>
<node>
<sub_node_1> value_1 </sub_node_1>
<sub_node_n> value_n </sub_node_n>
<repeatable_node> another_value_m </repeatable_node>
</node>
</nodes>
<?xml version="1.0"?>
<nodes>
<node>
<sub_node_1> value_1 </sub_node_1>
<sub_node_n> value_n </sub_node_n>
<repeatable_node> another_value_1 </repeatable_node>
<repeatable_node> another_value_m </repeatable_node>
</node>
</nodes>
Upvotes: 1