[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: A challenge.. Group Periods of Data (1..5, 2..8,

Subject: Re: A challenge.. Group Periods of Data (1..5, 2..8, 4..9) (10..12; 10..14)
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Wed, 4 May 2005 21:37:41 +1000
xslt1 max xslt2
Hi Karl,

Still an XSLT 2.0 solution, but this time using f:foldl() as promised.
This can be re-written 1:1 in XSLT 1.0 + FXSL for XSLT 1.0.

The reason I'm posting this second solution is because it resembles
very much the "functional tokenizer"  (see for example:
   http://www.biglist.com/lists/xsl-list/archives/200111/msg00905.html)

and your problem can be called something like:

                 "interval tokenization".



I still cannot fully assimilate the meaning and implications of this
striking similarity but it seems to prove that there's law, order and
elegance in the world of functional programming.

Here's the transformation:

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:foldl-func="foldl-func"
xmlns:f="http://fxsl.sf.net/"
exclude-result-prefixes="f foldl-func"
>

   <xsl:import href="../f/func-foldl.xsl"/>

   <xsl:output omit-xml-declaration="yes" indent="yes"/>

<!--
    This transformation must be applied to:
        ../data/periods.xml
-->
   <xsl:variable name="vFoldlFun" as="element()">
	   <foldl-func:foldl-func/>
   </xsl:variable>

   <xsl:variable name="vA0" as="element()+">
     <period start="0" end="0"/>
   </xsl:variable>

    <xsl:template match="/">
      <xsl:sequence select="f:foldl($vFoldlFun, $vA0, /*/* )[position() >
1]"/>
    </xsl:template>

    <xsl:template match="foldl-func:*" as="element()+"
     mode="f:FXSL">
       <xsl:param name="arg1"/>
       <xsl:param name="arg2"/>

       <xsl:variable name="vLastPeriod" select="$arg1[last()]"/>

       <xsl:choose>
         <xsl:when test=
            "number($arg2/@period_begin) > number($vLastPeriod/@end)">
           <xsl:sequence select="$arg1"/>
           <period start="{$arg2/@period_begin}" end="{$arg2/@period_end}"/>
         </xsl:when>
         <xsl:otherwise>
           <xsl:sequence select="$arg1[not(. is $vLastPeriod)]"/>
           <xsl:choose>
             <xsl:when test="number($arg2/@period_end) >
number($vLastPeriod/@end)">
               <period start="{$vLastPeriod/@start}"
end="{$arg2/@period_end}"/>
             </xsl:when>
             <xsl:otherwise>
               <xsl:sequence select="$vLastPeriod"/>
             </xsl:otherwise>
           </xsl:choose>
         </xsl:otherwise>
       </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

When applied on the same source xml document:

<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"/>
	<B period_begin="32" period_end="33"/>
	<B period_begin="33" period_end="38"/>
</A>

it produces the wanted result:

<period start="1" end="12"/>
<period start="14" end="30"/>
<period start="32" end="38"/>


Cheers,
Dimitre Novatchev.

On 5/4/05, Karl Stubsjoen <kstubs@xxxxxxxxx> wrote:
> Dimitre:
>
> *weeping* I am not able to transform with XSLT2 : (
> Do you have an "elegant" XSLT1 solution?
>
> Karl
>
> On 5/3/05, Dimitre Novatchev <dnovatchev@xxxxxxxxx> wrote:
> > On 5/4/05, Karl Stubsjoen <kstubs@xxxxxxxxx> wrote:
> > > A challenge, group the following XML into 2 periods.  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>
> >
> > Hi Karl,
> >
> > This has an elegant solution using the f:foldl() function of FXSL.
> >
> > Here, I'm giving a "first glance" XSLT 2.0 solution without using FXSL.
> >
> > This transformation:
> >
> > <xsl:stylesheet version="2.0"
> > xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> >  xmlns:xs="http://www.w3.org/2001/XMLSchema"
> >  exclude-result-prefixes="xs"
> > >
> > <xsl:output omit-xml-declaration="yes" indent="yes"/>
> >
> >  <xsl:template match="A">
> >    <xsl:variable name="vStarting" select=
> >    "*[not(@period_begin/xs:integer(.)
> >         &lt;=
> >           preceding-sibling::*/@period_end/xs:integer(.)
> >           )]">
> >    </xsl:variable>
> >
> >    <xsl:for-each select="$vStarting">
> >      <xsl:variable name="vPos" select="position()"/>
> >      <period start="{@period_begin}"
> >           end="{if ($vPos = last() )
> >                 then
> >                    max( (. | following-sibling::*)
> >                           /@period_end/xs:integer(.)
> >                        )
> >                 else
> >                    max( (. | following-sibling::*)
> >                               [. &lt;&lt; $vStarting[$vPos + 1]]
> >                                           /@period_end/xs:integer(.)
> >                     )
> >                 }"
> >       />
> >    </xsl:for-each>
> >  </xsl:template>
> > </xsl:stylesheet>
> >
> > when applied on this source xml document (added one more group to yours):
> >
> > <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"/>
> > <B period_begin="32" period_end="33"/>
> > <B period_begin="33" period_end="38"/>
> > </A>
> >
> > produces the wanted result:
> >
> > <period start="1" end="12"/>
> > <period start="14" end="30"/>
> > <period start="32" end="38"/>
> >
> > Cheers,
> > Dimitre Novatchev

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.