RE: [xsl] Processing and sorting non-military time

Subject: RE: [xsl] Processing and sorting non-military time
From: christoph.naber@xxxxxxxxxxxxxxxxxxx
Date: Fri, 17 Aug 2007 14:08:53 +0200
Hello,

I have no stylesheet that you can use right away, but I think I have
something that might help you further.

Maybe you want to think about seperating the events, so that you can call
for-each on each case:

Case 1: the events at 12.xx AM. They have to appear first in the list.
Case 2: the events which are not on the twelfth hour, ignoring whether its
PM or AM.
Case 3: the events on 12.xx PM.

This is (imho) possible by creating some new keys.

When you call a for-each for each of these cases, I think it should be
possible to achieve the right order.

BTW: Better use "apply-templates" instead of for-each. I'm quite new with
XSLT so I cannot speak from my own experience, but the professionals in
here mention it everytime someone asks about for-each and you read it in
every FAQ.

I wrote a stylesheet, but I'm not able to make the keys work correctly,
you have to check for yourself whats wrong.
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
        <xsl:output method="xml" indent="yes"/>

        <!-- should select all events for a given date -->
        <xsl:key name="events-by-date" match="event"
use="substring(occurrences/occurrence/start,1,10)" />

        <!-- should only select the events where
substring(occurrences/occurence/start, 12, 2) != '12' -->
        <xsl:key name="events-by-date-main"
match="event[not(contains(substring(occurrences/occurence/start, 12, 2),
'12'))]" use="substring(occurrences/occurrence/start,1,10)" />

        <!-- should only select the events where
substring(occurrences/occurence/start, 12, 2) == '12' and
substring(occurrences/occurence/start, 18, 2) == 'AM'-->
        <xsl:key name="events-by-date-12am"
match="event[substring(occurrences/occurence/start, 18, 2) = 'AM' and
substring(occurrences/occurence/start, 12, 2) = '12']"
use="substring(occurrences/occurrence/start,1,10)" />

        <!-- should only select the events where
substring(occurrences/occurence/start, 12, 2) == '12' and
substring(occurrences/occurence/start, 18, 2) == 'PM'-->
        <xsl:key name="events-by-date-12pm"
match="event[substring(occurrences/occurence/start, 18, 2) = 'PM' and
substring(occurrences/occurence/start, 12, 2) = '12']"
use="substring(occurrences/occurrence/start,1,10)" />

        <xsl:template match="events">
                <root>
                        <xsl:apply-templates select="event[count(. |
key('events-by-date',   substring(occurrences/occurrence/start,1,10))[1])
= 1]">
                                <xsl:sort
select="substring(occurrences/occurrence/start,1,10)" />
                        </xsl:apply-templates>
                </root>
        </xsl:template>

        <!-- this template should process all events from the date of the
actual event -->
        <xsl:template match="event" >
                <comment>selected by 12pm</comment>
                <xsl:apply-templates select="key('events-by-date-12pm',
substring(occurrences/occurrence/start,1,10))" mode="sorted">
                        <xsl:sort order="ascending"
select="substring(occurrences/occurrence/start,15,2)" />
                </xsl:apply-templates>
                <comment>selected by main</comment>
                <xsl:apply-templates select="key('events-by-date-main',
substring(occurrences/occurrence/start,1,10))" mode="sorted">
                        <xsl:sort order="ascending"
select="substring(occurrences/occurrence/start,18,2)" />
                        <xsl:sort order="ascending"
select="substring(occurrences/occurrence/start,12,2)" />
                        <xsl:sort order="ascending"
select="substring(occurrences/occurrence/start,15,2)" />
                </xsl:apply-templates>
                <comment>selected by 12am</comment>
                <xsl:apply-templates select="key('events-by-date-12am',
substring(occurrences/occurrence/start,1,10))" mode="sorted">
                        <xsl:sort order="ascending"
select="substring(occurrences/occurrence/start,15,2)" />
                </xsl:apply-templates>
        </xsl:template>

        <xsl:template match="event" mode="sorted">
                <!-- ... -->
        </xsl:template>

</xsl:stylesheet>


Greetings
Christoph



If you are not the intended addressee, please inform us immediately that you
have received this e-mail by mistake and delete it. We thank you for your
support.

Current Thread