473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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 2481


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ben Fitzgerald | last post by:
Hi I feel I'll be asking for someone to turn water into wine before this happens, but just in case! I'd like to have a page that provides a basic xml document with an xslt that defines the...
3
by: Philipp Lenssen | last post by:
I have this XML file which I would like to XSLT-sort based on the string contained within the item-children elements using basic MSXML (no recent version of IIS, so it might be an outdated MSXML --...
1
by: David | last post by:
I would like to be able to re-sort data in an HTML table on the without returning to the server. It seems like an XSLT should be able to accomplish this, but I can't find enough information... ...
0
by: Christopher M. Lauer | last post by:
I have done my best to answer this question but can not find the proper set of commands. I would like to transform an xml file (in code behind) and display its output in a specific html tag,...
1
by: ralf321 | last post by:
hello! i have this xml: <object> <node meta="27" > <data meta="13" changed="2005-09-05 16:16:43">a</data> <data meta="14" changed="2005-11-06 19:26:49">b</data> <data meta="15" ...
5
by: RicercatoreSbadato | last post by:
I want to load a table with 2 columns from an xml file. Is it possible with xslt to sort the columns by clicking on their header ?
6
by: bcochofel | last post by:
I'm using xsl to list an xml file that contains something like: sites, tag and weight. I'm listing this in a table with the following titles: | URL | TAG | WEIGHT (each title his a link) What...
3
by: silver_sabrina | last post by:
Hey everyone, I'm having some trouble with this. I need to convert one xml doc into another and I think that XSLT may be the answer, so I'd like some help from the gurus out here :) If XSLT cannot...
1
by: aplonis | last post by:
I have an XML (Atom) doc at this URL... Atom ...which displays as HTML using any one of three XSLT stylesheets, the principal one being at this URL... XSLT ...with two...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.