Altova Mailing List Archives


Re: [xsl] Grouping and then more grouping

From: Alex Muir <alex.g.muir@-----.--->
To: xsl-list@-----.------------.---
Date: 8/16/2011 12:09:00 AM
> 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?

Sorry my bad on the example of the output which your correct is in the
wrong order.

I've tried running it but it doesn't quite work perhaps because you
didn't have the same volume of input data that I had to test with,
sorry if that is the case.

Meanwhile I've found an ugly solution but got the job done before the
deadline in the morning so good for now but I'd still like to know a
better method if one exists.

Essentially this code solves the problem by adding an extra /group to
this template and rerunning from the last output.  <xsl:template
match="root/group/group/group/group/group/group/group/group">

 <xsl:template match="root">
    <root>

      <xsl:apply-templates/>

    </root>
  </xsl:template>

  <xsl:template match="group">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="root/group/group/group/group/group/group/group/group">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:call-template name="CopyGroup"/>
      <xsl:apply-templates select="item"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template name="CopyGroup">
    <xsl:for-each-group select="group" group-by="@xml:id">
      <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:copy-of select="current-group()/*"/>
      </xsl:copy>
    </xsl:for-each-group>
  </xsl:template>

  <xsl:template match="item">
    <xsl:copy-of select="."/>
  </xsl:template>

I changed the input a bit to make it perhaps easier moving name as an
attribute. I'll paste a sample of some more example input.

   <group xml:id="MILHIS_Yes" name="Yes">
      <group xml:id="MILHIS_Vehicles" name="Vehicles">
         <group xml:id="MILHIS_Air" name="Air">
            <group xml:id="MILHIS_Aeroplane" name="Aeroplane">
               <item xml:id="MILHIS_Attack_Aircraft" name="Attack Aircraft"/>
            </group>
         </group>
      </group>
   </group>
   <group xml:id="MILHIS_Yes" name="Yes">
      <group xml:id="MILHIS_Vehicles" name="Vehicles">
         <group xml:id="MILHIS_Air" name="Air">
            <group xml:id="MILHIS_Aeroplane" name="Aeroplane">
               <item xml:id="MILHIS_Bomber_Aircraft" name="Bomber Aircraft"/>
            </group>
         </group>
      </group>
   </group>
   <group xml:id="MILHIS_Yes" name="Yes">
      <group xml:id="MILHIS_Vehicles" name="Vehicles">
         <group xml:id="MILHIS_Air" name="Air">
            <group xml:id="MILHIS_Aeroplane" name="Aeroplane">
               <item xml:id="MILHIS_Jet_Aircraft" name="Jet Aircraft"/>
            </group>
         </group>
      </group>
   </group>
   <group xml:id="MILHIS_Yes" name="Yes">
      <group xml:id="MILHIS_Vehicles" name="Vehicles">
         <group xml:id="MILHIS_Air" name="Air">
            <group xml:id="MILHIS_Aeroplane" name="Aeroplane">
               <item xml:id="MILHIS_Rocket_Aircraft" name="Rocket Aircraft"/>
            </group>
         </group>
      </group>
   </group>
   <group xml:id="MILHIS_Yes" name="Yes">
      <group xml:id="MILHIS_Vehicles" name="Vehicles">
         <group xml:id="MILHIS_Air" name="Air">
            <group xml:id="MILHIS_Aeroplane" name="Aeroplane">
               <item xml:id="MILHIS_TransportCarrier_Aircraft"
