Slightly different to the standard Meunchain grouping as I'm dealing with a null(?) attribute for some of the tags that I'm transforming. I'd like the null groupings to be treated as their own individual group with the transformed output adding the grouped strings. Also if there is a grouping to do a count of how many there were.
<root>
<section>
<subsection>
<module>
<comp>111</comp>
</module>
<module group='group01'>
<comp>222</comp>
</module>
<module group='group01'>
<comp>333</comp>
</module>
<module>
<comp>444</comp>
</module>
<module>
<comp>555</comp>
</module>
</subsection>
</section>
<section>
<subsection>
<module group ="group02">
<comp>666</comp>
</module>
<module group ="group02">
<comp>777</comp>
</module>
<module>
<comp>888</comp>
</module>
<module group ="group03">
<comp>999</comp>
</module>
<module group ="group03">
<comp>101010</comp>
</module>
</subsection>
<subsection>
<module group ="group04">
<comp>11111</comp>
</module>
<module group ="group04">
<comp>121212</comp>
</module>
<module group ="group05">
<comp>131313</comp>
</module>
<module group ="group05">
<comp>141414</comp>
</module>
<module group ="group06">
<comp>151515</comp>
</module>
<module group ="group06">
<comp>161616</comp>
</module>
<module>
<comp>171717</comp>
</module>
</subsection>
</section>
Wanted output:
<AllSections>
<section>
<subsection>
<page>
<content>111</content>
</page>
<page>
<content>222333</content>
<count>2</count>
</page>
<page>
<content>444</content>
</page>
<page>
<content>555</content>
</page>
</subsection>
</section>
<section>
<subsection>
<page>
<content>666777</content>
<count>2</count>
</page>
<page>
<content>888</content>
</page>
<page>
<content>999101010</content>
<count>2</count>
</page>
</subsection>
<subsection>
<page>
<content>111111121212</content>
<count>2</count>
</page>
<page>
<content>131313141414161616</content>
<count>3</count>
</page>
<page>
<content>151515</content>
</page>
<page>
<content>171717</content>
</page>
</subsection>
</section>
Thanks!
For the elements with a group attribute, you are grouping by that attribute but also within the parent subsection element. Therefore you could start off by defining a key to group them this was
Next, you would need templates to match the various cases for the module elements. Firstly, you could have a template to match module elements with no group attribute, where you could format the output as required.
For modules, with group attributes you would need a match that checked that this particular module element occurred first in the group for the key defined above.
Within this template, you could then easily define a variable to hold the elements in the group, using the key, and then either output the child comp elements, or count them
Finally, you would need a third template to match all other module elements (i.e. elements with a group attribute, but not first in the group) to ignore them, to ensure they don't get output twice. (The XSLT processor should always match more specific templates before this more generic one)
Here is the full XSLT
When applied to your sample XML, the following is output