1<?xml version="1.0" encoding="UTF-8"?>
2<!--
3	Stylesheet for extracting Schematron information from a RELAX-NG schema.
4	Based on the stylesheet for extracting Schematron information from W3C XML Schema.
5	Created by Eddie Robertsson 2002/06/01
6	Update for ISO Schematron using XSLT2 Rick Jelliffe 2010/04/14
7-->
8<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
9xmlns:sch="http://purl.oclc.org/dsdl/schematron" xmlns:rng="http://relaxng.org/ns/structure/1.0">
10	<!-- Set the output to be XML with an XML declaration and use indentation -->
11	<xsl:output method="xml" omit-xml-declaration="no" indent="yes" standalone="yes"/>
12	<!-- -->
13	<!-- match schema and call recursive template to extract included schemas -->
14	<!-- -->
15	<xsl:template match="/rng:grammar | /rng:element">
16		<!-- call the schema definition template ... -->
17		<xsl:call-template name="gatherSchema">
18			<!-- ... with current node as the $schemas parameter ... -->
19			<xsl:with-param name="schemas" select="."/>
20			<!-- ... and any includes in the $include parameter -->
21			<xsl:with-param name="includes" select="document(/rng:grammar/rng:include/@href
22| //rng:externalRef/@href)"/>
23		</xsl:call-template>
24	</xsl:template>
25	<!-- -->
26	<!-- gather all included schemas into a single parameter variable -->
27	<!-- -->
28	<xsl:template name="gatherSchema">
29		<xsl:param name="schemas"/>
30		<xsl:param name="includes"/>
31		<xsl:choose>
32			<xsl:when test="count($schemas) &lt; count($schemas | $includes)">
33				<!-- when $includes includes something new, recurse ... -->
34				<xsl:call-template name="gatherSchema">
35					<!-- ... with current $includes added to the $schemas parameter ... -->
36					<xsl:with-param name="schemas" select="$schemas | $includes"/>
37					<!-- ... and any *new* includes in the $include parameter -->
38					<xsl:with-param name="includes" select="document($includes/rng:grammar/rng:include/@href
39| $includes//rng:externalRef/@href)"/>
40				</xsl:call-template>
41			</xsl:when>
42			<xsl:otherwise>
43				<!-- we have the complete set of included schemas, so now let's output the embedded schematron -->
44				<xsl:call-template name="output">
45					<xsl:with-param name="schemas" select="$schemas"/>
46				</xsl:call-template>
47			</xsl:otherwise>
48		</xsl:choose>
49	</xsl:template>
50	<!-- -->
51	<!-- output the schematron information -->
52	<!-- -->
53	<xsl:template name="output">
54		<xsl:param name="schemas"/>
55		<!-- -->
56		<sch:schema  queryBinding="xslt2" >
57			<!-- get header-type elements - eg title and especially ns -->
58			<!-- title (just one) -->
59			<xsl:copy-of select="$schemas//sch:title[1]"/>
60			<!-- get remaining schematron schema children -->
61			<!-- get non-blank namespace elements, dropping duplicates -->
62			<xsl:for-each select="$schemas//sch:ns">
63				<xsl:if test="generate-id(.) = generate-id($schemas//sch:ns[@prefix = current()/@prefix][1])">
64					<xsl:copy-of select="."/>
65				</xsl:if>
66			</xsl:for-each>
67			<xsl:copy-of select="$schemas//sch:phase"/>
68			<xsl:copy-of select="$schemas//sch:pattern"/>
69			<sch:diagnostics>
70				<xsl:copy-of select="$schemas//sch:diagnostics/*"/>
71			</sch:diagnostics>
72		</sch:schema>
73	</xsl:template>
74	<!-- -->
75</xsl:transform>