Improving Performance with Keys - Exercise

Contact Us or call 1-877-932-8228
Improving Performance with Keys - Exercise

Improving Performance with Keys

Duration: 15 to 20 minutes.

In this exercise, you will make an XSLT more efficient by using keys.

  1. Open Keys/Exercises/Top500SongsKey.xml and review the code. You will see that it contains a list of Song elements. Part of the code is shown below these instructions.
  2. Transform Keys/Exercises/Top500SongsKey.xml against Keys/Exercises/SongsByArtist.xsl. You will see that, for each song, it outputs the number of songs that artist has in the top 500. Currently, this is done without using keys.
  3. Open Keys/Exercises/SongsByArtist.xsl for editing.
  4. Modify the code so that it uses keys to do the same thing as it currently does.
  5. To test your solution, transform Keys/Exercises/Top500SongsKey.xml against Keys/Exercises/SongsByArtist.xsl.

Code Sample:

Keys/Exercises/Top500SongsKey.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="SongsByArtist.xsl"?>
<Songs Source="http://classicrock.about.com/library/misc/ptop500.htm">
	<Song Artist="Led Zeppelin">Stairway To Heaven</Song>
	<Song Artist="Rolling Stones">Satisfaction</Song>
	<Song Artist="Derek And The Dominoes">Layla</Song>
	<Song Artist="Beatles">A Day In The Life</Song>
	<Song Artist="Who">Won't Get Fooled Again</Song>
	<Song Artist="Doors">Light My Fire</Song>
	<Song Artist="Pink Floyd">Comfortably Numb</Song>
	<Song Artist="Eagles">Hotel California</Song>
	<Song Artist="Bruce Springsteen">Born To Run</Song>
---- C O D E   O M I T T E D ----

</Songs>

Code Sample:

Keys/Exercises/SongsByArtist.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="/">
		<Artists>
			<xsl:for-each select="Songs/Song">
				<xsl:variable name="Artist" select="@Artist"/>
				<Artist>
					<xsl:value-of select="@Artist"/>
					<xsl:text> songs in the top 500: </xsl:text>
					<xsl:value-of select="count(//Song[@Artist=$Artist])"/>
				</Artist>
			</xsl:for-each>
		</Artists>
	</xsl:template>
</xsl:stylesheet>

Challenge

Modify the code so that duplicates are removed. Sort the results by number of songs in the top 500 in descending order.

Solution:

Keys/Solutions/SongsByArtist.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:key name="keySongs" match="Song" use="@Artist"/>
	<xsl:template match="/">
		<Artists>
			<xsl:for-each select="Songs/Song">
				<Artist>
					<xsl:value-of select="@Artist"/>
					<xsl:text> songs in the top 500: </xsl:text>
					<xsl:value-of select="count(key('keySongs',@Artist))"/>
				</Artist>
			</xsl:for-each>
		</Artists>
	</xsl:template>
</xsl:stylesheet>

Challenge Solution:

Keys/Solutions/SongsByArtist-challenge.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:key name="keySongs" match="Song" use="@Artist"/>
	<xsl:template match="/">
		<Artists>
			<xsl:for-each select="Songs/Song">
				<xsl:sort select="count(key('keySongs',@Artist))" 
					order="descending" data-type="number"/>
				<xsl:variable name="Artist" select="@Artist"/>
				<xsl:if test="not(preceding-sibling::Song[@Artist=$Artist])">
					<Artist>
						<xsl:value-of select="@Artist"/>
						<xsl:text> songs in the top 500: </xsl:text>
						<xsl:value-of select="count(key('keySongs',@Artist))"/>
					</Artist>
				</xsl:if>
			</xsl:for-each>
		</Artists>
	</xsl:template>
</xsl:stylesheet>
Next