<?xml version="1.0"?>

<!-- 
	INCLUDE  
	Macro substitutions the xs:include elements and xs:import elements
	This XSLT script is part of a larger package XSD2SCH. 
	Version: 2006-10-12
	Release Type: Beta 
	Developers: Rick Jelliffe, Cheney Xin, Rahul Grewal
	Updates: Please check for updated versions, e.g. at www.schematron.com
	
-->

<!--
	This code copyright 2007 
		Allette Systems Pty. Ltd. (www.allette.com.au), 
		Topologi Pty. Ltd. (www.topologi.com), 
		JSTOR (http://www.jstor.org/)
		and Rick Jelliffe. 
	
	The code was written under sponsorhip of JSTOR The Scholarly Journal Archive
	 
	This code is available under the GPL (v3. http://www.gnu.org/copyleft/gpl.html)
	
 -->
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="no"/>

<xsl:template match="/">
	<schemas>	
		<xs:schema>
			<xsl:copy-of select="xs:schema/@* | xs:schema/namespace::node()" />
			<xsl:apply-templates select="xs:schema/*" />
		</xs:schema>
	</schemas>
</xsl:template>

<!-- When we find an import statement, just copy the whole schema in at that location.-->
<!-- If there are multiple imports, this is taken care of by the FLATTEN stage,
	however we do catch multiple imports in the same schema document, a minor case.
-->
<xsl:template match="xs:import[@schemaLocation]
	[not(preceding-sibling::xs:import/@schemaLocation=current()/@schemaLocation)]">
	<xsl:comment>Import Schema: <xsl:value-of select="@schemaLocation"/></xsl:comment>
	<xsl:variable name="documentSchema" select="document(@schemaLocation)"/>
	<!-- put schemaLocation as an attribute for imported schema is for flatten using -->
	<xs:schema  schemaLocation="{@schemaLocation}">
		<xsl:copy-of select="$documentSchema/xs:schema/@* | $documentSchema/xs:schema/namespace::node()"/>
		<xsl:apply-templates select="$documentSchema/xs:schema/*" />
	</xs:schema>
</xsl:template>

<!-- When we find an include statement, copy the contents of that schema in at the current
	location. -->	
<!-- There can be multiple includes in each schema. However, if there are multiple includes
	in the same schema this is useless. However we do catch multiple imports in the same 
	schema document, a minor case.
-->	
<!-- Note: An included namespace must have the same target namespace as the parent.
	If the included schema has no namespace, it takes on the namespace of the parent -->	
<xsl:template match="xs:include[@schemaLocation]
	 [not(preceding-sibling::xs:include/@schemaLocation=current()/@schemaLocation)]">
	<xsl:comment>Include Schema: <xsl:value-of select="@schemaLocation"/></xsl:comment>
	<xsl:variable name="documentSchema" select="document(@schemaLocation)"/>
	<xsl:apply-templates select="$documentSchema/xs:schema/*"/>
</xsl:template>
				
<xsl:template match="*[not(self::xs:import or self::xs:include or self::xs:schema)]">
	<xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet>


