<xsl:stylesheet 
	version="1.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns:xhtml="http://www.w3.org/1999/xhtml"
>
<!--
	xmlns="http://www.w3.org/1999/xhtml"  
-->
	<!-- 
	 ******************************************************************************************
	 *  Initializaion
	 ******************************************************************************************
	-->
	<xsl:output method="xml"/>
	<xsl:output indent="yes" />		<!-- Make output XML easy to read -->
	<!-- <xsl:text>&#xD;&#xA;</xsl:text> -->
	<!--
		extension-element-prefixes="exsl"
		exclude-result-prefixes="exsl"
	 -->

	<!-- Create a var to contain a lookup XML doc to be used in rewriting <Link> tags -->
	<xsl:variable name="linkRewriteLookupDoc" select="document( 'LinkRewriteLookup.xml' )"/>
	
	<!-- Define the lookup key -->
	<xsl:key name="linkRewriteKey" match="LinkReplacement" use="@contentID"/>
	
	<!-- 
	 ******************************************************************************************
	  template:
		 Load the lookup XML doc
	 ******************************************************************************************
	-->
	<xsl:template match="LinkReplacements">
		<xsl:apply-templates select="$linkRewriteLookupDoc"/>
	</xsl:template>
	
	<!-- 
	 ******************************************************************************************
	  template:
		 By default, recursively copy all nodes
	 ******************************************************************************************
	-->
	<xsl:template match="@* | node()">
		<xsl:copy>
			<xsl:apply-templates select="@* | node()"/>
		</xsl:copy>
	</xsl:template>
	
	<!-- 
	 ******************************************************************************************
	  template:
		 But for Link elements that have a contentID and no URL...
	 ******************************************************************************************
	-->
	<xsl:template match="Link[@contentID and not(@URL)]">
		<xsl:variable name="contentID" select="@contentID"/> <!-- extract contentID -->
		
		<!-- 
			 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			 * IMPLEMENTATION NOTE:
			 *
		     *  MUST define/set variable's value this way to overcome scoping issue:
			 *   - If variable were set inside the for-each loop, its scope would be limited
			 *     to the for-each loop.
			 *   - Defined this way, the scope is for the whole template.
			 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		-->
		<xsl:variable name="url">	<!-- Get URL associated with contentID -->
			<!-- 
				Wrap key( ) lookup in a for-each to cause context to be XML lookup doc
				rather than current doc.
			-->
			<xsl:for-each select="$linkRewriteLookupDoc">	
				<!-- Lookup URL corresponding to contentID -->
				<xsl:value-of select="key( 'linkRewriteKey',  $contentID )"/>
			</xsl:for-each>
		</xsl:variable>
		
		<!-- Generate a <Link> tag with URL attribute replacing  the contentID attribute -->
		<Link URL="{$url}">
			<!-- continue processing children and any other attributes -->
			<xsl:apply-templates select="@*[name()!='contentID'] | node()"/>
		</Link>
	</xsl:template>
	
</xsl:stylesheet>
