This can be solved with the following XSLT 1.0 transformation:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<!-- this key selects elements by their "Superset" name -->
<xsl:key name="kElementBySuperset" match="Element" use="
substring-before(@name, '_')"
/>
<!-- this key selects elements by their "Superset_Set" name -->
<xsl:key name="kElementBySet" match="Element" use="
concat(
substring-before(@name, '_'),
'_',
substring-before(substring-after(@name, '_'), '_')
)
" />
<!--- initalize output (note the template modes) -->
<xsl:template match="Set">
<xsl:apply-templates select="Element" mode="Superset">
<xsl:sort select="@name" />
</xsl:apply-templates>
</xsl:template>
<!-- output <Superset> elements, grouped by name -->
<xsl:template match="Element" mode="Superset">
<xsl:variable name="vSupersetName" select="
substring-before(@name, '_')
" />
<xsl:if test="
generate-id()
=
generate-id(key('kElementBySuperset', $vSupersetName)[1])
">
<Superset name="{$vSupersetName}">
<xsl:apply-templates
select="key('kElementBySuperset', $vSupersetName)"
mode="Set"
>
<xsl:sort select="@name" />
</xsl:apply-templates>
</Superset>
</xsl:if>
</xsl:template>
<!-- output <Set> elements, grouped by name -->
<xsl:template match="Element" mode="Set">
<xsl:variable name="vSetName" select="
concat(
substring-before(@name, '_'),
'_',
substring-before(substring-after(@name, '_'), '_')
)"
/>
<xsl:if test="
generate-id()
=
generate-id(key('kElementBySet', $vSetName)[1])
">
<Set name="{substring-after($vSetName, '_')}">
<xsl:apply-templates
select="key('kElementBySet', $vSetName)"
mode="Element"
>
<xsl:sort select="@name" />
</xsl:apply-templates>
</Set>
</xsl:if>
</xsl:template>
<!-- output <Element> elements -->
<xsl:template match="Element" mode="Element">
<xsl:variable name="vElementName" select="
substring-after(
substring-after(@name, '_'),
'_'
)
" />
<Element name="{$vElementName}" />
</xsl:template>
</xsl:stylesheet>
Output on my system when applied to your input document:
<Superset name="Superset1">
<Set name="Set1">
<Element name="Element1" />
<Element name="Element2" />
</Set>
<Set name="Set2">
<Element name="Element1" />
</Set>
</Superset>
<Superset name="Superset2">
<Set name="Set1">
<Element name="Element1" />
</Set>
<Set name="Set2">
<Element name="Element1" />
</Set>
</Superset>
It is worth noting that this solution is case-sensitive. I assume that is desirable (or at least not harmful) in your case. If case-insensitivity is required, then sprinkling a handful of these would become necessary (where "…" must of course be replaced by the missing letters):
translate($anyvalue, 'ABC…XYZ', 'abc…xyz')
I avoided that because it is very repetitive and makes the solution (even more) obscure.
Further reading: One of my solutions that does a similar two-step grouping using two <xsl:key>
s is here:
XSLT 3-level grouping on attributes
It is a bit more verbose on the internals, and it contains a lengthy explanation of <xsl:key>
that I'd like to avoid repeating here. ;-)