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

Re: a new(?) grouping problem

Subject: Re: a new(?) grouping problem
From: "Nikolai Grigoriev" <grig@xxxxxxxxxxx>
Date: Thu, 29 Jun 2000 17:22:17 +0400
Re: a new(?) grouping problem
Mike Brown wrote:

> I want to group consecutive days with the same hours together, and just
> print the first and last day in each group.
>
> I also want to ignore the 'Holidays' day. I put it in there because I
> can't use a solution that assumes Sunday's hours aren't followed by
> something that could be the same.

This can be achieved by recursion: you call a template recursively until there's
no more following-siblings that have the same text (passed as a param), like in
the stylesheet below:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="html" version="4.0"/>

<!-- Root template: just create a table. -->
<xsl:template match="Hours">
  <table><xsl:apply-templates/></table>
</xsl:template>

<!-- Exclude holidays from processing -->
<xsl:template match="Holidays" priority="2"/>

<!-- Single days, except for holidays. -->
<!-- A modeless template creates the row -->
<xsl:template match="Hours/*">
  <xsl:variable name="hours" select="text()"/>

  <xsl:if
      test="not(preceding-sibling::*[not(self::Holidays)][1][text()=$hours])">
    <tr>
      <td>
        <xsl:value-of select="name()"/>
        <xsl:apply-templates mode="end"
              select="following-sibling::*[not(self::Holidays)][1]">
          <xsl:with-param name="hours" select="$hours"/>
        </xsl:apply-templates>
      </td>
      <td><xsl:value-of select="."/></td>
    </tr>
  </xsl:if>
</xsl:template>

<!-- A day closes the period if there's no better candidate -->
<xsl:template match="Hours/*" mode="end">
  <xsl:param name="hours"/>

  <xsl:if test="text()=$hours">
    <xsl:choose>
      <xsl:when
          test="following-sibling::*[not(self::Holidays)][1][text()=$hours]">
        <xsl:apply-templates mode="end"
              select="following-sibling::*[not(self::Holidays)][1]">
          <xsl:with-param name="hours" select="$hours"/>
        </xsl:apply-templates>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text> - </xsl:text><xsl:value-of select="name()"/>
      </xsl:otherwise>
    </xsl:choose>

  </xsl:if>
</xsl:template>

</xsl:stylesheet>




 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


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.