[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: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 03 May 2005 16:51:38 -0400
group start end by periods
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

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.