<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:wr="LondonUnderground">
	<xsl:output method="html" indent="yes"/>
	<!-- ***********************************************************************************
	William Reilly (wreilly@digitas.com)
	CSCIE-259
	Assignment 2
	Part 3
	Maneuvering the London Underground

	underground.xsl

	March 14, 2002
	David Malan's Monday 6:30 P.M. Section
	*********************************************************************************** -->
	<xsl:template match="/">
		<html>
			<body>
				<a name="top"/>
				<h1>
					<font face="arial">London Underground Info</font>
				</h1>
				<table>
					<tr>
						<td>
							<a href="http://www.thetube.com/content/tubemap/images/tm_quad_2h_0309.gif" target="_blank">
								<img src="images/tubelogo1.gif" alt="London Tube (logo)" border="0"/>
							</a>
						</td>
						<td>
							<pre>
	William Reilly (<a href="mailto:wreilly@digitas.com">wreilly@digitas.com</a>)
	CSCIE-259, <a href="http://www.extension.harvard.edu/" target="_blank" title="Harvard Extension School website">Harvard Extension</a>
	Assignment 2
	Part 3
	Maneuvering the London Underground

	March 14, 2002
	David Malan's Monday 6:30 P.M. Section
</pre>
						</td>
					</tr>
				</table>
				<!-- *******************************************************************************
				PART THREE (3)
				****************************************************************************** -->
				<h2>
					<font color="red" face="arial">Part 3. "Maneuvering the London Underground"</font>
				</h2>
				<h3>
					<font face="arial">Summary:</font>
				</h3>
				<div>
					<font face="arial">Number of Lines: <xsl:value-of select="count(/INFO/LINE)"/>
					</font>
				</div>
				<div>
					<font face="arial">Number of Stations (unique): <xsl:value-of select="count(/INFO/STATION-INFO/STATION)"/>
					</font>
				</div>
				<div>
					<!-- Adding up _all_ minutes in 'CONNECT/@length' (for both directions) works fine so long as you simply then divide by two (2),
						to get the time required for _one_ (1) direction.  !  -->
					<font face="arial">Total end-to-end travel time on the Victoria line: <xsl:value-of select="sum(/INFO/LINE[@name='victoria']/STATION/CONNECT/@length) div 2"/>
					</font>
				</div>
				<!-- ===  COLOURS TABLE  ===  (TOP of Page)  ========================= -->
				<!-- This was simply coded by hand, but given more time could be derived from the data.
					See also the "Lookup Table" further below of LINES and COLORS
				-->
				<font face="arial">
					<table border="1">
						<tr>
							<td colspan="3" align="center">
								<b>"Lines" in Document Order</b>
							</td>
						</tr>
						<tr valign="top">
							<td bgcolor="#0099FF">1. <b>
									<a href="#victoria">Victoria</a>
								</b> - SkyBlue - #0099FF</td>
							<td bgcolor="#800080">2. <b>
									<a href="#piccadilly">Piccadilly</a>
								</b> - Thistle - #800080</td>
							<td bgcolor="#C0C0C0">3. <b>
									<a href="#jubilee">Jubilee</a>
								</b> - Silver - #C0C0C0</td>
						</tr>
						<tr valign="top">
							<td bgcolor="#808000">4. <b>
									<a href="#bakerloo">Bakerloo</a>
								</b> - Burlywood - #808000</td>
							<td bgcolor="#008000">5. <b>
									<a href="#northern">Northern</a>
								</b> - DarkSeaGreen - #008000</td>
							<td bgcolor="#FF00FF">6. <b>
									<a href="#central">Central</a>
								</b> - Pink - #FF00FF</td>
						</tr>
						<tr valign="top">
							<td bgcolor="#CC9900">7. <b>
									<a href="#circle">Circle</a>
								</b> - Khaki - #CC9900</td>
							<td bgcolor="#FF6666">8. <b>
									<a href="#metropolitan">Metropolitan</a>
								</b> - PeachPuff - #FF6666</td>
							<td bgcolor="#CC0099">9. <b>
									<a href="#district">District</a>
								</b> - Lavender - #CC0099</td>
						</tr>
						<tr valign="top">
							<td bgcolor="#00FFFF">10. <b>
									<a href="#district_olympia_branch">District Olympia Branch</a>
								</b> - CornflowerBlue - #00FFFF</td>
							<td bgcolor="#FF6600">11. <b>
									<a href="#east_london_line">East London Line</a>
								</b> - DarkOrange - #FF6600</td>
							<td bgcolor="#FFCC99">12. <b>
									<a href="#hammersmith_and_city">Hammersmith And City</a>
								</b> - Salmon - #FFCC99</td>
						</tr>
						<tr valign="top">
							<td bgcolor="#CC00FF">13. <b>
									<a href="#docklands_light_railway">Docklands Light Railway</a>
								</b> - Violet - #CC00FF</td>
							<td bgcolor="#0000FF">14. <b>
									<a href="#docklands_light_railway_beckton_branch">Docklands Light Railway Beckton Branch</a>
								</b> - SteelBlue - #0000FF</td>
							<td bgcolor="#CC9900">15. <b>
									<a href="#waterloo_and_city">Waterloo And City</a>
								</b> - Wheat - #CC9900</td>
						</tr>
						<tr valign="top">
							<td bgcolor="#996633">16. <b>
									<a href="#br_great_northern_electrics">Br Great Northern Electrics</a>
								</b> - SandyBrown - #996633</td>
							<td bgcolor="#00FF00">17. <b>
									<a href="#br_thameslink">Br Thameslink</a>
								</b> - LightSeaGreen - #00FF00</td>
							<td bgcolor="#99CCFF">18. <b>
									<a href="#br_harlequin_line">Br Harlequin Line</a>
								</b> - PowderBlue - #99CCFF</td>
						</tr>
						<tr valign="top">
							<td bgcolor="#CCFFFF">19. <b>
									<a href="#br_north_london_line">Br North London Line</a>
								</b> - LightBlue - #CCFFFF</td>
							<td colspan="2" valign="center">
								<font face="arial" size="2">[cscie259-discuss] "Colours that you can use for your London Tube" (Sent: Sun 3/10/2002 12:15 PM)</font>
							</td>
						</tr>
					</table>
				</font>
				<!-- ===  /COLOURS TABLE  ===  (TOP of Page)  ========================= -->

				<h3>
					<font face="arial">Details:</font>
				</h3>

				<xsl:for-each select="INFO/LINE">
					<a name="{@name}"/>
					<h1>
						<font face="arial">
							<xsl:choose>
								<!-- A few (three) Line names end with the word "line" in them, so we test first before appending the hard-coded "line" below to the other Line names -->
								<xsl:when test="substring(@name, string-length(@name) - 3, 4)='line'">
									<xsl:value-of select="translate(@name, '_', ' ')"/>
								</xsl:when>
								<xsl:otherwise>
									<xsl:value-of select="translate(@name, '_', ' ')"/> line
								</xsl:otherwise>
							</xsl:choose>
						</font>
					</h1>
					<div>
						<!-- This was simply coded by hand, but with more time this too could be coordinated with the "COLOURS TABLE" (above)
							and the "Lookup Table" (further below).
						-->
						<xsl:variable name="bgcolor">
							<xsl:choose>
								<xsl:when test="@name='victoria'">#0099FF</xsl:when>
								<xsl:when test="@name='piccadilly'">#800080</xsl:when>
								<xsl:when test="@name='jubilee'">#C0C0C0</xsl:when>
								<xsl:when test="@name='bakerloo'">#808000</xsl:when>
								<xsl:when test="@name='northern'">#008000</xsl:when>
								<xsl:when test="@name='central'">#FF00FF</xsl:when>
								<xsl:when test="@name='circle'">#CC9900</xsl:when>
								<xsl:when test="@name='metropolitan'">#FF6666</xsl:when>
								<xsl:when test="@name='district'">#CC0099</xsl:when>
								<xsl:when test="@name='district_olympia_branch'">#00FFFF</xsl:when>
								<xsl:when test="@name='east_london_line'">#FF6600</xsl:when>
								<xsl:when test="@name='hammersmith_and_city'">#FFCC99</xsl:when>
								<xsl:when test="@name='docklands_light_railway'">#CC00FF</xsl:when>
								<xsl:when test="@name='docklands_light_railway_beckton_branch'">#0000FF</xsl:when>
								<xsl:when test="@name='waterloo_and_city'">#CC9900</xsl:when>
								<xsl:when test="@name='br_great_northern_electrics'">#996633</xsl:when>
								<xsl:when test="@name='br_thameslink'">#00FF00</xsl:when>
								<xsl:when test="@name='br_harlequin_line'">#99CCFF</xsl:when>
								<xsl:when test="@name='br_north_london_line'">#CCFFFF</xsl:when>
								<xsl:otherwise>#FFFFFF</xsl:otherwise>
							</xsl:choose>
						</xsl:variable>
						<table border="1">
							<xsl:for-each select="STATION">
								<xsl:choose>
									<!-- Every third station, begin two contiguous rows.  These two rows will each contain information on the next three stations. -->
									<xsl:when test="position() mod 3 = 1">
										<!-- This 1st row (of 2) has STATION names. -->
										<!-- LINE color set for row -->
										<tr valign="top" align="center" bgcolor="{$bgcolor}">
											<xsl:call-template name="station3-row1"/>
										</tr>
										<!-- This 2nd row (of 2) has hypertext links (in LINE color!) to the LINES we link to. -->
										<!-- White bgcolor for 2nd row; text will be in color of LINE linking to -->
										<tr valign="top" bgcolor="#FFFFFF">
											<xsl:call-template name="station3-row2"/>
										</tr>
									</xsl:when>
								</xsl:choose>
							</xsl:for-each>
							<!-- STATION -->
						</table>
						<font face="arial" size="2">
							<a href="#top">Back to top</a>
						</font>
					</div>
				</xsl:for-each>
				<!-- LINE -->

				<!-- ===  COLOURS TABLE  ===  (BOTTOM of Page (repeated))  ========================= -->
				<font face="arial">
					<table border="1">
						<tr>
							<td colspan="3" align="center">
								<b>"Lines" in Document Order</b>
							</td>
						</tr>
						<tr valign="top">
							<td bgcolor="#0099FF">1. <b>
									<a href="#victoria">Victoria</a>
								</b> - SkyBlue - #0099FF</td>
							<td bgcolor="#800080">2. <b>
									<a href="#piccadilly">Piccadilly</a>
								</b> - Thistle - #800080</td>
							<td bgcolor="#C0C0C0">3. <b>
									<a href="#jubilee">Jubilee</a>
								</b> - Silver - #C0C0C0</td>
						</tr>
						<tr valign="top">
							<td bgcolor="#808000">4. <b>
									<a href="#bakerloo">Bakerloo</a>
								</b> - Burlywood - #808000</td>
							<td bgcolor="#008000">5. <b>
									<a href="#northern">Northern</a>
								</b> - DarkSeaGreen - #008000</td>
							<td bgcolor="#FF00FF">6. <b>
									<a href="#central">Central</a>
								</b> - Pink - #FF00FF</td>
						</tr>
						<tr valign="top">
							<td bgcolor="#CC9900">7. <b>
									<a href="#circle">Circle</a>
								</b> - Khaki - #CC9900</td>
							<td bgcolor="#FF6666">8. <b>
									<a href="#metropolitan">Metropolitan</a>
								</b> - PeachPuff - #FF6666</td>
							<td bgcolor="#CC0099">9. <b>
									<a href="#district">District</a>
								</b> - Lavender - #CC0099</td>
						</tr>
						<tr valign="top">
							<td bgcolor="#00FFFF">10. <b>
									<a href="#district_olympia_branch">District Olympia Branch</a>
								</b> - CornflowerBlue - #00FFFF</td>
							<td bgcolor="#FF6600">11. <b>
									<a href="#east_london_line">East London Line</a>
								</b> - DarkOrange - #FF6600</td>
							<td bgcolor="#FFCC99">12. <b>
									<a href="#hammersmith_and_city">Hammersmith And City</a>
								</b> - Salmon - #FFCC99</td>
						</tr>
						<tr valign="top">
							<td bgcolor="#CC00FF">13. <b>
									<a href="#docklands_light_railway">Docklands Light Railway</a>
								</b> - Violet - #CC00FF</td>
							<td bgcolor="#0000FF">14. <b>
									<a href="#docklands_light_railway_beckton_branch">Docklands Light Railway Beckton Branch</a>
								</b> - SteelBlue - #0000FF</td>
							<td bgcolor="#CC9900">15. <b>
									<a href="#waterloo_and_city">Waterloo And City</a>
								</b> - Wheat - #CC9900</td>
						</tr>
						<tr valign="top">
							<td bgcolor="#996633">16. <b>
									<a href="#br_great_northern_electrics">Br Great Northern Electrics</a>
								</b> - SandyBrown - #996633</td>
							<td bgcolor="#00FF00">17. <b>
									<a href="#br_thameslink">Br Thameslink</a>
								</b> - LightSeaGreen - #00FF00</td>
							<td bgcolor="#99CCFF">18. <b>
									<a href="#br_harlequin_line">Br Harlequin Line</a>
								</b> - PowderBlue - #99CCFF</td>
						</tr>
						<tr valign="top">
							<td bgcolor="#CCFFFF">19. <b>
									<a href="#br_north_london_line">Br North London Line</a>
								</b> - LightBlue - #CCFFFF</td>
							<td colspan="2" valign="center">
								<font face="arial" size="2">[cscie259-discuss] "Colours that you can use for your London Tube" (Sent: Sun 3/10/2002 12:15 PM)</font>
							</td>
						</tr>
					</table>
				</font>
				<!-- ===  /COLOURS TABLE  ===  (BOTTOM of Page (repeated))  ========================= -->
			</body>
		</html>
	</xsl:template>

	
	<!-- "Lookup table method" transformation.  See O'Reilly book 'XSLT'  , page 356. -->
	<!-- Given the LINE/@name value, we come here to determine the Hex color number to use in creating the text used next to a link: "go" -->
	<wr:line-colors>
		<wr:line-color line="victoria">#0099FF</wr:line-color>
		<wr:line-color line="piccadilly">#800080</wr:line-color>
		<wr:line-color line="jubilee">#C0C0C0</wr:line-color>
		<wr:line-color line="bakerloo">#808000</wr:line-color>
		<wr:line-color line="northern">#008000</wr:line-color>
		<wr:line-color line="central">#FF00FF</wr:line-color>
		<wr:line-color line="circle">#CC9900</wr:line-color>
		<wr:line-color line="metropolitan">#FF6666</wr:line-color>
		<wr:line-color line="district">#CC0099</wr:line-color>
		<wr:line-color line="district_olympia_branch">#00FFFF</wr:line-color>
		<wr:line-color line="east_london_line">#FF6600</wr:line-color>
		<wr:line-color line="hammersmith_and_city">#FFCC99</wr:line-color>
		<wr:line-color line="docklands_light_railway">#CC00FF</wr:line-color>
		<wr:line-color line="docklands_light_railway_beckton_branch">#0000FF</wr:line-color>
		<wr:line-color line="waterloo_and_city">#CC9900</wr:line-color>
		<wr:line-color line="br_great_northern_electrics">#996633</wr:line-color>
		<wr:line-color line="br_thameslink">#00FF00</wr:line-color>
		<wr:line-color line="br_harlequin_line">#99CCFF</wr:line-color>
		<wr:line-color line="br_north_london_line">#CCFFFF</wr:line-color>
	</wr:line-colors>
	


	<!-- +++++++++++++++++++++  NAMED TEMPLATE station3-row1  ++++++++++++++++++++++++++ -->

	<xsl:template name="station3-row1">
		<td>
			<!-- Context node is currently a STATION node.  It is a 'mod 3 = 1' node from the nodeset of STATION nodes for a certain LINE. -->
			<a name="{concat(parent::LINE/@name, '-', @name)}"/>
			<!-- Note: No need to test if there's data for the first column == there will be (we're here because mod 3 = 1) -->
			<font size="2" face="arial">
				<xsl:value-of select="translate(parent::LINE/@name, '_', ' ')"/> station # <xsl:value-of select="position()"/>
			</font>
			<br/>
			<font size="4" face="arial">
				<strong>
					<xsl:value-of select="translate(@name, '_', ' ')"/>
				</strong>
			</font>
		</td>
		<td>
			<!-- Get the next STATION in that nodeset. -->
			<a name="{concat(parent::LINE/@name, '-', following-sibling::STATION[position()=1]/@name)}"/>
			<!-- Test if there's data for this second column, or if not we're at the end of the STATIONS for this LINE -->
			<xsl:if test="following-sibling::STATION[position()=1]">
				<font size="2" face="arial">
					<xsl:value-of select="translate(parent::LINE/@name, '_', ' ')"/> station # <xsl:value-of select="position() + 1"/>
				</font>
				<br/>
				<font size="4" face="arial">
					<strong>
						<xsl:value-of select="translate(following-sibling::STATION[position()=1]/@name, '_', ' ')"/>
					</strong>
				</font>
			</xsl:if>
		</td>
		<td>
			<!-- Get the next STATION after that.  Three total for our ROW.-->
			<a name="{concat(parent::LINE/@name, '-', following-sibling::STATION[position()=2]/@name)}"/>
			<!-- Test if there's data for this third column, or if not we're at the end of the STATIONS for this LINE -->
			<xsl:if test="following-sibling::STATION[position()=2]">
				<font size="2" face="arial">
					<xsl:value-of select="translate(parent::LINE/@name, '_', ' ')"/> station # <xsl:value-of select="position() + 2"/>
				</font>
				<br/>
				<font size="4" face="arial">
					<strong>
						<xsl:value-of select="translate(following-sibling::STATION[position()=2]/@name, '_', ' ')"/>
					</strong>
				</font>
			</xsl:if>
		</td>
	</xsl:template>
	<!-- +++++++++++++++++++++  /NAMED TEMPLATE station3-row1  ++++++++++++++++++++++++++ -->



	<!-- +++++++++++++++++++++  NAMED TEMPLATE station3-row2  ++++++++++++++++++++++++++ -->

	<xsl:template name="station3-row2">
		<td width="230">
			<xsl:choose>
				<!-- Note: No need to test if there's data for the first column == there will be (we're here because mod 3 = 1) -->
				<xsl:when test="CHANGE">
					<font face="arial" size="1">CHANGES</font>
					<ul>
						<xsl:for-each select="CHANGE">
							<li>
								<xsl:call-template name="changes-list"/>
							</li>
						</xsl:for-each>
					</ul>
				</xsl:when>
				<xsl:otherwise>
					<font face="arial" size="1">[No Changes]</font>
				</xsl:otherwise>
			</xsl:choose>
		</td>
		<td width="230">
			<xsl:if test="following-sibling::STATION[position()=1]">
				<xsl:choose>
				<!-- Test if there's data for this second column, or if not we're at the end of the STATIONS for this LINE -->
					<xsl:when test="following-sibling::STATION[position()=1]/CHANGE">
						<font face="arial" size="1">CHANGES</font>
						<ul>
							<xsl:for-each select="following-sibling::STATION[position()=1]/CHANGE">
								<li>
									<xsl:call-template name="changes-list"/>
								</li>
							</xsl:for-each>
						</ul>
					</xsl:when>
					<xsl:otherwise>
						<font face="arial" size="1">[No Changes]</font>
					</xsl:otherwise>
				</xsl:choose>
				<!-- CHANGE -->
			</xsl:if>
		</td>
		<td width="230">
			<xsl:if test="following-sibling::STATION[position()=2]">
				<xsl:choose>
				<!-- Test if there's data for this third column, or if not we're at the end of the STATIONS for this LINE -->
					<xsl:when test="following-sibling::STATION[position()=2]/CHANGE">
						<font face="arial" size="1">CHANGES</font>
						<ul>
							<xsl:for-each select="following-sibling::STATION[position()=2]/CHANGE">
								<li>
									<xsl:call-template name="changes-list"/>
								</li>
							</xsl:for-each>
						</ul>
					</xsl:when>
					<xsl:otherwise>
						<font face="arial" size="1">[No Changes]</font>
					</xsl:otherwise>
				</xsl:choose>
				<!-- CHANGE -->
			</xsl:if>
		</td>
	</xsl:template>
	<!-- +++++++++++++++++++++  /NAMED TEMPLATE station3-row2  ++++++++++++++++++++++++++ -->



	<!-- +++++++++++++  CHANGES-LIST Named Template  +++++++++++++++++++ -->

	<xsl:template name="changes-list">
		<font face="arial">
			<xsl:attribute name="size">2</xsl:attribute>
			<xsl:attribute name="color"><!-- "Lookup table method" transformation.  See O'Reilly book 'XSLT'  , page 356. --><xsl:value-of select="document('')/*/wr:line-colors/wr:line-color[@line=current()/@target]"/></xsl:attribute>
			<xsl:value-of select="translate(@target, '_', ' ')"/>
			<xsl:choose>
				<!-- A few (?) CHANGE elements have a 'targetStation' attribute.  The internal link a href needs to be steered there
				vs. the usual treatment of finding the "target" STATION by simply using the name of the current STATION.
				
				Discussion.
				That of course is entirely intuitive: if I'm at Stockwell STATION on the Victoria LINE and am told there's a CHANGE
				to the Northern LINE, I expect that I make it at the same STATION: namely Stockwell.  Most work this way. 
				For those that instead have a 'targetStation' attribute, we check and correct the link. 
			-->
				<xsl:when test="@targetStation">
					<!-- exception cases -->
					<a href="#{concat(@target, '-', @targetStation)}" title="{translate(@target, '_', ' ')}">
						<em>
							go
						</em>
					</a>
				</xsl:when>
				<xsl:otherwise>
					<!-- the norm -->
					<a href="#{concat(@target, '-', parent::STATION/@name)}" title="{translate(@target, '_', ' ')}">
						<em>
							go
						</em>
					</a>
				</xsl:otherwise>
			</xsl:choose>
		</font>
	</xsl:template>
	<!-- +++++++++++++  /CHANGES-LIST Named Template  +++++++++++++++++++ -->

</xsl:stylesheet>
