This question requires a slightly more detailed answer than just pointing to a good Muenchian Grouping source.
The reason is that for the necessary grouping, it is necessary to identify the names of all the child elements of the element "ele [SomeString]" and their parent . Such grouping requires a key definition that is uniquely determined by both unique sources, usually through concatenation.
This conversion is :
<xsl: stylesheet version = "1.0"
xmlns: xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl: output omit-xml-declaration = "yes" indent = "yes" />
<xsl: key name = "kElByName" match = "*"
use = "concat (generate-id (..), '+', name ())" />
<xsl: template match = "node () | @ *">
<xsl: copy>
<xsl: apply-templates select = "node () | @ *" />
</ xsl: copy>
</ xsl: template>
<xsl: template match = "* [starts-with (name (), 'ele')]">
<xsl: copy>
<xsl: copy-of select = "@ *" />
<xsl: apply-templates select =
"* [generate-id ()
=
generate-id (key ('kElByName',
concat (generate-id (..), '+', name ())
)[1])
] "
/>
</ xsl: copy>
</ xsl: template>
</ xsl: stylesheet>
when applied to this XML document :
<Root>
<ele1>
<child1> context1 </child1>
<child2> test1 </child2>
<child1> context1 </child1>
</ele1>
<ele2>
<child1> context2 </child1>
<child2> test2 </child2>
<child1> context2 </child1>
</ele2>
<ele3>
<child2> context2 </child2>
<child2> test2 </child2>
<child1> context1 </child1>
</ele3>
</Root>
creates the desired result :
<Root>
<ele1>
<child1> context1 </child1>
<child2> test1 </child2>
</ele1>
<ele2>
<child1> context2 </child1>
<child2> test2 </child2>
</ele2>
<ele3>
<child2> context2 </child2>
<child1> context1 </child1>
</ele3>
</Root>
source share