Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSL Sort treats lowercase separately from uppercase

My XSLT sorts lastnames alphabetically, but I just noticed that some of the names start with "de" and "von" or "van". These lowercase prefixes are being sorted and placed AFTER the Uppercase names. How do I tell the XSLT to sort all cases together?

Using XSLT 1.0

Here's the section that sorts the data:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="Data">
<xsl:strip-space elements="*"/>
<xsl:template match="Data">
<xsl:copy>
<xsl:apply-templates select="Consultant">
<xsl:sort select="Surname" order="ascending" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>  
<xsl:template match="Consultant">
<consultant><Surname><xsl:value-of select="Surname"/></Surname>
<FirstName><xsl:value-of select="FirstName"/></FirstName>
<!--…-->
</consultant>
</xsl:template>
</xsl:stylesheet>

Here is some sample XML:

<?xml version="1.0" encoding="UTF-8"?>
<Data>
<consultant>
<Surname>Arnon</Surname>
<FirstName>Lana</FirstName>
</consultant>
<consultant>
<Surname>von Armon</Surname>
<FirstName>George</FirstName>
</consultant>
<consultant>
<Surname>Arnon</Surname>
<FirstName>Lana</FirstName>
</consultant>
<consultant>
<Surname>de Armon</Surname>
<FirstName>George</FirstName>
</consultant>
</Data>
like image 503
Jim Maivald Avatar asked Sep 08 '25 07:09

Jim Maivald


2 Answers

You need to normalize the case in the <xsl:sort/>

If your environment supports XPATH 2.0 then you can use either upper-case() or lower-case() like below:

<xsl:sort select="upper-case(Surname)" order="ascending" />

If your environment doesn't support XPATH 2.0, then you will need to use translate() like the following:

<xsl:sort select="translate(Surname, 'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')" order="ascending" />
like image 78
Keith Matthew Avatar answered Sep 10 '25 03:09

Keith Matthew


Using translate() is a hack which you should resort to only if your processor does not support the lang attribute of <xsl: sort> - see http://www.w3.org/TR/xslt#sorting.

like image 36
michael.hor257k Avatar answered Sep 10 '25 03:09

michael.hor257k