Re: [xsl] <xsl:sort> using a derived element

Subject: Re: [xsl] <xsl:sort> using a derived element
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Wed, 7 Mar 2001 09:14:14 +0000
Hi Edith,

> Since I'm not doing exactly what you mentioned, I put in more
> snippets and a better picture of the data...

Great.  This shows where you're going wrong.  Here:

>         <xsl:apply-templates select="PATHFINDER/List[ListID='102000']">
>                 <xsl:sort select="substring-after(ItmDesc,'-')"
order="ascending"/>>
>         </xsl:apply-templates>

You're sorting all the List elements that are children of the
PATHFINDER element (which are children of the current node), whose
ListID child is equal to '10200'.  You're sorting those List elements
on the string after the '-' in their child ItmDesc element's value.

Now, there's only one of those List elements within the XML, so
templates will be applied to that element.  There's only one so
there's not much point doing a sort anyway, but even if there were
more, then it would look for ItmDesc elements that are children of the
List element - there aren't any in your XML (the ItmDesc elements are
children of the Itm elements).

The template that's applied is:

> <xsl:template match="PATHFINDER/List[ListID='102000']">
>         <xsl:for-each select="Itm">
>                 <OPTION><xsl:attribute name="value"><xsl:value-of
select="ItmID"/>>
> </xsl:attribute><xsl:value-of select="substring-after(ItmDesc,'-')"/>
> </OPTION>
>         </xsl:for-each>
> </xsl:template>

Here, you cycle through the Itm elements in document order.  There's
no sort on the xsl:for-each.

What I think you want to do is simply move the xsl:sort element from
the xsl:apply-templates in the first template and put it as the first
child of the xsl:for-each element in the template above.  You'll then
have:

  <xsl:for-each select="Itm">
     <xsl:sort select="substring-after(ItmDesc, '-')"
               order="ascending" />
     ...
  </xsl:for-each>

That will iterate through the Itm elements, sorted according to the
string after the '-' in the ItmDesc children of the Itm elements.
Which should work fine.

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