<xsl:stylesheet version="1.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
	xmlns:xlink="http://www.w3.org/1999/xlink">
	<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
	<!-- =========================================================== 
	This stylesheet is designed to be used on gml.dep (produced from 
	gml.xsd by depends.xslt) to produce a specialized gmlSubset.xsd that 
	contains only the types and elements specified in the "wanted" parameter,
	and the types and elements on which they depend.  Note that the type and
	element items in the "wanted" parameter must include namespace prefixes, 
	and that they must be separated by commas, including a trailing comma after 
	the last item.  
	===========================================================-->
	<xsl:include href="utility.xslt"/>
	<xsl:param name="baseUri" select="document('../base/gml.xsd')"/>
	<!-- sample1 <xsl:param name="wanted">gml:featureProperty,gml:lineStringProperty,gml:polygonProperty,</xsl:param> -->
	<!-- sample2 <xsl:param name="wanted">gml:GeographicCRS,gml:_Coverage,gml:track,</xsl:param> -->
	<!-- sample3 <xsl:param name="wanted">gml:_FeatureCollection,gml:ItemStyleDescriptorType,gml:FeatureConstraintType,</xsl:param> -->
	<xsl:param name="wanted">gml:Point,gml:pointProperty,</xsl:param>
	<xsl:template match="/">
		<xsl:variable name="wantedList">
			<xsl:call-template name="getWantedList">
				<xsl:with-param name="list" select="$wanted"/>
				<xsl:with-param name="from">BEGIN</xsl:with-param>
				<xsl:with-param name="depth">0</xsl:with-param>
			</xsl:call-template>
		</xsl:variable>
		<xsl:variable name="vers"  select="//depends/@version"/>	
		<schema targetNamespace="http://www.opengis.net/gml" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:sch="http://www.ascc.net/xml/schematron" xmlns:gml="http://www.opengis.net/gml" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:smil20="http://www.w3.org/2001/SMIL20/" xmlns:smil20lang="http://www.w3.org/2001/SMIL20/Language"  elementFormDefault="qualified" version="{$vers}">
			<annotation>
				<documentation>GML Subset schema for <xsl:value-of select="$wanted"/>  written by gmlSubset.xslt. </documentation>
			</annotation>
			
			<xsl:if test="contains($wantedList,'xlink:')">
				<import namespace="http://www.w3.org/1999/xlink" schemaLocation="xlinks.xsd"/>
			</xsl:if>
			<xsl:if test="contains($wantedList,'smil20:')">
				<import namespace="http://www.w3.org/2001/SMIL20/" schemaLocation="smil20.xsd"/>
			</xsl:if>
			<xsl:if test="contains($wantedList,'smil20lang:')">
				<import namespace="http://www.w3.org/2001/SMIL20/Language" schemaLocation="smil20-language.xsd"/>
			</xsl:if>
			<xsl:call-template name="writeWantedList">
				<xsl:with-param name="list" select="$wantedList"/>
			</xsl:call-template>
		</schema>
	</xsl:template>
	<!-- =========================================================== -->
	<xsl:template name="getDocName">
		<xsl:param name="wanted"/>
		<xsl:for-each select="//depends/def[@name=$wanted]">
			<xsl:value-of select="@doc"/>
		</xsl:for-each>
	</xsl:template>
	<!-- =========================================================== -->
	<xsl:template name="getUses">
		<xsl:param name="wanted"/>
		<xsl:for-each select="//depends/def[@name=$wanted]">
			<xsl:for-each select="uses">
				<xsl:value-of select="@name"/>
				<xsl:text>,</xsl:text>
			</xsl:for-each>
		</xsl:for-each>
	</xsl:template>
	<!-- =========================================================== -->
	<xsl:template name="writeWanted">
		<xsl:param name="wanted"/>
		<xsl:choose>
			<xsl:when test="contains($wanted,'xlink:') or contains($wanted,'wfs:') or contains($wanted,'smil20')">
				<!-- XLINK <xsl:value-of select="$wanted"/> -->
			</xsl:when>
			<xsl:otherwise>
				<!-- OTHER <xsl:value-of select="$wanted"/> -->
				<xsl:variable name="docName">
					<xsl:call-template name="getDocName">
						<xsl:with-param name="wanted" select="$wanted"/>
					</xsl:call-template>
				</xsl:variable>
				<xsl:variable name="localName">
					<xsl:call-template name="removePrefix">
						<xsl:with-param name="name" select="$wanted"/>
						<xsl:with-param name="pre">:</xsl:with-param>
					</xsl:call-template>
				</xsl:variable>
				<xsl:call-template name="Separator"/>
				<xsl:for-each select="document($docName,$baseUri)">
					<xsl:for-each select="//xsd:schema/*[@name = $localName]">
						<xsl:copy-of select="."/>
					</xsl:for-each>
				</xsl:for-each>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<!-- =========================================================== -->
	<xsl:template name="writeWantedList">
		<xsl:param name="list"/>
		<xsl:if test="$list != ''">
			<xsl:variable name="first" select="substring-before($list, ',')"/>
			<xsl:variable name="rest" select="substring-after($list, ',')"/>
			<xsl:call-template name="writeWanted">
				<xsl:with-param name="wanted" select="$first"/>
			</xsl:call-template>
			<xsl:if test="contains($rest,',')">
				<xsl:call-template name="writeWantedList">
					<xsl:with-param name="list" select="$rest"/>
				</xsl:call-template>
			</xsl:if>
		</xsl:if>
	</xsl:template>
	<!-- =========================================================== -->
	<xsl:template name="getWantedList">
		<xsl:param name="list"/>
		<xsl:param name="seen"/>
		<xsl:param name="from"/>
		<xsl:param name="depth"/>
		<xsl:if test="$list != ''">
			<xsl:variable name="first" select="substring-before($list, ',')"/>
			<xsl:variable name="firstSep" select="concat($first,',')"/>
			<xsl:variable name="rest" select="substring-after($list, ',')"/>
			<xsl:choose>
				<xsl:when test="contains($seen,$firstSep)">
					<xsl:call-template name="getWantedList">
						<xsl:with-param name="list" select="$rest"/>
						<xsl:with-param name="seen" select="$seen"/>
						<xsl:with-param name="from">REST</xsl:with-param>
						<xsl:with-param name="depth" select="$depth + 1"/>
					</xsl:call-template>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$firstSep"/>
					<xsl:variable name="usesList">
						<xsl:call-template name="getUses">
							<xsl:with-param name="wanted" select="$first"/>
						</xsl:call-template>
					</xsl:variable>
					<xsl:variable name="toDo" select="concat($usesList,$rest)"/>
					<xsl:variable name="nowSeen" select="concat($seen,$firstSep)"/>
					<xsl:call-template name="getWantedList">
						<xsl:with-param name="list" select="$toDo"/>
						<xsl:with-param name="seen" select="$nowSeen"/>
						<xsl:with-param name="from">USES</xsl:with-param>
						<xsl:with-param name="depth" select="$depth + 1"/>
					</xsl:call-template>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:if>
	</xsl:template>
	<!-- =========================================================== -->
</xsl:stylesheet>
