Altova Mailing List Archives


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

From: Simon Pepping <sampepping@-----.--->
To: xsl-list@-----.------------.---
Date: 2/13/2012 1:03:00 PM
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@s...> 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>

--~------------------------------------------------------------------
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.