xsl-list
[Top] [All Lists]

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

2012-02-12 14:48:48
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>




I've had some success thus far.  The following transform:


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:xs="http://www.w3.org/2001/XMLSchema"; xmlns:fn="http://www.w3.org/2005/xpath-functions"; xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"; exclude-result-prefixes="xd xs fn" version="2.0">
    <xd:doc scope="stylesheet">
        <xd:desc>
            <xd:p><xd:b>Created on:</xd:b> Feb 1, 2 amarendy</xd:p>
            <xd:p><xd:b>Author:</xd:b>Anthony Marendy</xd:p>
        </xd:desc>
    </xd:doc>
    <xsl:template match="//Appointments">
        <MeetingPlan>
<xsl:for-each-group select="//Invitee" group-by="../@AppointmentType"> <xsl:variable name="currentAppointmentType" select="current-grouping-key()"/> <xsl:for-each-group select="current-group()" group-by="../@TimeOfDay">

<xsl:variable name="currentTimeOfDay" select="current-grouping-key()"/> <xsl:variable name="appointmentsWithSameTypeAndTime" select="current-group()"/>


<xsl:for-each-group select="$appointmentsWithSameTypeAndTime" group-by="@Surname"> <xsl:variable name="currentSurname" select="current-grouping-key()"/>

<xsl:for-each-group select="current-group()" group-by="@Firstname"> <xsl:variable name="currentFirstname" select="current-grouping-key()"/>


<!-- Get the list of appointments that the current invitee has. --> <xsl:variable name="inviteeAppointments" select="for $d in current-group()/.. return $d"/> <xsl:if test="(count($inviteeAppointments) > 0)"> <Appointments TimeOfDay="{$currentTimeOfDay}" AppointmentType="{$currentAppointmentType}">
                                    <Dates>
<xsl:for-each select="$inviteeAppointments">
                                            <MeetingDate date="{./@Date}"/>
                                        </xsl:for-each>
                                    </Dates>
                                    <Invitees>


<!-- Now loop through and find all invitees that have the same combination of appointments -->

<xsl:for-each-group select="$appointmentsWithSameTypeAndTime" group-by="@Surname"> <xsl:variable name="loopSurname" select="current-grouping-key()"/> <xsl:for-each-group select="current-group()" group-by="@Firstname"> <xsl:variable name="loopFirstname" select="current-grouping-key()"/> <xsl:variable name="entriesForCurrentInvitee" select="current-group()"/> <xsl:variable name="loopAppointments" select="for $d in $entriesForCurrentInvitee/.. return $d"/>

<xsl:if test="fn:deep-equal($loopAppointments, $inviteeAppointments)"> <Invitee Firstname="{$loopFirstname}" Surname="{$loopSurname}"/>
                                                </xsl:if>

                                            </xsl:for-each-group>
                                        </xsl:for-each-group>
                                    </Invitees>
                                </Appointments>
                            </xsl:if>
                        </xsl:for-each-group>
                    </xsl:for-each-group>
                </xsl:for-each-group>
            </xsl:for-each-group>
        </MeetingPlan>
    </xsl:template>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>




Produces the following output:


<?xml version="1.0" encoding="UTF-8"?>
<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-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>
    <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>


The way the transform works is that it works its way down find a particular appointment type and time, then it determines the list of dates for that appointment. It then loops through all of the appointments of that type and time to find any that have the same set of dates.

You can see that the output the problem is that the output is duplicated. The reason is that that there are multiple appointments in the same set, and each of these will match in the second lookup.

Firstly, I just can't help but feeling I'm going about this the wrong way, so suggestions to other approaches would be appreciated.
Alternately, can anyone suggest how I could remove these duplicates?
I have thought about passing the result through a second stylesheet - which I could live with, but would prefer a different solution.


Thanks,
Anthony.







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