Re: [xsl] Creating a padded sort key: easier from elt sequence or string?

Subject: Re: [xsl] Creating a padded sort key: easier from elt sequence or string?
From: Yves Forkl <Y.Forkl@xxxxxx>
Date: Wed, 26 Jul 2006 10:59:10 +0200
I would like to say Thank you to darkman, David Carlisle and Michael Kay for your precious comments.

drkm> >> [Which one of two representations of "3.11.A.9" is
drkm> >> easier to turn into a padded sort key? 1) a string]
drkm> >>
drkm> >> 2) an element containing a sequence of PCDATA elements,
drkm> >> one for each of the numbers and letters (the dot in
drkm> >> between may be added during processing)
drkm>
drkm>
drkm>   With that, you can simply use several xsl:sort.

So I'll try with the second approach. (As I am currently not using
Saxon, I don't want to depend on its built-in collation options for
the moment.)

I suppose indicating multiple sort keys would yield something like
this in the case of "3.11.A.9", when each of the numbers and letters is marked up as sort_key_elt:


<xsl:for-each select="$my_nodeset_to_sort">
  <xsl:sort select="/path_to_dotted_sequence_elt/sort_key_elt[1]"
    data-type="number"/>
  <xsl:sort select="/path_to_dotted_sequence_elt/sort_key_elt[3]"
    data-type="number"/>
  <xsl:sort select="/path_to_dotted_sequence_elt/sort_key_elt[2]"
    data-type="text"/>
  <xsl:sort select="/path_to_dotted_sequence_elt/sort_key_elt[4]"
    data-type="number"/>
</xsl:for-each>

However, I face two problems:

- As my dotted sequence can be of variable length, I need to determine
  the number of sort statements dynamically. Maybe by using recursion?
  (How to modify the lines above in order to avoid undesired changes
  of the context node when calling the recursion template)?

- The position of the numbers and letters in the sequence can also
  vary. While Jeni Tennison recommends simply using a variable to set
  the value of attribute data-type dynamically (in "XSLT and XPath On
  The Edge", p. 157), I can't see how this could combine with the
  first problem, i.e. dynamic selection of the number of sort keys,
  into a sensible thing.

What might a sketch of a template for sorting based on the sort key in
the element sequence look like?

Or is it better to concatenate the padded contents of all sort_key_elt
elements into a string to be used as a single sort key of type "text"?

Yves

Current Thread