name="Transport/Carrier Aircraft"/>
            </group>
         </group>
      </group>
   </group>
   <group xml:id="MILHIS_Yes" name="Yes">
      <group xml:id="MILHIS_Vehicles" name="Vehicles">
         <group xml:id="MILHIS_Air" name="Air">
            <group xml:id="MILHIS_Aeroplane" name="Aeroplane">
               <item xml:id="MILHIS_Unmanned_Drone" name="Unmanned Drone"/>
            </group>
         </group>
      </group>
   </group>
 <group xml:id="MILHIS_Yes" name="Yes">
      <group xml:id="MILHIS_Vehicles" name="Vehicles">
         <group xml:id="MILHIS_Air" name="Air">
            <group xml:id="MILHIS_Aeroplane" name="Aeroplane">
               <group xml:id="MILHIS_Fighter_Aircraft" name="Fighter Aircraft">
                  <item xml:id="MILHIS_Stealth_Fighter" name="Stealth Fighter"/>
               </group>
            </group>
         </group>
      </group>
   </group>
   <group xml:id="MILHIS_Yes" name="Yes">
      <group xml:id="MILHIS_Weapons" name="Weapons">
         <group xml:id="MILHIS_Small_Arms__Personal_Weapons"
                name="Small Arms &amp; Personal Weapons">
            <group xml:id="MILHIS_AntiPersonnel_Weapons"
name="Anti-Personnel Weapons">
               <group xml:id="MILHIS_Fully_Automatic_Weapon"
name="Fully Automatic Weapon">
                  <item xml:id="MILHIS_Multiple_Barrel_Firearm"
name="Multiple Barrel Firearm"/>
               </group>
            </group>
         </group>
      </group>
   </group>
   <group xml:id="MILHIS_Yes" name="Yes">
      <group xml:id="MILHIS_Weapons" name="Weapons">
         <group xml:id="MILHIS_Small_Arms__Personal_Weapons"
                name="Small Arms &amp; Personal Weapons">
            <group xml:id="MILHIS_AntiPersonnel_Weapons"
name="Anti-Personnel Weapons">
               <group xml:id="MILHIS_Fully_Automatic_Weapon"
name="Fully Automatic Weapon">
                  <item xml:id="MILHIS_Submachine_Gun" name="Submachine Gun"/>
               </group>
            </group>
         </group>
      </group>
   </group>
 <group xml:id="MILHIS_No" name="No">
      <group xml:id="MILHIS_Troops" name="Troops">
         <group xml:id="MILHIS_Infantry" name="Infantry">
            <item xml:id="MILHIS_Maori_Warrior" name="Maori Warrior"/>
         </group>
      </group>
   </group>
   <group xml:id="MILHIS_No" name="No">
      <group xml:id="MILHIS_Troops" name="Troops">
         <group xml:id="MILHIS_Infantry" name="Infantry">
            <item xml:id="MILHIS_Medieval_Footsoldier" name="Medieval
Footsoldier"/>
         </group>
      </group>
   </group>

Example output from all the input data.. again a sample only partially
reflecting the sample content above but what I'm getting for a final
output in general. I hope I'm being clear but it's late..

   <group xml:id="MILHIS_Yes" name="Yes">
      <group xml:id="MILHIS_Vehicles" name="Vehicles">
         <group xml:id="MILHIS_Air" name="Air">
            <group xml:id="MILHIS_Aeroplane" name="Aeroplane">
               <group xml:id="MILHIS_Fighter_Aircraft" name="Fighter Aircraft">
                  <item xml:id="MILHIS_Stealth_Fighter" name="Stealth Fighter"/>
               </group>
               <item xml:id="MILHIS_Attack_Aircraft" name="Attack Aircraft"/>
               <item xml:id="MILHIS_Bomber_Aircraft" name="Bomber Aircraft"/>
               <item xml:id="MILHIS_Jet_Aircraft" name="Jet Aircraft"/>


The output the I've got from running your script starts like this.
Jeep shouldn't be under Air Vehicles so items are being misplaced.

<group xml:id="MILHIS_Yes">
   <group xml:id="MILHIS_Vehicles">
      <group xml:id="MILHIS_Air">
         <item xml:id="MILHIS_Armoured_Vehicle" name="Armoured Vehicle"/>
         <item xml:id="MILHIS_Chariot" name="Chariot"/>
         <item xml:id="MILHIS_Jeep" name="Jeep"/>



Thanks
Alex

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe@l...>
--~--

Disclaimer

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.