Ask Ben: Sorting A Sub-Tree Of An XML Document Using ColdFusion And XSLT

<!--- Define the XML data. --->
<cfxml variable="xmlData">
 
	<xmlitems>
		<xmlitem name="b">something</xmlitem>
		<xmlitem name="a">somethingelse</xmlitem>
	</xmlitems>
 
</cfxml>
 
 
<!--- Define the XSL Transofrm data. --->
<cfxml variable="xmlTransform">
 
	<!--- Document type declaration. --->
	<?xml version="1.0" encoding="ISO-8859-1"?>
 
	<xsl:transform
		version="1.0"
		xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
		<!--- Match the root node (xmlitems). --->
		<xsl:template match="xmlitems">
 
			<!---
				Copy the current node's top-level values
				(the tag and it's attributes, but not it's
				descendents).
			--->
			<xsl:copy>
 
				<!--- Loop over the xmlitem nodes. --->
				<xsl:for-each select="xmlitem">
 
					<!---
						As we are looping over these xmlitem
						nodes, sort them ascendingly according
						to their NAME attribute.
					--->
					<xsl:sort
						select="@name"
						data-type="text"
						order="ascending"
						/>
 
					<!---
						Copy the entire node (include its
						descendantas).
					--->
					<xsl:copy-of select="." />
 
				</xsl:for-each>
 
			</xsl:copy>
 
		</xsl:template>
 
	</xsl:transform>
 
</cfxml>
 
 
<cfoutput>
 
	<!--- Output the transformation. --->
	#HTMLEditFormat(
		XmlTransform( xmlData, xmlTransform )
		)#
 
</cfoutput>

For Cut-and-Paste