469,641 Members | 1,376 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,641 developers. It's quick & easy.

XSLT sorting question

Hi,

I need to generate a list based on a sorted nodeset, except duplicates
need to be discarded. I initially though of doing a sort on a nodeset and
then passing it to a template that iterates over the set and compares the
current element it's processing to the previous one, continueing to the
next element immediately if it was the same as the previous one.

However, quite annoyingly the xsl:sort command cannot "just" be called, I
need to call it in a for-each, which seems to destroy the ability to
determine what the previous element processed was.

My idea was to do the following (in prologlike code)

buildlist() :-
node('//entry',List),
sort(List,SortedList)
writelist(Sortedlist,'',1).

writelist(List, Lastseen, Count) :-
Newcounter is Count + 1,
Element = List[Count],
( Lastseen != Element,
writeelement(Element)
;
# do nothing )
writelist(List,Element,Newcount).

writeelement(E) :- # do whatever writing is required here

I am not entirely sure why sort() never became a select function in
addition (or frankly, replacement) to being an element that can only be
used inside iteration, but it would seem that the fact that one cannot do
something like:

<xsl:variable name="sortedvarname" select="sort(variable,condition-
string)"/>

was a bad idea - but this is a problem I have with XSLT's design, so that
won't get me any closer to an answer =)

Does anyone know how to either obtain the count for which element from a
set is used in a foreach, or how to sort a nodeset before sending it on
for further processing in another template?

Mike Kamermans

Nov 23 '05 #1
6 2328


Michiel Kamermans wrote:

However, quite annoyingly the xsl:sort command cannot "just" be called, I
need to call it in a for-each,
xsl:sort can be used inside of <xsl:apply-templates>, not only
xsl:for-each. It might help to write two stylesheets, one that sorts as
needed, the second that takes the result of the first as the input and
then does what you additionally want to do. You simply chain those two
stylesheets then to get the complete result.
Or if your processor supports an extension function to convert a result
tree fragment into a node set then you can sort and store in a variable
(which then contains a result tree fragment), then use the extension
function to convert the result tree fragment to a node set and do what
you need to do with the node set.

On the other hand when I think about the initial requirement
I need to generate a list based on a sorted nodeset, except duplicates
need to be discarded


then I think it should be possible in one step, simply use grouping to
eliminate duplicates in the node set you sort and process further e.g.
<xsl:apply-templates select="item[conditionHereToFilterDuplicates]">
<xsl:sort ... />
</xsl:apply-templates>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 23 '05 #2
Sadly this solution would require an "apply-template", while I need the
functionality in a "call-template" instead. Is there any creative way that
you (or anyone else) knows of to get a sorted, duplicates removed nodeset
to pass on to a custom function?

Mike Kamermans

Nov 23 '05 #3


Michiel Kamermans wrote:
Sadly this solution would require an "apply-template", while I need the
functionality in a "call-template" instead. Is there any creative way that
you (or anyone else) knows of to get a sorted, duplicates removed nodeset
to pass on to a custom function?


As said, you can write two stylesheets and chain them (have the second
process the result of the first) or you can use an extension function to
convert a result tree fragment to a node set. Many XSLT processors have
such a function and EXSLT has attempted to standardize the name and
namespace that function lives in:
<http://www.exslt.org/exsl/functions/node-set/index.html>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 23 '05 #4
I'll check up on that... but my word is that the worst name ever; "extended
extendible [...]"?

*shakes head*

Mike
Nov 23 '05 #5
Michiel Kamermans said the following on 11/14/2005 13:04 +0200:
I need to generate a list based on a sorted nodeset, except duplicates
need to be discarded. [..]


Googling for "xsl sort uniq" gives a lot of hits, it looks like somebody
have been there before. This one looks like what you want:

http://www.xslt.com/html/xsl-list/2002-02/msg00571.html

If not, try Google with those three words.

--
Regards
Harrie
Nov 23 '05 #6
Michiel Kamermans wrote:
Hi,

I need to generate a list based on a sorted nodeset, except duplicates
need to be discarded.


This is discussed in extenso on the XSL List (www.mulberrytech.com)
and in the XSL FAQ (http://www.dpawson.co.uk/xsl/xslfaq.html). There
are several techniques which use the Muenchian method to do what you
describe.

///Peter
--
XML FAQ: http://xml.silmaril.ie/

Nov 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Ben Fitzgerald | last post: by
3 posts views Thread by Philipp Lenssen | last post: by
reply views Thread by Christopher M. Lauer | last post: by
1 post views Thread by ralf321 | last post: by
5 posts views Thread by RicercatoreSbadato | last post: by
6 posts views Thread by bcochofel | last post: by
3 posts views Thread by silver_sabrina | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.