Re: [xsl] dynamic sorting

Subject: Re: [xsl] dynamic sorting
From: David Carlisle <davidc@xxxxxxxxx>
Date: Thu, 5 Feb 2004 15:30:50 GMT
This is a FAQ

  <xsl:param name="sort">person</xsl:param>
That makes $sort in to a result tree fragment consisting of a root node
and a text node with the string "person".

It's an error to query in to a result tree fragment and so
<xsl:apply-templates select="$sort">
is an error.
In XSLT2 it's not an error but it will apply templates to that new
document that just contains the string person and no element nodes, so
it will do the wrong thing.

If you had done
  <xsl:param name="sort" select="person"/>

then the sort param would have been the person element and so
<xsl:apply-templates select="$sort">
would be legal although then the current node would be the person
elements and so you probably would want

<xsl:sort select="fname"/>
rather than
<xsl:sort select="person/fname"/

unless person elements have a person child.

The whole thing looks suspicous as it is in a template for /
if you are in a well formed document there can only be one element child
of / and so you are sorting a list of one.

  My goal is that the user of my xsl file can give a parameter "sort" (to his
 xslt parser) which defines his preferred way of sorting.

so you will probably be passing in a string and you want to sort on an
element with that name, so faq answer is
<xsl:sort select="*[name()=$sort]"/>

But again you need to make sure you are sorting a list of more than one



This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:

 XSL-List info and archive:

Current Thread