Re: [xsl] <xsl:sort> question.

Subject: Re: [xsl] <xsl:sort> question.
From: "Thomas B. Passin" <tpassin@xxxxxxxxxxxx>
Date: Fri, 19 Apr 2002 13:33:09 -0400
[Dung, Ming-tzung]

>    The version 1 will work and the version 2 will not sort the data, even
> though I think that these two are logically equivalent.
>   Please let me know what you think?  Thanks in advance!!

They are not the same.  In version 1, the template gets instantiated once.
The four addressbook/address elements then get processed by the for-each.

In version 2, the template gets instantiated for each separate
/addressbook/address element, for a total of four times in your example.
Each time, the for-each gets applied to the current node, which consists of
a single address node.  So there is nothing to sort.

You can demonstrate this by adding a visible marker so you can see when the
template has been instantiated, like this

  <xsl:template match="/addressbook/address">
    --template called --
    <xsl:for-each select=".">
      <xsl:sort select="name/last-name"/>
      <p>
      <xsl:value-of select="name/last-name"/>
      </p>
    </xsl:for-each>
    </html>
  </xsl:template>

You will see the marker once for version 1, four times for version 2.

Cheers,

Tom P

> ------
> **Version1 - output the sorted last name**
> <xsl:template match="/">
>      <xsl:for-each select="addressbook/address">
>       <xsl:sort select="name/last-name"/>
>       <p>
>       <xsl:value-of select="name/last-name"/>
>       </p>
>     </xsl:for-each>
> </xsl:template>
>
> **Version 2 - output the sorted last name**
>   <xsl:template match="/addressbook/address">
>     <xsl:for-each select=".">
>       <xsl:sort select="name/last-name"/>
>       <p>
>       <xsl:value-of select="name/last-name"/>
>       </p>
>     </xsl:for-each>
>   </xsl:template>
>
> **Input xml data
> -----
> <?xml version="1.0"?>
> <addressbook>
>   <address>
>     <name>
>       <title>Mr.</title>
>       <first-name>Chester Hasbrouck</first-name>
>       <last-name>Frisby</last-name>
>     </name>
>   </address>
>   <address>
>     <name>
>       <first-name>Harry</first-name>
>       <last-name>Backstayge</last-name>
>     </name>
>   </address>
>   <address>
>     <name>
>       <first-name>Mary</first-name>
>       <last-name>McGoon</last-name>
>     </name>
>   </address>
>   <address>
>     <name>
>       <title>Ms.</title>
>       <first-name>Amanda</first-name>
>       <last-name>Reckonwith</last-name>
>     </name>
>   </address>
> </addressbook>
> -----
>
> Ming
>
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
>


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


Current Thread