I am trying to build a list that parses my entire xml document. I need to list the numeric names then the alpha names. The list should look something like this.
6 6600 Training 6500 Training A Accelerated Training T Training
This is a snippet of the xml.
<courses> <course> <name>Accelerated Training</name> </course> <course> <name>6600 Training</name> </course> <course> <name>Training</name> </course> <course> <name>6500 Training</name> </course> </courses>
This is the code I am currently using. I found this in another question on the site and have customized it somewhat. Currently it doesn’t take into account my need for parsing by number and it also returns out of alphabetical order.
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:output omit-xml-declaration='yes' indent='yes'/> <xsl:variable name='vLower' select= ''abcdefghijklmnopqrstuvwxyz''/> <xsl:variable name='vUpper' select= ''ABCDEFGHIJKLMNOPQRSTUVWXYZ''/> <xsl:key name='kTitleBy1stLetter' match='courses/course' use='substring(name,1,1)'/> <xsl:template match='/*'> <xsl:for-each select='course [generate-id() = generate-id(key('kTitleBy1stLetter', substring(name,1,1)) [1] ) ]'> <xsl:variable name='v1st' select='substring(name,1,1)'/> <h2><xsl:value-of select='$v1st'/></h2> <div class='{translate($v1st, $vUpper, $vLower)}-content'> <ul> <xsl:for-each select='key('kTitleBy1stLetter',$v1st)'> <li><xsl:value-of select='name'/></li> </xsl:for-each> </ul> </div> </xsl:for-each> </xsl:template> </xsl:stylesheet>
Basically you need to group by first letter and sort by
<name>. You are on a good way with your Muenchian grouping approach already.I would suggest an alternative that’s a bit easier on the eye:
outputs:
EDIT: For the sake of legibility I left out the upper-casing of the first letter. The correct key would be this (you can’t use a variable in a key, hence the literal alphabet strings):
The same goes of course for the
$initialvariable in the second template, but here you can in fact use variables again.EDIT #2: Since sorting is case-sensitive as well, you can use the same expression: