Altova Mailing List Archives


Re: [xsl] Sorting problem...

From: Jeni Tennison <mail@---------------->
To:
Date: 2/14/2001 1:52:00 AM
Hi David,

> I'm trying to sort some concatenated strings in a dropdown box in my
> XSL. (I'm also sure there's a more compact way to write my XSL,
> frankly). My XML is:
>
> <root sub_id="84">
>         <folder name="c" cdate="2/13/01" id="f_49">
>                 <folder name="m" cdate="2/13/01" id="f_42" />
>         </folder>
>         <folder name="y" cdate="2/13/01" id="f_45" />
>         <folder name="d" cdate="2/13/01" id="f_43" />
>         <folder name="r" cdate="2/13/01" id="f_44" />
>         <folder name="d" cdate="2/13/01" id="f_49">
>                 <folder name="t" cdate="2/13/01" id="f_42">
>                         <folder name="z" cdate="2/13/01" id="f_42">
>                 </folder>
>         </folder>
> </root>
>
> I want my output to look alphabetical like the following:
>
> -c
> --c:m
> -d
> --d:t
> ---d:t:z
> -r
> -y

There are two issues here - the alphabetical ordering and getting
the right number of dashes before each of the folder names in the
hierarchy.

Let's work on the template for each folder first.  For each folder,
you want a dash if it's first level (has only one ancestor element),
two dashes if it's second level (has two ancestor elements), three if
it's third level and so on.  If you iterate over each of the ancestor
elements and add a dash each time, then you'll get the number of
dashes you need (a lot quicker and more extensible than counting them
and then adding the number you're after):

  <xsl:for-each select="ancestor::*">-</xsl:for-each>

Then, you want the name of each folder ancestor, and the name of the
folder itself, separated by colons.  You can get this by iterating
over each of the folder ancestors and adding their name, then a colon,
and finally adding the name of the current folder:

  <xsl:for-each select="ancestor::folder">
     <xsl:value-of select="@name" />:<xsl:text />
  </xsl:for-each>
  <xsl:value-of select="@name" />

Or by iterating over each of the folder ancestors and the current
folder, and not adding a colon for the last folder (the current one):

  <xsl:for-each select="ancestor-or-self::folder">
     <xsl:value-of select="@name" />
     <xsl:if test="position() != last()">:</xsl:if>
  </xsl:for-each>

Putting that together with the other things you had in your template,
you get:

<xsl:template match="folder">
   <option value="{@id}">
      <xsl:for-each select="ancestor::*">-</xsl:for-each>
      <xsl:for-each select="ancestor::folder">
         <xsl:value-of select="@name" />:<xsl:text />
      </xsl:for-each>
      <xsl:value-of select="@name" />
   </option>
   <xsl:apply-templates />
</xsl:template>

Now, to sort the folders correctly, you have to apply templates to
them in the right order.  That involves the xsl:sort instruction
embedded within the xsl:apply-templates.

  <xsl:apply-templates>
     <xsl:sort select="@name" />
  </xsl:apply-templates>

You need that both when you originally apply templates within the
root-matching template and when you apply templates within the
folder-matching template:

<xsl:template match="root">
  <xsl:apply-templates>
     <xsl:sort select="@name" />
  </xsl:apply-templates>
</xsl:template>

<xsl:template match="folder">
   <option value="{@id}">
      <xsl:for-each select="ancestor::*">-</xsl:for-each>
      <xsl:for-each select="ancestor::folder">
         <xsl:value-of select="@name" />:<xsl:text />
      </xsl:for-each>
      <xsl:value-of select="@name" />
   </option>
  <xsl:apply-templates>
     <xsl:sort select="@name" />
  </xsl:apply-templates>
</xsl:template>

A final thing.  I notice that in your source you have two folders
named 'd' but in your output, you have only one option.  I'm not sure
how you want them to be combined, especially if they have different
subfolders, but if you do want to merge the two, then you might want
to look at information on Muenchian grouping e.g. at
http://www.jenitennison.com/xslt/grouping/muenchian.html.

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



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

Disclaimer

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.