Re: [xsl] Selecting /getting only one node value out of the many identical values

Subject: Re: [xsl] Selecting /getting only one node value out of the many identical values
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Sun, 4 Mar 2001 12:04:57 +0000
Hi Tapan,

> this XML is genereted based on query from a user and comes from a
> database. I want to display to the user only the available "brands"
> of shirts Ie. output shall be only "Arrow" and "Lee" in my case. and
> the user can then further query on the subitems like "size" and
> "color". SO I want a < Select "brand" from "clothes/category/sno"
> where "there is no repetiion"

Getting the 'distinct' values involves getting only those values that
haven't already occurred in the document.  The basic method of doing
this is to find all the nodes where there isn't a preceding node that
has the same value in the document.

In your case you want all the sno elements:

  clothes/category/sno

And then you want to filter in those where there aren't any preceding
(sibling) sno elements whose brand is the same as the brand of the
particular sno element:

  clothes/category/sno[not(preceding-sibling::sno/brand = brand)]

If you have *lots* of sno elements, then another possibility is to
create a key that indexes the sno elements by brand:

<xsl:key name="sno-by-brand" match="sno" use="brand" />

You can then find all the sno elements with a particular brand (say
'Arrow') with the key() function:

  key('sno-by-brand', 'Arrow')

and you can find all the sno elements that are first in the list of
sno elements of a particular brand, as returned by the key, with:

  clothes/category/sno[generate-id() =
                       generate-id(key('sno-by-brand', brand)[1])]

or:

  clothes/category/sno[count(.|key('sno-by-brand', brand)[1]) = 1]

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