<?xml version="1.0" encoding="Shift_JIS"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:rng="http://relaxng.org/ns/structure/1.0"
	xmlns:ht="http://www.w3.org/2002/06/xhtml2"
	xmlns="http://relaxng.org/ns/structure/1.0"
	exclude-result-prefixes="rng"
>

<xsl:output
	method="xml"
	indent="yes"
	encoding="UTF-8"
/>

<xsl:template match="@*|text()|comment()|processing-instruction()">
	<xsl:copy />
</xsl:template>

<xsl:template match="@href" />

<xsl:template match="rng:*">
	<xsl:element name="{local-name()}">
		<xsl:apply-templates select="@*" />
		<xsl:apply-templates />
	</xsl:element>
</xsl:template>

<xsl:template match="rng:grammar">
	<grammar xmlns:ht="http://www.w3.org/2002/06/xhtml2">
		<xsl:apply-templates select="@*" />
		<xsl:apply-templates />
	</grammar>
</xsl:template>


<xsl:template match="rng:include">

	<xsl:variable name="grammar.element" select="
		document(string(@href), /)/child::rng:grammar
	" />
	<xsl:variable name="components.nodeset" select="
		descendant::rng:*
		[not(self::rng:div)]
		[boolean(parent::rng:div) or boolean(parent::rng:include)]
	" />
	<div>
		<xsl:apply-templates select="@*" />
		<ht:h>From <xsl:value-of select="string(@href)" /></ht:h>
		<div>
			<xsl:apply-templates select="$grammar.element/@*" />
			<xsl:apply-templates select="child::node()" />

			<xsl:choose>

				<xsl:when test="not($components.nodeset)">
					<xsl:apply-templates select="$grammar.element/child::node()" />
				</xsl:when>

				<xsl:otherwise>
					<xsl:apply-templates select="$grammar.element/child::node()" mode="exclusive">
						<xsl:with-param name="components.nodeset" select="$components.nodeset" />
					</xsl:apply-templates>
				</xsl:otherwise>

			</xsl:choose>

		</div>
	</div>
</xsl:template>

<xsl:template match="rng:start" mode="exclusive">
	<xsl:param name="components.nodeset" />
	<xsl:choose>
		<xsl:when test="
			not(
				$components.nodeset/descendant-or-self::rng:start
			)
		">
			<xsl:apply-templates select="self::*" />
		</xsl:when>
		<xsl:otherwise />
	</xsl:choose>
</xsl:template>

<xsl:template match="rng:define" mode="exclusive">
	<xsl:param name="components.nodeset" />
	<xsl:choose>
		<xsl:when test="
			not(
				$components.nodeset/descendant-or-self::rng:define
				[string(@name) = string(current()/@name)]
			)
		">
			<xsl:apply-templates select="self::*" />
		</xsl:when>
		<xsl:otherwise />
	</xsl:choose>
</xsl:template>

<xsl:template match="node()" mode="exclusive">
	<xsl:apply-templates select="self::node()" />
</xsl:template>

<xsl:template match="rng:externalRef">
	<div>
		<xsl:apply-templates select="self::rng:externalRef/@*" />
		<xsl:apply-templates select="document(string(@href), /)/child::node()" />
		<xsl:apply-templates select="child::*" /> <!-- <other />* -->
	</div>
</xsl:template>


</xsl:stylesheet>

