Subject: Re: [xsl] Sorting problem... From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx> Date: Wed, 14 Feb 2001 11:48:58 +0000 |
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
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Sorting problem..., cutlass | Thread | [xsl] Re: empty element sabotages x, Dimitre Novatchev |
Re: [xsl] XSLT 1.1 comments, Tobias Reif | Date | Re: [xsl] Grouping, Jeni Tennison |
Month |