[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Grouping by distinct combinations of descendant elements in xsl 2.0 and xpath 2.0


Subject: Re: [xsl] Grouping by distinct combinations of descendant elements in xsl 2.0 and xpath 2.0
From: Simon Pepping <sampepping@xxxxxxxxx>
Date: Mon, 13 Feb 2012 14:02:57 +0100

Hi,

This does the grouping that you want:

  <xsl:template match="/">
    <xsl:variable name="appointments" as="element(Appointment)+">
      <xsl:for-each select="//Invitee">
        <Appointment Date="{../@Date}" TimeOfDay="{../@TimeOfDay}"
AppointmentType="{../@AppointmentType}"
          Firstname="{@Firstname}" Surname="{@Surname}"
Name="{concat(@Firstname,@Surname)}"/>
      </xsl:for-each>
    </xsl:variable>
    <xsl:variable name="ap-groups" as="element(ap-group)+">
      <xsl:for-each-group select="$appointments"
group-by="concat(@TimeOfDay,'-',@AppointmentType,'-',@Name)">
        <ap-group key="{current-grouping-key()}">
          <xsl:for-each select="current-group()">
            <xsl:copy-of select="."/>
          </xsl:for-each>
        </ap-group>
      </xsl:for-each-group>
    </xsl:variable>
    <xsl:variable name="ap-groups-by-set">
      <xsl:for-each-group select="$ap-groups"
group-by="xx:get-dates(Appointment/@Date,'')">
        <ap-group-by-set key="{current-grouping-key()}">
          <xsl:for-each select="current-group()">
            <xsl:copy-of select="."/>
          </xsl:for-each>
        </ap-group-by-set>
      </xsl:for-each-group>
    </xsl:variable>
    <Appointments>
      <xsl:for-each select="$ap-groups-by-set">
        <xsl:copy-of select="."/>
      </xsl:for-each>
    </Appointments>
  </xsl:template>

  <xsl:function name="xx:get-dates">
    <xsl:param name="Dates"/>
    <xsl:param name="dates"/>
    <xsl:choose>
      <xsl:when test="$Dates">
        <xsl:variable name="dates-c">
          <xsl:if test="$dates">
            <xsl:text>:</xsl:text>
          </xsl:if>
        </xsl:variable>
        <xsl:value-of
select="xx:get-dates($Dates[position()!=1],concat($dates,$dates-c,$Dates[1]))
"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$dates"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:function>

On Sun, Feb 12, 2012 at 21:48, Anthony Marendy <amarendy@xxxxxxxxx> wrote:
>
> Hi.
>
> I am currently trying to transform the xml as shown below.  It requires me
to essentially find the distinct list of all people who have the same set of
meeting dates and times, and then group them by those that have the same dates
and times together.
>
> i.e. a single invitee is never split over multiple appointment groups - if
they have all days the same and one extra, then they become a new group.
>
>
>
> <Appointments>
>    <Appointment Date="2011-01-01" TimeOfDay="06:00:00"
AppointmentType="Meeting">
>        <Invitee Firstname="Martha" Surname="Jones"/>
>        <Invitee Firstname="Louis" Surname="Jones"/>
>    </Appointment>
>    <Appointment Date="2011-01-02" TimeOfDay="06:00:00"
AppointmentType="Meeting">
>        <Invitee Firstname="Martha" Surname="Jones"/>
>        <Invitee Firstname="Louis" Surname="Jones"/>
>        <Invitee Firstname="Gordon" Surname="Jones"/>
>    </Appointment>
>    <Appointment Date="2011-01-03" TimeOfDay="06:00:00"
AppointmentType="Meeting">
>        <Invitee Firstname="Martha" Surname="Jones"/>
>        <Invitee Firstname="Louis" Surname="Jones"/>
>        <Invitee Firstname="Gordon" Surname="Jones"/>
>    </Appointment>
>    <Appointment Date="2011-01-02" TimeOfDay="06:00:00"
AppointmentType="PhoneHookup">
>        <Invitee Firstname="Martha" Surname="Jones"/>
>        <Invitee Firstname="Louis" Surname="Jones"/>
>     </Appointment>
> </Appointments>
>
>
> Into:
>
>
> <MeetingPlan>
>    <Appointments TimeOfDay="06:00:00" AppointmentType="Meeting">
>        <Dates>
>            <MeetingDate date="2011-01-01"/>
>            <MeetingDate date="2011-01-02"/>
>            <MeetingDate date="2011-01-03"/>
>        </Dates>
>        <Invitees>
>            <Invitee Firstname="Martha" Surname="Jones"/>
>            <Invitee Firstname="Louis" Surname="Jones"/>
>        </Invitees>
>    </Appointments>
>    <Appointments TimeOfDay="06:00:00" AppointmentType="Meeting">
>        <Dates>
>            <MeetingDate date="2011-01-02"/>
>            <MeetingDate date="2011-01-03"/>
>        </Dates>
>        <Invitees>
>            <Invitee Firstname="Gordon" Surname="Jones"/>
>        </Invitees>
>    </Appointments>
>    <Appointments TimeOfDay="06:00:00" AppointmentType="PhoneHookup">
>        <Dates>
>            <MeetingDate date="2011-01-02"/>
>        </Dates>
>        <Invitees>
>            <Invitee Firstname="Martha" Surname="Jones"/>
>            <Invitee Firstname="Louis" Surname="Jones"/>
>        </Invitees>
>    </Appointments>
> </MeetingPlan>


Current Thread
Keywords
xml