Looping and Sorting - Exercise

Contact Us or call 1-877-932-8228
Looping and Sorting - Exercise

Looping and Sorting

Duration: 5 to 10 minutes.

In this exercise, you will modify the XSLT you created in the last exercise so that the results are sorted by FirstName.

  1. Open FlowControl/Exercises/ForEachSort.xsl for editing.
  2. Modify the code so that the results will be sorted by FirstName.
  3. To test your solution, transform FlowControl/Exercises/BusinessLetterSort.xml against FlowControl/Exercises/ForEachSort.xsl.

Solution:

FlowControl/Solutions/ForEachSort.xsl
<?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="/">
		<Names>
		<xsl:for-each select="//Name">
			<xsl:sort select="FirstName"/>
				<Name>
					<Title><xsl:value-of select="@Title"/></Title>
					<FName><xsl:value-of select="FirstName"/></FName>
					<LName><xsl:value-of select="LastName"/></LName>
				</Name>
		</xsl:for-each>
		</Names>
	</xsl:template>
</xsl:stylesheet>
Next