<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.esd.org.uk/standards" xmlns:esd="http://www.esd.org.uk/standards" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<xsl:include href="metadata.xslt"/>
	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
	<xsl:namespace-alias stylesheet-prefix="esd" result-prefix="#default"/>
	<xsl:template match="esd:Domains">
		<xsl:variable name="Lgcl">
			<xsl:apply-templates select="esd:Domain[@Key = 'Lgcl']"/>
		</xsl:variable>
		<xsl:variable name="LgclGclMapping">
			<xsl:apply-templates select="esd:Domain[@Key = 'LgclGclMapping']"/>
		</xsl:variable>
		<xsl:variable name="LgclLgslMapping">
			<xsl:apply-templates select="esd:Domain[@Key = 'LgclLgslMapping']"/>
		</xsl:variable>
		<xsl:apply-templates select="document($Lgcl)/esd:ControlledList">
			<xsl:with-param name="Lgcl" select="$Lgcl"/>
			<xsl:with-param name="LgclGclMapping" select="$LgclGclMapping"/>
			<xsl:with-param name="LgclLgslMapping" select="$LgclLgslMapping"/>
			<xsl:with-param name="HomeLocation">http://www.esd.org.uk/standards/lgcl/1.03/lgclwithmapping.xml</xsl:with-param>
			<xsl:with-param name="LatestVersionLocation">http://www.esd.org.uk/standards/lgcl/lgclwithmapping.xml</xsl:with-param>
		</xsl:apply-templates>
	</xsl:template>
	<xsl:template match="esd:Domain">
		<xsl:variable name="CurrentVersion" select="esd:Versions/@Current"/>
		<xsl:value-of select="esd:Versions/esd:Release[@Number = $CurrentVersion]/@Resource"/>
	</xsl:template>
	<xsl:template match="esd:ControlledList">
		<xsl:param name="Lgcl"/>
		<xsl:param name="LgclGclMapping"/>
		<xsl:param name="LgclLgslMapping"/>
		<xsl:param name="HomeLocation"/>
		<xsl:param name="LatestVersionLocation"/>
		<xsl:param name="Date.Modified"/>
		<xsl:element name="ControlledList">
			<xsl:attribute name="xsi:schemaLocation">http://www.esd.org.uk/standards&#32;http://www.esd.org.uk/standards/xmlschemas/lgclwithmapping20040317.xsd</xsl:attribute>
			<xsl:attribute name="Version"><xsl:value-of select="@Version"/></xsl:attribute>
			<xsl:attribute name="LatestVersionLocation"><xsl:value-of select="$LatestVersionLocation"/></xsl:attribute>
			<xsl:attribute name="HomeLocation"><xsl:value-of select="$HomeLocation"/></xsl:attribute>
			<xsl:attribute name="VersionDate"><xsl:value-of select="@VersionDate"/></xsl:attribute>
			<xsl:attribute name="ListName"><xsl:value-of select="@ListName"/></xsl:attribute>
			<xsl:attribute name="ItemName"><xsl:value-of select="@ItemName"/></xsl:attribute>
			<xsl:apply-templates select="document($Lgcl)//esd:Metadata">
				<xsl:with-param name="Relation.IsReplacedBy" select="$LatestVersionLocation"/>
				<xsl:with-param name="Source" select="$HomeLocation"/>
				<xsl:with-param name="Date.Modified" select="$Date.Modified"/>
			</xsl:apply-templates>
			<xsl:apply-templates select="esd:Item">
				<xsl:with-param name="LgclGclMapping" select="$LgclGclMapping"/>
				<xsl:with-param name="LgclLgslMapping" select="$LgclLgslMapping"/>
			</xsl:apply-templates>
		</xsl:element>
	</xsl:template>
	<xsl:template match="esd:Item">
		<xsl:param name="LgclGclMapping"/>
		<xsl:param name="LgclLgslMapping"/>
		<xsl:element name="Item">
			<xsl:attribute name="Id"><xsl:value-of select="@Id"/></xsl:attribute>
			<xsl:if test="@ConceptId != ''">
				<xsl:attribute name="ConceptId"><xsl:value-of select="@ConceptId"/></xsl:attribute>
			</xsl:if>
			<xsl:if test="@Obsolete != ''">
				<xsl:attribute name="Obsolete"><xsl:value-of select="@Obsolete"/></xsl:attribute>
			</xsl:if>
			<xsl:attribute name="AddedInVersion"><xsl:value-of select="@AddedInVersion"/></xsl:attribute>
			<xsl:attribute name="LastUpdatedInVersion"><xsl:value-of select="@LastUpdatedInVersion"/></xsl:attribute>
			<xsl:if test="@AToZ != ''">
				<xsl:attribute name="AToZ"><xsl:value-of select="@AToZ"/></xsl:attribute>
			</xsl:if>
			<xsl:if test="@Category != ''">
				<xsl:attribute name="Category"><xsl:value-of select="@Category"/></xsl:attribute>
			</xsl:if>
			<xsl:if test="@Preferred != ''">
				<xsl:attribute name="Preferred"><xsl:value-of select="@Preferred"/></xsl:attribute>
			</xsl:if>
			<xsl:copy-of select="*"/>
			<xsl:variable name="CurrentId" select="@Id"/>
			<xsl:apply-templates select="document($LgclGclMapping)/esd:ItemMappings/esd:ItemMapping[esd:From/esd:Item/@Id = $CurrentId]" mode="Gcl"/>
			<xsl:apply-templates select="document($LgclLgslMapping)/esd:ItemMappings/esd:ItemMapping[esd:From/esd:Item/@Id = $CurrentId]" mode="Lgsl"/>
		</xsl:element>
	</xsl:template>
	<xsl:template match="esd:ItemMapping" mode="Gcl">
		<xsl:for-each select="esd:To/esd:Item">
			<xsl:element name="GclItem">
				<xsl:attribute name="Id"><xsl:value-of select="@Id"/></xsl:attribute>
				<xsl:if test="@ConceptId != ''">
					<xsl:attribute name="ConceptId"><xsl:value-of select="@ConceptId"/></xsl:attribute>
				</xsl:if>
				<xsl:value-of select="."/>
			</xsl:element>
		</xsl:for-each>
	</xsl:template>
	<xsl:template match="esd:ItemMapping" mode="Lgsl">
		<xsl:for-each select="esd:To/esd:Item">
			<xsl:element name="LgslItem">
				<xsl:attribute name="Id"><xsl:value-of select="@Id"/></xsl:attribute>
				<xsl:if test="@ConceptId != ''">
					<xsl:attribute name="ConceptId"><xsl:value-of select="@ConceptId"/></xsl:attribute>
				</xsl:if>
				<xsl:value-of select="."/>
			</xsl:element>
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>
