Re: a new(?) grouping problem

Subject: Re: a new(?) grouping problem
From: Nick Browne <NickBrowne@xxxxxxxxxxxxxxx>
Date: Thu, 29 Jun 2000 12:18:36 +0100
Mike, try this to solve your grouping problem. It gives the answer you wanted based on the data given (which I
appreciate may not always the full solution). I'm sure you don't need my (verbose) comments but I'll leave
them in for my benefit !  :

<?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"/>

 <xsl:template match="/">

  <xsl:variable name="HoursData" select="/Hours/*[not(name()='Holidays')]"/>

  <table>
   <xsl:for-each select="$HoursData[normalize-space() !=
                          normalize-space(preceding-sibling::*[1])]">
    <tr>

     <td>
      <xsl:value-of select="name()"/>
      <!-- if current day isn't Sunday, and next day's hours are
           the same as this day's, print hyphen -->
      <xsl:if test="name() != 'Sunday' and . =
              following-sibling::*[not(name()='Holidays')][1]">
      <xsl:text>-</xsl:text>

      <!-- the question is, what goes here in place of foo? -->

      <xsl:call-template name="hours_chk">
       <xsl:with-param name="pos" select="1"/>
      </xsl:call-template>

      </xsl:if>
     </td>
     <td>
      <xsl:value-of select="."/>
     </td>

    </tr>
   </xsl:for-each>
  </table>
 </xsl:template>

 <!-- Using the (single) day element initially passed from the for-each as
      the context, work forward via recursion until a day is found
      that has a different 'hours' value and is not 'Holidays'          -->

 <xsl:template name="hours_chk">
  <xsl:param name="pos"/>

  <!-- Decide whether we need to keep searching ..          -->

  <xsl:choose>

   <!-- If there is still another following day with a value
        equal to the context value, call the template again and
        repeat the check for the next day                                                    -->

   <xsl:when test="(following-sibling::*[$pos]/text() = text() and
                    name(following-sibling::*[$pos]) != 'Holidays')">

    <xsl:call-template name="hours_chk">
     <xsl:with-param name="pos" select="$pos+1"/>
    </xsl:call-template>

   </xsl:when>

  <!-- Stop the search. If here, then we have found a day where the
       hours differed from the first in the group. Display the day name
       of the one before the failing one, i.e. display the last in the group -->

  <xsl:otherwise>
    <xsl:value-of select="name(following-sibling::*[$pos - 1])"/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

</xsl:stylesheet>

Nick Browne
Slipstone Ltd


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


Current Thread