<?xml version="1.0" encoding="UTF-8"?>
<?xar XSLT?>
<!-- 
     OVERVIEW : iso_dsdl_include.xsl
     
	    This is an inclusion preprocessor for the non-smart text inclusions
	    of ISO DSDL. It handles 
	    	<relax:extRef> for ISO RELAX NG
	    	<sch:include>  for ISO Schematron and Schematron 1.n
	    	<xi:xinclude>  simple W3C XIncludes for ISO NVRL and DSRL 
	    	<crdl:ref>     for draft ISO CRDL
	    	<dtll:include> for draft ISO DTLL
	    	<* @xlink:href> for simple W3C XLink 1.1 embedded links
	    	
		 
		This should be the first in any chain of processing. It only requires
		XSLT 1. Each kind of inclusion can be turned off (or on) on the command line.
		
		Ids in fragment identifiers or xpointers will be sought in the following
		order:
		    * @xml:id
		    * id() for typed schemas (e.g. from DTD) [NOTE: XInclude does not support this]
		    * untyped @id 
		    
		Limitations:
			* No rebasing: relative paths will be interpreted based on the initial document's
			path, not the including document. (Severe limitation!)
			* No checking for circular references
			* Not full xpointers: only ID matching
			* <relax:include> not implemented 
			* XInclude handling of xml:base and xml:lang not implemented   
-->
<!-- 
  VERSION INFORMATION
  2008-08-20
  	* Fix bug: in XSLT1 cannot do $document/id('x') but need to use for-each
  	
  2008-08-04
  	* Add support for inclusions in old namespace  
  	
  2008-08-03
  	* Fix wrong param name include-relaxng & include-crdl (KH, PH)
  	* Allow inclusion of XSLT and XHTML (KH)
  	* Fix inclusion of fragments (KH)
  	
  2008-07-25
  	* Add selectable input parameter
  
  2008-07-24  
	* RJ New
-->
<!--
 LEGAL INFORMATION
 
 Copyright (c) 2008 Rick Jelliffe 

 This software is provided 'as-is', without any express or implied warranty. 
 In no event will the authors be held liable for any damages arising from 
 the use of this software.

 Permission is granted to anyone to use this software for any purpose, 
 including commercial applications, and to alter it and redistribute it freely,
 subject to the following restrictions:

 1. The origin of this software must not be misrepresented; you must not claim
 that you wrote the original software. If you use this software in a product, 
 an acknowledgment in the product documentation would be appreciated but is 
 not required.

 2. Altered source versions must be plainly marked as such, and must not be 
 misrepresented as being the original software.

 3. This notice may not be removed or altered from any source distribution.
