Re: [xsl] A challenge.. Group Periods of Data (1..5, 2..8, 4..9) (10..12; 10..14)

Subject: Re: [xsl] A challenge.. Group Periods of Data (1..5, 2..8, 4..9) (10..12; 10..14)
From: Karl Stubsjoen <kstubs@xxxxxxxxx>
Date: Tue, 3 May 2005 14:06:05 -0700
Nice Wendell!

The challend did state that the periods listed were arbitrary
(unkown).  Any way to drive your solution without parameters or
pre-existing variables defining these periods?

On 5/3/05, Wendell Piez <wapiez@xxxxxxxxxxxxxxxx> wrote:
> Hi Karl,
>
> At 04:16 PM 5/3/2005, you wrote:
> >A challenge, group the following XML into 2 periods.
>
> Okay, I'll bite.
>
> (Anyone else answering this challenge, read no further till you're done!)
>
> I'm assuming your target periods are known at time of writing. (If only
> known at run-time, they can be parameterized. If not even that, more
> specification is called for.) I'm also assuming your data is known to be
> valid to the assumptions made for sorting (for example, there's no B whose
> beginning is in one period and end is in another) -- there's no
> exception-handling for any of that.
>
> Here goes:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0"
>   xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>   xmlns:local="data:hey-david"
>   exclude-result-prefixes="local">
>
> <xsl:output indent="yes"/>
>
> <xsl:variable name="period-groups" xmlns="data:hey-david">
>   <group start="1" end="12"/>
>   <group start="14" end="30"/>
> </xsl:variable>
>
> <xsl:variable name="periods" select="//*[@period_begin]"/>
>
> <xsl:template match="/">
>   <result>
>     <xsl:apply-templates select="document('')/*/*/local:group"/>
>   </result>
> </xsl:template>
>
> <xsl:template match="local:group">
>   <xsl:variable name="start" select="@start"/>
>   <xsl:variable name="end" select="@end"/>
>   <period begins="{@start}" ends="{@end}">
>     <xsl:for-each
>       select="$periods[@period_begin &gt;= $start and @period_end &lt;=
> $end]">
>       <xsl:sort select="@period_begin"/>
>       <xsl:sort select="@period_end"/>
>       <xsl:copy-of select="."/>
>     </xsl:for-each>
>   </period>
> </xsl:template>
>
> </xsl:stylesheet>
>
> Run on the input you provided, this gets you the output you say you want.
>
> Cheers,
> Wendell
>
> >   The periods are
> >arbitrary, but for this example they happen to be:
> >Period 1:  1 - 12
> >Period 2:  14 - 30
> >
> >Expected Result:
> ><result>
> >   <period begins="1" ends="12">
> >     <B period_begin="1" period_end="5"/>
> >     <B period_begin="2" period_end="7"/>
> >     <B period_begin="3" period_end="10"/>
> >     <B period_begin="4" period_end="12"/>
> >   </period>
> >   <period begins="14" ends="30">
> >     <B period_begin="14" period_end="16"/>
> >     <B period_begin="16" period_end="20"/>
> >     <B period_begin="16" period_end="30"/>
> >   </period>
> ></result>
> >
> >Source XML / Result (sorted)
> ><A>
> >   <B period_begin="1" period_end="5"/>
> >   <B period_begin="2" period_end="7"/>
> >   <B period_begin="3" period_end="10"/>
> >   <B period_begin="4" period_end="12"/>
> >   <B period_begin="14" period_end="16"/>
> >   <B period_begin="16" period_end="20"/>
> >   <B period_begin="16" period_end="30"/>
> ></A>
> >
> >Source XML / Result (un-sorted)
> ><A>
> >   <B period_begin="14" period_end="16"/>
> >   <B period_begin="2" period_end="7"/>
> >   <B period_begin="16" period_end="20"/>
> >   <B period_begin="1" period_end="5"/>
> >   <B period_begin="4" period_end="12"/>
> >   <B period_begin="16" period_end="30"/>
> >   <B period_begin="3" period_end="10"/>
> ></A>
>
> ======================================================================
> Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
> Mulberry Technologies, Inc.                http://www.mulberrytech.com
> 17 West Jefferson Street                    Direct Phone: 301/315-9635
> Suite 207                                          Phone: 301/315-9631
> Rockville, MD  20850                                 Fax: 301/315-8285
> ----------------------------------------------------------------------
>   Mulberry Technologies: A Consultancy Specializing in SGML and XML
> ======================================================================

Current Thread