[XML-SIG] XSLT sorting by "authors" element, with multiple authors

Tamito KAJIYAMA kajiyama@grad.sccs.chukyo-u.ac.jp
Wed, 14 May 2003 02:14:03 +0900


"Jon Berry" <jberry@sandia.gov> writes:
| 
| I'd like to sort by the last name, alphabetically by
| first differing author.  So the algorithm would be:
|     * within each article, sort authors by lastname
|     * to compare Article A with Article B:
|               * Look at last names of first author (if different, 
| comparison done)
|               * else if first authors are the same, look at second authors,
|               * etc.
|               * if not distinguished, go on to next sorting key (say 
| 'title')
| 
| Noting of course that we might be comparing articles with
| different numbers of authors.  
| 
| In initial searches, it looked like the   xsl:for-each-group
| and/or xsl:function constructs might help, but they don't seem
| to be supported by the current PyXML/4Suite implementations.
| 
| So, with the constraint that I'm trying to avoid buying a book for now,
| is this doable using templates, easy, and currently implementable
| with free software?

I believe that using extension functions is a good approach to
cope with your problem.

The primary cause of your problem is that the xsl:sort looks at
only the value of the first node, instead of all the selected
nodes.  For example, the following xsl:sort element selects all
the last names of an article's authors, but only the first last
name is used as a sort key.

<xsl:sort select="authors/person/@lastname" order="ascending" />

So, a simple solution is to change this behavior of xsl:sort by
defining an extension function like this:

<xsl:sort select="ext:strings(authors/person/@lastname)" order="ascending" />

where the extension function ext:strings() can be defined, for
example, as follows:

def strings(context, nodeset):
    return "".join(map(lambda x: x.nodeValue, nodeset))

It seems very Pythonic, doesn't it? :-)  See documentation for
more information on extension functions in Python.

Another comment:

| * if not distinguished, go on to next sorting key (say 'title')

XSLT allows multiple xsl:sort elements, so this should not be
a problem.

Hope this helps,

-- 
KAJIYAMA, Tamito <kajiyama@grad.sccs.chukyo-u.ac.jp>