-->
<xslt:stylesheet version="1.0" xmlns:xslt="http://www.w3.org/1999/XSL/Transform" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:iso="http://purl.oclc.org/dsdl/schematron" xmlns:nvdl="http://purl.oclc.org/dsdl/nvdl" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:schold="http://www.ascc.net/xml/schematron" xmlns:crdl="http://purl.oclc.org/dsdl/crepdl/ns/structure/1.0" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:dtll="http://www.jenitennison.com/datatypes" xmlns:dsdl="http://www.dsdl.org/namespace" xmlns:relax="http://relaxng.org/ns/structure/1.0" xmlns:xlink="http://www.w3.org/1999/xlink">
	<!-- Note: The URL for the dsdl namespace is not official -->
	<xsl:param name="include-schematron">true</xsl:param>
	<xsl:param name="include-crdl">true</xsl:param>
	<xsl:param name="include-xinclude">true</xsl:param>
	<xsl:param name="include-dtll">true</xsl:param>
	<xsl:param name="include-relaxng">true</xsl:param>
	<xsl:param name="include-xlink">true</xsl:param>
	<xsl:template match="/">
		<xsl:apply-templates select="." mode="dsdl:go"/>
	</xsl:template>
	<!-- output everything else unchanged -->
	<xslt:template match="node()" priority="-1" mode="dsdl:go">
		<xslt:copy>
			<xslt:copy-of select="@*"/>
			<xslt:apply-templates mode="dsdl:go"/>
		</xslt:copy>
	</xslt:template>
	<!-- =========================================================== -->
	<!-- ISO/IEC 19757 - DSDL Document Schema Definition Languages   -->
	<!-- Part 2 - Regular grammar-based validation - RELAX NG        -->
	<!-- This only implements relax:extRef not relax:include which   -->
	<!-- is complex.                                                 -->
	<!-- =========================================================== -->
	<xslt:template match="relax:extRef[ $include-relaxng = 'true' ]" mode="dsdl:go">
		<!-- Insert subschema -->
		<xsl:variable name="document-uri" select="substring-before(concat(@href,'#'), '#')"/>
		<xsl:variable name="fragment-id" select="substring-after(@href, '#')"/>
		<xsl:choose>
			<xsl:when test="string-length( $document-uri ) = 0 and string-length( $fragment-id ) = 0">
				<xsl:message>Error: Impossible URL in RELAX NG extRef include</xsl:message>
			</xsl:when>
			<!-- this case is when there is in embedded schema in the same document elsewhere -->
			<xslt:when test="string-length( $document-uri ) = 0">
				<xslt:apply-templates mode="dsdl:go" select="//*[@xml:id= $fragment-id ] | id( $fragment-id) | //*[@id= $fragment-id ]"/>
			</xslt:when>
			<xsl:when test="string-length( $fragment-id ) &gt; 0">
				<xsl:variable name="theDocument" select="document( $document-uri,/ )"/>
				<xsl:if test="not($theDocument)">
					<xsl:message terminate="no">
						<xsl:text>Unable to open referenced included file: </xsl:text>
						<xsl:value-of select="@href"/>
					</xsl:message>
				</xsl:if>
				<!-- use a for-each so that the id() function works correctly on the external document -->
				<xsl:for-each select="$theDocument">
					<xsl:variable name="theFragment" select="$theDocument//*[@xml:id= $fragment-id ]        
                  |  id( $fragment-id)          
              | $theDocument//*[@id= $fragment-id ]"/>
					<xsl:if test="not($theFragment)">
						<xsl:message terminate="no">
							<xsl:text>Unable to locate id attribute: </xsl:text>
							<xsl:value-of select="@href"/>
						</xsl:message>
					</xsl:if>
					<xsl:apply-templates select=" $theFragment[1]" mode="dsdl:go"/>
				</xsl:for-each>
			</xsl:when>
			<xsl:otherwise>
				<xsl:variable name="theDocument" select="document( $document-uri,/ )"/>
				<xsl:variable name="theFragment" select="$theDocument/*"/>
				<xsl:if test="not($theDocument)">
					<xsl:message terminate="no">
						<xsl:text>Unable to open referenced included file: </xsl:text>
						<xsl:value-of select="@href"/>
					</xsl:message>
				</xsl:if>
				<xsl:if test="not($theFragment)">
					<xsl:message terminate="no">
						<xsl:text>Unable to locate id attribute: </xsl:text>
						<xsl:value-of select="@href"/>
					</xsl:message>
				</xsl:if>
				<xsl:apply-templates select="$theFragment " mode="dsdl:go"/>
			</xsl:otherwise>
		</xsl:choose>
	</xslt:template>
	<!-- =========================================================== -->
	<!-- ISO/IEC 19757 - DSDL Document Schema Definition Languages   -->
	<!-- Part 3 - Rule-based validation - Schematron                 -->
	<!-- =========================================================== -->
	<!-- Extend the URI syntax to allow # references -->
	<!-- Add experimental support for simple containers like  /xxx:xxx/iso:pattern to allow better includes -->
	<xsl:template match="iso:include[ $include-schematron = 'true' ]" mode="dsdl:go">
		<xsl:variable name="document-uri" select="substring-before(concat(@href,'#'), '#')"/>
		<xsl:variable name="fragment-id" select="substring-after(@href, '#')"/>
		<xsl:choose>
			<xsl:when test="string-length( $document-uri ) = 0 and string-length( $fragment-id ) = 0">
				<xsl:message>Error: Impossible URL in Schematron include</xsl:message>
			</xsl:when>
			<!-- this case is when there is in embedded schema in the same document elsewhere -->
			<xslt:when test="string-length( $document-uri ) = 0">
				<xslt:apply-templates mode="dsdl:go" select="//iso:*[@xml:id= $fragment-id ] 
              	 |id( $fragment-id)
              	 | //iso:*[@id= $fragment-id ]"/>
			</xslt:when>
			<!-- case where there is a fragment in another document (should be an iso: element) -->
			<xsl:when test="string-length( $fragment-id ) &gt; 0">
				<xsl:variable name="theDocument" select="document( $document-uri,/ )"/>
				<xsl:if test="not($theDocument)">
					<xsl:message terminate="no">
						<xsl:text>Unable to open referenced included file: </xsl:text>
						<xsl:value-of select="@href"/>
					</xsl:message>
				</xsl:if>
				<!-- use for-each to rebase id() to external document -->
				<xsl:for-each select="$theDocument">
					<xsl:variable name="theFragment" select=" $theDocument//iso:*[@xml:id= $fragment-id ] |
              	 id($fragment-id) |
              	$theDocument//iso:*[@id= $fragment-id ]"/>
					<xsl:if test=" $theFragment/self::iso:schema ">
						<xsl:message>Schema error: Use include to include fragments, not a whole schema</xsl:message>
					</xsl:if>
					<xsl:if test="not($theFragment)">
						<xsl:message terminate="no">
							<xsl:text>Unable to locate id attribute: </xsl:text>
							<xsl:value-of select="@href"/>
						</xsl:message>
					</xsl:if>
					<xsl:apply-templates select=" $theFragment[1]" mode="dsdl:go"/>
				</xsl:for-each>
			</xsl:when>
			<!-- Case where there is no ID so we include the whole document -->
			<!-- Experimental addition: include fragments of children -->
			<xsl:otherwise>
				<xsl:variable name="theDocument" select="document( $document-uri,/ )"/>
				<xsl:variable name="theFragment" select="$theDocument/iso:*"/>
				<xsl:variable name="theContainedFragments" select="$theDocument/*/iso:* | $theDocument/*/xsl:* | $theDocument/*/xhtml:*"/>
				<xsl:if test="not($theDocument)">
					<xsl:message terminate="no">
						<xsl:text>Unable to open referenced included file: </xsl:text>
						<xsl:value-of select="@href"/>
					</xsl:message>
				</xsl:if>
				<xsl:if test=" $theFragment/self::iso:schema or $theContainedFragments/self::iso:schema">
					<xsl:message>Schema error: Use include to include fragments, not a whole schema</xsl:message>
				</xsl:if>
				<xsl:if test="not($theFragment) and not ($theContainedFragments)">
					<xsl:message terminate="no">
						<xsl:text>Unable to locate id attribute: </xsl:text>
						<xsl:value-of select="@href"/>
					</xsl:message>
				</xsl:if>
				<!-- If this were XLST 2, we could use  
					if ($theFragment) then $theFragment else $theContainedFragments
				here (thanks to KN)
			-->
				<xsl:choose>
					<xsl:when test=" $theFragment ">
						<xsl:apply-templates select="$theFragment " mode="dsdl:go"/>
					</xsl:when>
					<xsl:otherwise>
						<!-- WARNING!  EXPERIMENTAL! Use at your own risk. This may be discontinued! -->
						<xsl:apply-templates select="  $theContainedFragments " mode="dsdl:go"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<!-- =========================================================== -->
	<!-- Handle Schematron 1.6 inclusions: clone of ISO code above   -->
	<!-- =========================================================== -->
	<!-- Extend the URI syntax to allow # references -->
	<!-- Add experimental support for simple containers like  /xxx:xxx/schold:pattern to allow better includes -->
	<xsl:template match="schold:include[ $include-schematron = 'true' ]" mode="dsdl:go">
		<xsl:variable name="document-uri" select="substring-before(concat(@href,'#'), '#')"/>
		<xsl:variable name="fragment-id" select="substring-after(@href, '#')"/>
		<xsl:choose>
			<xsl:when test="string-length( $document-uri ) = 0 and string-length( $fragment-id ) = 0">
				<xsl:message>Error: Impossible URL in Schematron include</xsl:message>
			</xsl:when>
			<!-- this case is when there is in embedded schema in the same document elsewhere -->
			<xslt:when test="string-length( $document-uri ) = 0">
				<xslt:apply-templates mode="dsdl:go" select="//schold:*[@xml:id= $fragment-id ] 
              	 |id( $fragment-id)
              	 | //schold:*[@id= $fragment-id ]"/>
			</xslt:when>
			<!-- case where there is a fragment in another document (should be an iso: element) -->
			<xsl:when test="string-length( $fragment-id ) &gt; 0">
				<xsl:variable name="theDocument" select="document( $document-uri,/ )"/>
				<xsl:if test="not($theDocument)">
					<xsl:message terminate="no">
						<xsl:text>Unable to open referenced included file: </xsl:text>
						<xsl:value-of select="@href"/>
					</xsl:message>
				</xsl:if>
				<!-- use for-each to rebase id() to $theDocument -->
				<xsl:for-each select="$theDocument">
					<xsl:variable name="theFragment" select=" $theDocument//schold:*[@xml:id= $fragment-id ] |
              	id($fragment-id) |
              	$theDocument//schold:*[@id= $fragment-id ]"/>
					<xsl:if test=" $theFragment/self::schold:schema ">
						<xsl:message>Schema error: Use include to include fragments, not a whole schema</xsl:message>
					</xsl:if>
					<xsl:if test="not($theFragment)">
						<xsl:message terminate="no">
							<xsl:text>Unable to locate id attribute: </xsl:text>
							<xsl:value-of select="@href"/>
						</xsl:message>
					</xsl:if>
					<xsl:apply-templates select=" $theFragment[1]" mode="dsdl:go"/>
				</xsl:for-each>
			</xsl:when>
			<!-- Case where there is no ID so we include the whole document -->
			<!-- Experimental addition: include fragments of children -->
			<xsl:otherwise>
				<xsl:variable name="theDocument" select="document( $document-uri,/ )"/>
				<xsl:variable name="theFragment" select="$theDocument/iso:*"/>
				<xsl:variable name="theContainedFragments" select="$theDocument/*/schold:* | $theDocument/*/xsl:* | $theDocument/*/xhtml:*"/>
				<xsl:if test="not($theDocument)">
					<xsl:message terminate="no">
						<xsl:text>Unable to open referenced included file: </xsl:text>
						<xsl:value-of select="@href"/>
					</xsl:message>
				</xsl:if>
				<xsl:if test=" $theFragment/self::schold:schema or $theContainedFragments/self::schold:schema">
					<xsl:message>Schema error: Use include to include fragments, not a whole schema</xsl:message>
				</xsl:if>
				<xsl:if test="not($theFragment) and not ($theContainedFragments)">
					<xsl:message terminate="no">
						<xsl:text>Unable to locate id attribute: </xsl:text>
						<xsl:value-of select="@href"/>
					</xsl:message>
				</xsl:if>
				<!-- If this were XLST 2, we could use  
					if ($theFragment) then $theFragment else $theContainedFragments
				here (thanks to KN)
			-->
				<xsl:choose>
					<xsl:when test=" $theFragment ">
						<xsl:apply-templates select="$theFragment " mode="dsdl:go"/>
					</xsl:when>
					<xsl:otherwise>
						<!-- WARNING!  EXPERIMENTAL! Use at your own risk. This may be discontinued! -->
						<xsl:apply-templates select="  $theContainedFragments " mode="dsdl:go"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<!-- =========================================================== -->
	<!-- ISO/IEC 19757 - DSDL Document Schema Definition Languages   -->
	<!-- Part 5 - DataType Library Language - DTLL                   -->
	<!-- Committee Draft  Experimental support only                  -->
	<!-- The <include> element may well be replaced by XInclude in   -->
	<!-- any final version.                                          -->
	<!-- =========================================================== -->
	<xslt:template match="dtll:include[ $include-dtll = 'true' ]" mode="dsdl:go">
		<!-- Insert subschema -->
		<xsl:variable name="document-uri" select="substring-before(concat(@href,'#'), '#')"/>
		<xsl:variable name="fragment-id" select="substring-after(@href, '#')"/>
		<xsl:choose>
			<xsl:when test="string-length( $document-uri ) = 0 and string-length( $fragment-id ) = 0">
				<xsl:message>Error: Impossible URL in DTLL include</xsl:message>
			</xsl:when>
			<!-- this case is when there is in embedded schema in the same document elsewhere -->
			<xslt:when test="string-length( $document-uri ) = 0">
				<xslt:apply-templates mode="dsdl:go" select="//*[@xml:id= $fragment-id ] | id( $fragment-id) 
              	| //*[@id= $fragment-id ]"/>
			</xslt:when>
			<xsl:when test="string-length( $fragment-id ) &gt; 0">
				<xsl:variable name="theDocument" select="document( $document-uri,/ )"/>
				<xsl:if test="not($theDocument)">
					<xsl:message terminate="no">
						<xsl:text>Unable to open referenced included file: </xsl:text>
						<xsl:value-of select="@href"/>
					</xsl:message>
				</xsl:if>
				<!-- use for-each to rebase id() to $theDocument -->
				<xsl:for-each select="$theDocument">
					<xsl:variable name="theFragment" select="$theDocument//*[@xml:id= $fragment-id ]
               | id( $fragment-id ) 
               | $theDocument//*[@id= $fragment-id ]"/>
					<xsl:if test="not($theFragment)">
						<xsl:message terminate="no">
							<xsl:text>Unable to locate id attribute: </xsl:text>
							<xsl:value-of select="@href"/>
						</xsl:message>
					</xsl:if>
					<xsl:apply-templates select=" $theFragment[1]" mode="dsdl:go"/>
				</xsl:for-each>
			</xsl:when>
			<xsl:otherwise>
				<xsl:variable name="theDocument" select="document( $document-uri,/ )"/>
				<xsl:variable name="theFragment" select="$theDocument/*"/>
				<xsl:if test="not($theDocument)">
					<xsl:message terminate="no">
						<xsl:text>Unable to open referenced included file: </xsl:text>
						<xsl:value-of select="@href"/>
					</xsl:message>
				</xsl:if>
				<xsl:if test="not($theFragment)">
					<xsl:message terminate="no">
						<xsl:text>Unable to locate id attribute: </xsl:text>
						<xsl:value-of select="@href"/>
					</xsl:message>
				</xsl:if>
				<xsl:apply-templates select="$theFragment " mode="dsdl:go"/>
			</xsl:otherwise>
		</xsl:choose>
	</xslt:template>
	<!-- =========================================================== -->
	<!-- ISO/IEC 19757 - DSDL Document Schema Definition Languages   -->
	<!-- Part 7 - Character Repertoire Description Language - CRDL   -->
	<!-- Final Committee Draft 2008-01-11 Experimental support only  -->
	<!-- =========================================================== -->
	<xslt:template match="crdl:ref[ $include-crdl = 'true' ]" mode="dsdl:go">
		<!-- Insert subschema -->
		<xsl:variable name="document-uri" select="substring-before(concat(@href,'#'), '#')"/>
		<xsl:variable name="fragment-id" select="substring-after(@href, '#')"/>
		<xsl:choose>
			<xsl:when test="string-length( $document-uri ) = 0 and string-length( $fragment-id ) = 0">
				<xsl:message>Error: Impossible URL in CRDL include</xsl:message>
			</xsl:when>
			<!-- this case is when there is in embedded schema in the same document elsewhere -->
			<xslt:when test="string-length( $document-uri ) = 0">
				<xslt:apply-templates mode="dsdl:go" select="//*[@xml:id= $fragment-id ] | id( $fragment-id)
              	| //*[@id= $fragment-id ]"/>
			</xslt:when>
			<xsl:when test="string-length( $fragment-id ) &gt; 0">
				<xsl:variable name="theDocument" select="document( $document-uri,/ )"/>
				<xsl:if test="not($theDocument)">
					<xsl:message terminate="no">
						<xsl:text>Unable to open referenced included file: </xsl:text>
						<xsl:value-of select="@href"/>
					</xsl:message>
				</xsl:if>
				<!-- use for-each to rebase id() to $theDocument -->
				<xsl:for-each select="$theDocument">
					<xsl:variable name="theFragment" select="$theDocument//*[@xml:id= $fragment-id ]
               | id( $fragment-id )
               | $theDocument//*[@id= $fragment-id ]"/>
					<xsl:if test="not($theFragment)">
						<xsl:message terminate="no">
							<xsl:text>Unable to locate id attribute: </xsl:text>
							<xsl:value-of select="@href"/>
						</xsl:message>
					</xsl:if>
					<xsl:apply-templates select=" $theFragment " mode="dsdl:go"/>
				</xsl:for-each>
			</xsl:when>
			<xsl:otherwise>
				<xsl:variable name="theDocument" select="document( $document-uri,/ )"/>
				<xsl:variable name="theFragment" select="$theDocument/*"/>
				<xsl:if test="not($theDocument)">
					<xsl:message terminate="no">
						<xsl:text>Unable to open referenced included file: </xsl:text>
						<xsl:value-of select="@href"/>
					</xsl:message>
				</xsl:if>
				<xsl:if test="not($theFragment)">
					<xsl:message terminate="no">
						<xsl:text>Unable to locate id attribute: </xsl:text>
						<xsl:value-of select="@href"/>
					</xsl:message>
				</xsl:if>
				<xsl:apply-templates select="$theFragment " mode="dsdl:go"/>
			</xsl:otherwise>
		</xsl:choose>
	</xslt:template>
	<!-- =========================================================== -->
	<!-- ISO/IEC 19757 - DSDL Document Schema Definition Languages   -->
	<!-- Part 4 - Namespace-based Validation Dispatching Language - NVDL -->
	<!-- Note: This does not include schemas referenced for          -->
	<!-- validation, it merely handles any simple XIncludes          -->
	<!-- =========================================================== -->
	<!-- ISO/IEC 19757 - DSDL Document Schema Definition Languages   -->
	<!-- Part 8 - Document Schema Renaming Language - DSRL           -->
	<!-- Note: Final? Committee Draft   Experimental support only    -->
	<!-- =========================================================== -->
	<!-- XInclude support for id based references only, with 1 level -->
	<!-- of fallback.                                                -->
	<!-- =========================================================== -->
	<xslt:template mode="dsdl:go" match="xi:include[@href][not(@parseType) or @parseType ='xml'][ $include-xinclude = 'true' ]">
		<!-- Simple inclusions only here -->
		<xsl:choose>
			<xsl:when test="contains( @href, '#')">
				<xsl:message terminate="yes">Fatal error: Xinclude href contains
       				fragment identifier #</xsl:message>
			</xsl:when>
			<xsl:when test="contains( @xpointer, '(')">
				<xsl:message terminate="yes">Fatal error: Sorry, this software
       			only supports simple ids in XInclude xpointers</xsl:message>
			</xsl:when>
			<xsl:when test="string-length( @href ) = 0 and string-length( @xpointer ) = 0">
				<xsl:message terminate="yes">Fatal Error: Impossible URL in XInclude include</xsl:message>
			</xsl:when>
			<!-- this case is when there is in embedded schema in the same document elsewhere -->
			<xslt:when test="string-length( @href ) = 0">
				<xslt:apply-templates mode="dsdl:go" select="//*[@xml:id= current()/@xpointer  ] | id( @xpointer)
              	| //*[@id= current()/@xpointer  ]"/>
			</xslt:when>
			<xsl:when test="string-length( @xpointer ) &gt; 0">
				<xsl:variable name="theDocument" select="document( @href,/ )"/>
				<xsl:variable name="theFragment" select="$theDocument//*[@xml:id= current()/@xpointer  ]
             
              | $theDocument//*[@id= current()/@xpointer  ]"/>
				<!-- removed
               | $theDocument/id( @xpointer)
               because it requires rebasing in XSLT1 and that would mess up the use of current()
               -->
				<!-- Allow one level of fallback, to another XInclude -->
				<xsl:if test="not($theDocument)">
					<xsl:choose>
						<xsl:when test="xi:fallback">
							<xsl:variable name="theDocument" select="document( xi:fallback[1]/xi:include[not(@parseType)
                    	 or @parseType='xml']/@href,/ )"/>
							<xsl:variable name="theFragment" select="$theDocument//*[@xml:id= current()/xi:fallback[1]/xi:include/@xpointer  ]
              				| $theDocument//*[@id= current()/xi:fallback[1]/xi:include/@xpointer  ]"/>
							<!-- removed 
              						| $theDocument/id( xi:fallback[1]/xi:include/@xpointer)
              				because it id() would need rebasing in XSLT1 and that would mess up use of current()
              				-->
							<xsl:if test="not($theDocument)">
								<xsl:message terminate="no">
									<xsl:text>Unable to open referenced included file and fallback
									file: </xsl:text>
									<xsl:value-of select="@href"/>
								</xsl:message>
							</xsl:if>
						</xsl:when>
						<xsl:otherwise>
							<xsl:message terminate="no">
								<xsl:text>Unable to open referenced included file: </xsl:text>
								<xsl:value-of select="@href"/>
							</xsl:message>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:if>
				<xsl:apply-templates select=" $theFragment" mode="dsdl:go"/>
			</xsl:when>
			<!-- Document but no fragment specified -->
			<xsl:otherwise>
				<xsl:variable name="theDocument" select="document( @href,/ )"/>
				<xsl:variable name="theFragment" select="$theDocument/*"/>
				<xsl:if test="not($theDocument)">
					<xsl:message terminate="no">
						<xsl:text>Unable to open referenced included file: </xsl:text>
						<xsl:value-of select="@href"/>
					</xsl:message>
				</xsl:if>
				<xsl:apply-templates select="$theFragment " mode="dsdl:go"/>
			</xsl:otherwise>
		</xsl:choose>
	</xslt:template>
	<!-- =========================================================== -->
	<!-- W3C XLink 1.1 embedded simple links                        -->
	<!-- =========================================================== -->
	<xslt:template match="*[@xlink:href][not(parent::*[@xlink:type='complex'])]
	           [not(@xlink:type) or (@xlink:type='simple')]
	           [@xlink:show='embed']
	           [not(@xlink:actuate) or (@xlink:actuate='onLoad')]
	           [ $include-xlink = 'true' ]" mode="dsdl:go" priority="1">
		<xsl:variable name="document-uri" select="substring-before(concat(@xlink:href,'#'), '#')"/>
		<xsl:variable name="fragment-id" select="substring-after(@xlink:href, '#')"/>
		<xsl:choose>
			<xsl:when test="string-length( $document-uri ) = 0 and string-length( $fragment-id ) = 0">
				<xsl:message>Error: Impossible URL in XLink embedding link</xsl:message>
			</xsl:when>
			<!-- this case is when there is in embedded schema in the same document elsewhere -->
			<xslt:when test="string-length( $document-uri ) = 0">
				<xslt:apply-templates mode="dsdl:go" select="//*[@xml:id= $fragment-id ] | id( $fragment-id) 
              	| //*[@id= $fragment-id ]"/>
			</xslt:when>
			<xsl:when test="string-length( $fragment-id ) &gt; 0">
				<xsl:variable name="theDocument" select="document( $document-uri,/ )"/>
				<xsl:if test="not($theDocument)">
					<xsl:message terminate="no">
						<xsl:text>Unable to open referenced included file: </xsl:text>
						<xsl:value-of select="@xlink:href"/>
					</xsl:message>
				</xsl:if>
				<!-- use for-each to rebase id() to $theDocument -->
				<xsl:for-each select="$theDocument">
					<xsl:variable name="theFragment" select="$theDocument//*[@xml:id= $fragment-id ]
               | id( $fragment-id ) 
               | $theDocument//*[@id= $fragment-id ]"/>
					<xsl:if test="not($theFragment)">
						<xsl:message terminate="no">
							<xsl:text>Unable to locate id attribute: </xsl:text>
							<xsl:value-of select="@xlink:href"/>
						</xsl:message>
					</xsl:if>
					<xsl:apply-templates select=" $theFragment[1]" mode="dsdl:go"/>
				</xsl:for-each>
			</xsl:when>
			<xsl:otherwise>
				<xsl:variable name="theDocument" select="document( $document-uri,/ )"/>
				<xsl:variable name="theFragment" select="$theDocument/*"/>
				<xsl:if test="not($theDocument)">
					<xsl:message terminate="no">
						<xsl:text>Unable to open referenced included file: </xsl:text>
						<xsl:value-of select="@xlink:href"/>
					</xsl:message>
				</xsl:if>
				<xsl:if test="not($theFragment)">
					<xsl:message terminate="no">
						<xsl:text>Unable to locate id attribute: </xsl:text>
						<xsl:value-of select="@xlink:href"/>
					</xsl:message>
				</xsl:if>
				<xsl:apply-templates select="$theFragment " mode="dsdl:go"/>
			</xsl:otherwise>
		</xsl:choose>
	</xslt:template>
</xslt:stylesheet>
