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>
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" />
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With