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

Subject: RE: [xsl] <xsl:sort> question.
From: Jeff Beadle <Jbeadle@xxxxxxxx>
Date: Fri, 19 Apr 2002 13:25:14 -0400
Hello,

They shouldn't sort the same.

In Version1 you're matching on the document root, then rolling through a
list of addressbook/address elements sorted by name/last-name child
elements.

In Version2 your matching on the addressbook/address element directly and
not rolling through them as list, your for-each will only execute one time
because within the given template you only have one addressbook/address
element ... you should get many, un-sorted, <p> elements.


What you may want to try is this:

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

or, if you're worried about only finding addressbook elements with address
children:

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

HTH,
Jeff


-----Original Message-----
From: Dung, Ming-tzung [mailto:Ming-tzung_Dung@xxxxxxxxxxxxx]
Sent: Friday, April 19, 2002 12:54 PM
To: XSLT List (E-mail)
Subject: [xsl] <xsl:sort> question.


   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!!
------
**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