<?xml version="1.0" encoding="UTF-8"?>
<!-- 

This Version: V 0.5 (20 January 2003) 
   http://examplotron.org/0/5/compile.xsl

Latest Version: 
   http://examplotron.org/compile.xsl

Latest version for this namespace: 
   http://examplotron.org/0/compile.xsl

Previous Version: V 0.4 (27 March 2001) 
   http://examplotron.org/0/4/compile.xsl

History:

V0.1: creation
V0.2: 
 - Added version and history information.
V0.3:
 - Added support for eg:assert
V0.4:
 - Added support for eg:import and eg:placeHolder
V0.5:
 - Major architectural change: the generated document is no longer
   a XSLT transformation but a Relax NG schema.

Copyright (c) 2001-2003 Eric van der Vlist
                   
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
``Software''), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

The name of the authors when specified in the source files shall be 
kept unmodified.

THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL 4XT.ORG BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<xsl:stylesheet 
	version="1.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns:eg="http://examplotron.org/0/" 
	xmlns = "http://relaxng.org/ns/structure/1.0"
	xmlns:sch ="http://www.ascc.net/xml/schematron"
	exclude-result-prefixes = "eg"
	>
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
	<xsl:strip-space elements="*"/>

	<xsl:template match="/">
		<grammar>
			<start>
				<xsl:apply-templates/>
			</start>
		</grammar>
	</xsl:template>

	<xsl:template match="*" name="eg:element">
		<element name="{local-name()}">
			<xsl:if test="not(namespace-uri()=namespace-uri(parent::*))">
				<xsl:attribute name="ns">
					<xsl:value-of select="namespace-uri()"/>
				</xsl:attribute>
			</xsl:if>
			<xsl:if test="@eg:assert">
				<sch:assert test="{@eg:assert}">
					<xsl:copy-of select="namespace::*"/>
				</sch:assert>
			</xsl:if>
			<xsl:apply-templates select="@*"/>
			<xsl:apply-templates select="*|text()"/>
		</element>
	</xsl:template>
	
	<xsl:template match="*[@eg:occurs='+']">
		<oneOrMore>
			<xsl:call-template name="eg:element"/>
		</oneOrMore>
	</xsl:template>
	
	<xsl:template match="*[@eg:occurs='?']">
		<optional>
			<xsl:call-template name="eg:element"/>
		</optional>
	</xsl:template>
	
	<xsl:template match="*[@eg:occurs='*']">
		<zeroOrMore>
			<xsl:call-template name="eg:element"/>
		</zeroOrMore>
	</xsl:template>
	
	<xsl:template match="@eg:*"/>
	
	<xsl:template match="@*">
		<attribute name="{local-name()}">
			<xsl:if test="namespace-uri()">
				<xsl:attribute name="ns">
					<xsl:value-of select="namespace-uri()"/>
				</xsl:attribute>
			</xsl:if>
			<xsl:text/>
		</attribute>
	</xsl:template>

	<xsl:template match="text()">
		<text/>
	</xsl:template>


</xsl:stylesheet>
