Re: [xsl] Grouping and then more grouping

Subject: Re: [xsl] Grouping and then more grouping
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Mon, 15 Aug 2011 18:08:49 -0500
At 2011-08-15 21:45 +0000, Alex Muir wrote:
I'm trying to group the identical groups by id together such that given
...

First of all, you are violating the principle of xml:id= by having more than one element with the same value for xml:id=. xml:id= attributes should be unique across an XML document. But that won't prevent it from being used.


I'll get a result that merges the like groups together and copies items
...

I'm not sure why you have "Sea" before "Air" when "Air" comes before "Sea" in your input data.


I've written the following which groups the first level and played
around with getting something that moves to lower group levels as I
need however I'm not sure what to do to descend through each group
joining them together by id.

This was interesting because I would say it isn't a typical use of grouping. It can be done and I have an answer below that I hope helps.


Typical sub-grouping is grouping under the same ancestor, yet this is grouping items found under different ancestors. So, whenever I created a population for grouping, I went to all of the child groups of the current group, but I only acted on the first of each current group. The solution fell together very quickly and is quite compact.

I just can't figure out how you expect "Sea" to come before "Air" when "Air" comes before "Sea" in your input. Are you not mentioning an ordering requirement?

. . . . . . . . . . . Ken


t:\ftemp>type alex.xml <?xml version="1.0" encoding="UTF-8"?> <root> <group xml:id="MILHIS_Yes"> <name>Yes</name> <group xml:id="MILHIS_Vehicles"> <name>Vehicles</name> <group xml:id="MILHIS_Air"> <name>Air</name> <group xml:id="MILHIS_Aeroplane"> <name>Aeroplane</name> <item xml:id="MILHIS_Attack_Aircraft"> <name>Attack Aircraft</name> </item> </group> </group> </group> </group> <group xml:id="MILHIS_Yes"> <name>Yes</name> <group xml:id="MILHIS_Vehicles"> <name>Vehicles</name> <group xml:id="MILHIS_Air"> <name>Air</name> <group xml:id="MILHIS_Aeroplane"> <name>Aeroplane</name> <item xml:id="MILHIS_Bomber_Aircraft"> <name>Bomber Aircraft</name> </item> </group> </group> </group> </group> <group xml:id="MILHIS_Yes"> <name>Yes</name> <group xml:id="MILHIS_Vehicles"> <name>Vehicles</name> <group xml:id="MILHIS_Sea"> <name>Sea</name> <group xml:id="MILHIS_Ship"> <name>Ship</name> <item xml:id="MILHIS_Aircraft_Carrier"> <name>Aircraft Carrier</name> </item> </group> </group> </group> </group> </root> t:\ftemp>call xslt2 alex.xml alex.xsl <?xml version="1.0" encoding="UTF-8"?> <group xml:id="MILHIS_Yes"> <name>Yes</name> <group xml:id="MILHIS_Vehicles"> <name>Vehicles</name> <group xml:id="MILHIS_Air"> <name>Air</name> <group xml:id="MILHIS_Aeroplane"> <name>Aeroplane</name> <item xml:id="MILHIS_Attack_Aircraft"> <name>Attack Aircraft</name> </item> <item xml:id="MILHIS_Bomber_Aircraft"> <name>Bomber Aircraft</name> </item> </group> </group> <group xml:id="MILHIS_Sea"> <name>Sea</name> <group xml:id="MILHIS_Ship"> <name>Ship</name> <item xml:id="MILHIS_Aircraft_Carrier"> <name>Aircraft Carrier</name> </item> </group> </group> </group> </group> t:\ftemp>type alex.xsl <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="2.0">

<xsl:output indent="yes"/>
<xsl:template match="root">
  <xsl:call-template name="CopyGroup">
    <xsl:with-param name="groups" select="group"/>
  </xsl:call-template>
</xsl:template>
<xsl:template name="CopyGroup">
  <xsl:param name="groups"/>
  <xsl:for-each-group select="$groups" group-by="@xml:id">
      <group>
        <!--copy the common information about this group-->
        <xsl:copy-of select="@xml:id, name, $groups/item"/>
        <!--process subgroups based on all children of group members-->
        <xsl:call-template name="CopyGroup">
          <xsl:with-param name="groups" select="current-group()/group"/>
        </xsl:call-template>
      </group>
  </xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
t:\ftemp>rem Done!


-- Contact us for world-wide XML consulting & instructor-led training Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Legal business disclaimers: http://www.CraneSoftwrights.com/legal

Current Thread