The reason is here:
<parts> <xsl:apply-templates> <xsl:sort select="object"/> </xsl:apply-templates> </parts>
This applies the patterns to the children ( object ) of the current node ( objects ) and sorts them by the string value of their first child element.
However, in the provided XML document, the object does not have any child elements of the object , so they all have the same sort key - an empty string - and their original order does not change the sort.
Decision
<parts> <xsl:apply-templates> <xsl:sort select="."/> </xsl:apply-templates> </parts>
Full conversion becomes :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="objects"> <parts> <xsl:apply-templates> <xsl:sort select="."/> </xsl:apply-templates> </parts> </xsl:template> <xsl:template match="object"> <part> <xsl:apply-templates/> </part> </xsl:template> </xsl:stylesheet>
and when it applies to the provided XML document:
<objects> <object>Clutch</object> <object>Gearbox</object> <object>Cylinder head</object> <object>Starter</object> <object>Airbox</object> <object>Inlet manifold</object> </objects>
the desired, correct result is output:
<parts> <part>Airbox</part> <part>Clutch</part> <part>Cylinder head</part> <part>Gearbox</part> <part>Inlet manifold</part> <part>Starter</part> </parts>
source share