[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Newbie needs help with sorting a filtered list

Subject: Re: Newbie needs help with sorting a filtered list
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Mon, 29 Jan 2007 00:40:09 -0500
Re:  Newbie needs help with sorting a filtered list
At 2007-01-28 23:54 -0500, M. Casey wrote:
I'm an absolute newbie at XSL.

Welcome!


I have an ASP form which is passing two values to my xsl -- let's say for this example "food" and "fruit". I need an alphabetized list of all items that match those criteria. So;
apple
banana
orange
is the result I want.


Here's the XML:
...
This gives me the correct items, but not in alphabetical order:

<xsl:param name="param1"/>
<xsl:param name="param2"/>

<xsl:template match="catalog/entry">
  <xsl:if test="category[@type=$param1] and category[@subcat=$param2]">
    <xsl:for-each select="word">

This sorts the one word within each entry that fits the criteria and returns the result in the order of the entries:


Returns this list:

orange
apple
banana

This one gives me an alphabetized list, but of all the items, not just the ones that match the params:

<xsl:template match="catalog">
<xsl:if test="entry/category[@type=$param1] and entry/category[@subcat=$param2]">
<xsl:for-each select="entry/word">

This sorts all of the words if any of the entries fit the criteria:


Returns this:

apple
banana
endive
kale
orange
spinach

What am I doing wrong,

You are not selecting only the ones meeting your criteria and then sorting those ... please see the example below.


and why do these two versions, which to my rank amateur brain look like they should be the same, return such different results?

Because you are not considering the position of the current node and the selection of the current node list for the sort.


I hope the answer below helps.

. . . . . . . . . Ken

T:\ftemp>type casey.xml
<catalog>
  <entry>
    <word>orange</word>
    <category type="food" subcat="fruit"/>
    <info>
      <serving_size>...</serving_size>
      <preparation>...</preparation>
    </info>
  </entry>
  <entry>
    <word>spinach</word>
    <category type="food" subcat="vegetable"/>
    <info>
      <serving_size>...</serving_size>
      <preparation>...</preparation>
    </info>
  </entry>
  <entry>
    <word>apple</word>
    <category type="food" subcat="fruit"/>
    <info>
      <serving_size>...</serving_size>
      <preparation>...</preparation>
    </info>
  </entry>
  <entry>
    <word>kale</word>
    <category type="food" subcat="vegetable"/>
    <info>
      <serving_size>...</serving_size>
      <preparation>...</preparation>
    </info>
  </entry>
  <entry>
    <word>endive</word>
    <category type="food" subcat="vegetable"/>
    <info>
      <serving_size>...</serving_size>
      <preparation>...</preparation>
</info>
  </entry>
  <entry>
    <word>banana</word>
    <category type="food" subcat="fruit"/>
    <info>
      <serving_size>...</serving_size>
      <preparation>...</preparation>
    </info>
  </entry>
</catalog>

T:\ftemp>type casey.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<xsl:output method="text"/>

<xsl:param name="param1"/>
<xsl:param name="param2"/>

<xsl:template match="/">
  param1=<xsl:value-of select="$param1"/>
  param2=<xsl:value-of select="$param2"/>
  <xsl:text>
</xsl:text>
  <xsl:for-each select="catalog/entry[category/@type=$param1 and
                                      category/@subcat=$param2]">
    <xsl:sort select="word"/>
    <xsl:apply-templates select="word"/>
    <xsl:text>
</xsl:text>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>xslt casey.xml casey.xsl con param1=food param2=fruit

  param1=food
  param2=fruit
apple
banana
orange

T:\ftemp>

--
World-wide corporate, govt. & user group XML, XSL and UBL training
RSS feeds:     publicly-available developer resources and training
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Cancer Awareness Aug'05  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.