xsl-list
[Top] [All Lists]

Re: [xsl] Nested Muenchian Grouping

2006-10-05 08:33:39
Please try this stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:output method="xml" indent="yes" />

<xsl:key name="x" match="player" use="@state"/>
<xsl:key name="y" match="player" use="concat(@state, ':', @team)"/>
<xsl:key name="z" match="player" use="concat(@state, ':', @team, ':',
@position)"/>

<xsl:template match="/test">
 <group>
   <xsl:for-each select="group/player[generate-id() =
generate-id(key('x', @state)[1])]">
     <xsl:variable name="state" select="@state" />
     <state name="{(_at_)state}">
        <xsl:for-each select="../player[generate-id() =
generate-id(key('y', concat($state, ':', @team))[1])]">
          <team name="{(_at_)team}">
            <xsl:variable name="team" select="@team" />
            <xsl:for-each select="../player[generate-id() =
generate-id(key('z', concat($state, ':', $team, ':',
@position))[1])]">
               <position name="{(_at_)position}">
                  <xsl:for-each select="key('z', concat($state, ':',
$team, ':', @position))">
                    <player name="{(_at_)name}" number="{.}"/>
                  </xsl:for-each>
               </position>
            </xsl:for-each>
          </team>
        </xsl:for-each>
     </state>
   </xsl:for-each>
 </group>
</xsl:template>

</xsl:stylesheet>

This when applied to the given XML:

<test>
 <group>
   <player name="joe" position="pitcher" team="mets" state="ny">2</player>
   <player name="mark" position="outfielder" team="mets" state="ny">11</player>
   <player name="john" position="pitcher" team="mets" state="ny">23</player>
   <player name="pete" position="outfielder" team="mets" state="ny">27</player>
   <player name="roy" position="outfielder" team="mets" state="ny">13</player>
   <player name="carl" position="infielder" team="mets" state="ny">32</player>
 </group>
</test>

Produces the desired output:

<?xml version="1.0" encoding="UTF-8"?>
<group>
  <state name="ny">
     <team name="mets">
        <position name="pitcher">
           <player name="joe" number="2"/>
           <player name="john" number="23"/>
        </position>
        <position name="outfielder">
           <player name="mark" number="11"/>
           <player name="pete" number="27"/>
           <player name="roy" number="13"/>
        </position>
        <position name="infielder">
           <player name="carl" number="32"/>
        </position>
     </team>
  </state>
</group>


On 10/5/06, Minervini, Chris 
<christopher(_dot_)minervini(_at_)lehman(_dot_)com> wrote:

I need to turn this:

<test>
       <group>
               <player name="joe" position="pitcher" team="mets"
state="ny">2</player>
               <player name="mark" position="outfielder" team="mets"
state="ny">11</player>
               <player name="john" position="pitcher" team="mets"
state="ny">23</player>
               <player name="pete" position="outfielder" team="mets"
state="ny">27</player>
               <player name="roy" position="outfielder" team="mets"
state="ny">13</player>
               <player name="carl" position="infielder" team="mets"
state="ny">32</player>
       </group>
</test>

Into something like this:

<group>
       <state name="ny">
               <team name="mets">
                       <position name="pitcher">
                               <player name="joe" number="2"/>
                               <player name="john" number="23"/>
                       </position>
                       <position name="outfielder">
                               <player name="mark" number="11"/>
                               <player name="pete" number="27"/>
                               <player name="roy" number="13"/>
                       </position>
                       <position name="infielder">
                               <player name="carl" number="32"/>
                       </position>
               </team>
       </state>
</group>

Assuming there can be many states, many teams and many positions

I tried to nest the Muenchian method like this:

<xsl:key name="state-grouping" match="player" use="@state"/>
<xsl:key name="team-grouping" match="player" use="@team"/>
<xsl:key name="position-grouping" match="player" use="@position"/>

<xsl:template match="test/group">
       <group>
               <xsl:for-each select="player[count(. |
key('state-grouping', @player_ownership_details)[1]) = 1]">
                       <xsl:variable name="state" select="@state"/>
                       <xsl:variable name="state_node_set"
select="../player[(_at_)state=$state]"/>
                       <state name="{$state}">
                               <xsl:for-each
select="$state_node_set[(count(. | key('team-grouping', @team)[1]) =
1)]">
                                       <xsl:variable name="team"
select="@team"/>
                                       <xsl:variable
name="team_node_set" select="$state_node_set[(_at_)team = $team]"/>
                                       <team name="{$team}">
                                               <xsl:for-each
select="$team_node_set[(count(. | key('position-grouping',
@position)[1]) = 1)]">
                                                       <xsl:variable
name="position" select="@position"/>
                                                       <xsl:variable
name="position_node_set" select="$team_node_set[(_at_)position =
$position]"/>
                                                       <position
name="{$position}">

<xsl:for-each select="$position_node_set">

<player name="{(_at_)name}" number="{.}" />

</xsl:for-each>
                                                       </position>
                                               </xsl:for-each>
                                       </team>
                               </xsl:for-each>
                       </state>
               </xsl:for-each>
       </group>
</xsl:template>

But it doesn't produce the correct output when I add more data (more
teams, position, etc.)

Am I using the muenchian method correctly?  Is this something that can
be done?  I've been working on a solution all day (with more complicated
data) and I can't seem to get it to work.  Anyone out there ever pull
off something like this?


------------------------------------------------------------------------------
This message is intended only for the personal and confidential use of the 
designated recipient(s) named above.  If you are not the intended recipient of 
this message you are hereby notified that any review, dissemination, 
distribution or copying of this message is strictly prohibited.  This 
communication is for information purposes only and should not be regarded as an 
offer to sell or as a solicitation of an offer to buy any financial product, an 
official confirmation of any transaction, or as an official statement of Lehman 
Brothers.  Email transmission cannot be guaranteed to be secure or error-free.  
Therefore, we do not represent that this information is complete or accurate 
and it should not be relied upon as such.  All information is subject to change 
without notice.

--------
IRS Circular 230 Disclosure:
Please be advised that any discussion of U.S. tax matters contained within this 
communication (including any attachments) is not intended or written to be used 
and cannot be used for the purpose of (i) avoiding U.S. tax related penalties 
or (ii) promoting, marketing or recommending to another party any transaction 
or matter addressed herein.


--
Regards,
Mukul Gandhi

--~------------------------------------------------------------------
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(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>