<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:pat="http://www.w3.org/2002/ws/databinding/patterns/6/09/"
    xmlns:ex="http://www.w3.org/2002/ws/databinding/examples/6/09/"
    xmlns:soap11enc="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:wsdl11="http://schemas.xmlsoap.org/wsdl/"
    exclude-result-prefixes="xsi xs pat ex wsdl11 soap11enc">

    <xsl:import href="../common/xml-to-string.xsl"/>
    <xsl:import href="../common/copy-elements-and-text.xsl"/>

    <xsl:param name="status" />
    <xsl:param name="examples">../basic/examples-tmp.xml</xsl:param>
    <xsl:param name="patns">http://www.w3.org/2002/ws/databinding/patterns/6/09/</xsl:param>
    <xsl:param name="exns">http://www.w3.org/2002/ws/databinding/examples/6/09/</xsl:param>

    <xsl:output method="xml" 
	encoding="UTF-8" 
	indent="yes"
        omit-xml-declaration="yes"
	cdata-section-elements="eg"/>

    <xsl:template match="/">
      <xsl:for-each select="//pat:pattern[not(preceding::pat:pattern/@groupid = @groupid)]">
        <xsl:variable name="groupid" select="@groupid"/>
	<xsl:if test="//pat:pattern[@status = $status and @groupid = $groupid]">
	  <div2 id="group-{@groupid}">
	      <head><xsl:value-of select="//pat:group[@groupid = $groupid]/pat:title"/></head>
		<p><include-explanation ref="{@groupid}"/></p>
	      <xsl:apply-templates select="." mode="group"/>
	  </div2>
	</xsl:if>
       </xsl:for-each>
    </xsl:template>

    <xsl:template match="pat:pattern" mode="group">
      <xsl:variable name="groupid" select="@groupid"/>
      <xsl:for-each select="//pat:pattern[@status=$status and @groupid = $groupid]">
	  <xsl:apply-templates select="."/>
      </xsl:for-each>
    </xsl:template>

    <xsl:template match="pat:pattern">
	<xsl:variable name="id" select="@xml:id"/>
        <xsl:variable name="uri"><xsl:value-of select="$patns"/><xsl:value-of select="@xml:id"/></xsl:variable>

	<div3 id="pattern-{$id}" class="pattern">
	  <head><xsl:value-of select="$id"/></head>

	<p>
	   An <bibref ref="XMLSchema"/>, or other <bibref ref="XML"/> document containing an <bibref ref="XMLSchema"/> element
	    <code>&lt;xs:schema&gt;</code>, exhibits the 
	    <a href="{$uri}"><xsl:value-of select="$id"/></a>
	    pattern identified using the URI <bibref ref="RFC3986"/> <xsl:text> </xsl:text> 
	    <a href="{$uri}"><xsl:value-of select="$uri"/></a>
	    when the following <bibref ref="XPath20"/> expression
	    applied to a document or element node with a context node of <code>//xs:schema</code> 
	    results in an <bibref ref="XPath20"/> node-set containing at least one node:
        </p>
	    <xpath id="xpath-{$id}"><xsl:value-of select="pat:xpath"/></xpath>
	<p>
	    <include-explanation ref="{$id}"/>
	</p>

	<xsl:for-each select="document($examples)//ex:example[@xml:id = $id or @pattern = $id]">
	    <xsl:variable name="exampleId" select="@xml:id"/>
	    <xsl:variable name="exampleURI"><xsl:value-of select="$exns"/><xsl:value-of select="$exampleId"/></xsl:variable>

	    <xsl:if test="ex:types">
	    <p>The following example <bibref ref="XMLSchema"/> document
	    [<a href="{$exampleURI}"><xsl:value-of select="$exampleId"/></a>] 
	    illustrates the use of the 
	    <a href="{$uri}"><xsl:value-of select="$id"/></a> 
	    pattern:</p>
	    <xsl:apply-templates select="ex:types" mode="example"/>
	    </xsl:if>

	    <xsl:if test="ex:typedef">
	    <p id="example-{$exampleId}">The following example <bibref ref="XMLSchema"/> extract illustrates the use of the 
		<a href="{$uri}"><xsl:value-of select="$id"/></a> 
		pattern within an <bibref ref="XMLSchema"/> document
		[<a href="{$exampleURI}"><xsl:value-of select="$exampleId"/></a>]: </p>
	    <xsl:apply-templates select="ex:typedef" mode="example"/>
	    </xsl:if>

	    <xsl:apply-templates select="ex:instance"/>

	</xsl:for-each>
	</div3>
    </xsl:template>

    <xsl:template match="ex:instance">
	<xsl:variable name="exampleId" select="../../@xml:id"/>
	<xsl:variable name="id" select="@xml:id"/>
        <xsl:variable name="uri"><xsl:value-of select="$exns"/><xsl:value-of select="@xml:id"/></xsl:variable>

	<p id="instance-{@xml:id}">
	<xsl:choose>
	<xsl:when test="not(preceding-sibling::ex:instance)">
	The following example <bibref ref="XML"/> element is 
	valid against the above example <bibref ref="XMLSchema"/> when included 
	inside 
	</xsl:when>
	<xsl:when test="count(preceding-sibling::ex:instance) = 1">
		as is the following element when included in 
	</xsl:when>
	<xsl:otherwise> 
		and the following element when included in 
	</xsl:otherwise>
	</xsl:choose>
	an instance document 
	[<a href="{$exns}{../@xml:id}#{@xml:id}"><xsl:value-of select="@xml:id"/></a>]:</p> 

	    <xsl:apply-templates select="./*" mode="instance"/>

    </xsl:template>

    <xsl:template match="*" mode="instance">
	<exampleInner id="example-{@xml:id}" href="{$exns}{../@xml:id}">
	    <eg><xsl:attribute name="xml:space">preserve</xsl:attribute><xsl:apply-templates select="." mode="pre"/></eg>
	</exampleInner>
    </xsl:template>

    <xsl:template match="*" mode="example">
	<exampleInner id="example-{@xml:id}" href="{$exns}{../@xml:id}">
	    <eg><xsl:copy-of select="node()"/></eg>
	</exampleInner>
    </xsl:template>

    <xsl:template match="*" mode="pre">
	<xsl:call-template name="xml-to-string">
	    <xsl:with-param name="include-ns-decls" select="false()"/>
	</xsl:call-template>
	<xsl:text>&#10;</xsl:text>
    </xsl:template>

</xsl:stylesheet>

