RE: How to transform flat structure into hierarchical one?

Subject: RE: How to transform flat structure into hierarchical one?
From: Jeni Tennison <Jeni.Tennison@xxxxxxxxxxxxxxxx>
Date: Wed, 07 Jun 2000 17:27:22 +0100
Aleksandrs,

Paulo Gaspar wrote:
>And the typical algorithm to group the nodes has you wish involves a few
>logical steps:
> A - Sort the nodes by house number;
> B - Separate the records in groups with the same house_id.

When I read this, I realised that I forgot to include in my reply to you
anything about sorting the rooms and houses within your records.  It is
very easy to add sorting to the solution that I sent earlier: you use an
xsl:sort element within each xsl:for-each, with the select attribute
indicating the thing that you want to sort by.  So in your case:

<xsl:template match="record_set">
  <house_list>
    <!-- cycle through the first records in each group -->
    <!-- in order based on house_id -->
    <xsl:for-each select="record[generate-id() = generate-id(key('records',
house_id)[1])]">
      <xsl:sort select="house_id" />
      <house>
        <id><xsl:value-of select="house_id" /></id>
        <rooms>
          <!-- cycle through each of the records in the group -->
          <!-- in order based on room_id -->
          <xsl:for-each select="key('records', house_id)">
            <xsl:sort select="room_id" />
            <room>
              <id><xsl:value-of select="room_id" /></id>
            </room>
          </xsl:for-each>
        </rooms>
      </house>
    </xsl:for-each>
  </house_list>
</xsl:template>

The same thing can be done with xsl:apply-templates (by including an
xsl:sort within the xsl:apply-templates element), if you were using that
instead.

Sorry for the ommission.

Cheers,

Jeni

Dr Jeni Tennison
Epistemics Ltd, Strelley Hall, Nottingham, NG8 6PE
Telephone 0115 9061301 ? Fax 0115 9061304 ? Email
jeni.tennison@xxxxxxxxxxxxxxxx



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


Current Thread