<?xml version="1.0"?>
<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:wsdl11="http://schemas.xmlsoap.org/wsdl/"
    xmlns:soap11enc="http://schemas.xmlsoap.org/soap/encoding/"
    exclude-result-prefixes="xs xsi pat ex wsdl11 soap11enc">

<xsl:import href="../common/xmlspec.xsl"/>

<xsl:strip-space elements="eg pre exampleInner"/>

<xsl:param name="showAssertions">true</xsl:param>

<xsl:param name="additional.css">
p { clear: both }

th
{ 
    color: #000000; 
    background-color: #CCCC99; 
}

span.test-assertion 
{ 
    background-color: #FFC0CB; 
}

span.test-assertion-tr:hover 
{
    background-color: #FFC0CB;
}

.prefix 
{ 
    float:left; width: 15%;
    background: yellow;
    color: black;
    border: solid black thin; 
    padding: .1em;
    margin-left: 0%;
    margin-right: 2%;
}

div.xpath
{ 
    text-align: center;
    background-color: #FFCC88;
    border-top-width: 4px;
    border-top-style: double;
    border-top-color: #FFEECC;
    border-bottom-width: 4px;
    border-bottom-style: double;
    border-bottom-color: #FFEECC;
    padding: 8px; 
    margin: 0em
}

pre em      
{
    font-style: italic;
    font-weight: bold;
}
</xsl:param>

<xsl:output method="xml" encoding="utf-8"
	    doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"
	    doctype-system="http://www.w3.org/TR/html4/loose.dtd" indent="no" />

<xsl:template match="xpath">
    <div class="xpath" id="{@id}"><code><xsl:value-of select="."/></code></div>
</xsl:template>



	<!--  Name the assertion anchor and link to the assertion summary table entry. -->
	<xsl:template match="assert">
		<xsl:choose>
			<xsl:when test="$showAssertions='true'">
			  <xsl:variable name="classname">
				<xsl:choose>
				  <xsl:when test="/spec/@role='editors-copy'">
				    <xsl:text>test-assertion</xsl:text>
				  </xsl:when>
				  <xsl:otherwise>
				    <xsl:text>test-assertion-tr</xsl:text>		    
				  </xsl:otherwise>
				</xsl:choose>
			  </xsl:variable>
			  <span id="{@id}" class="{$classname}">
			    <xsl:apply-templates select="*|text()" />
				<sup><a href="#{@id}-summary"
					title="Link to assertion {@id} summary">
					<xsl:text>&#x2020;</xsl:text>
				</a></sup>
			  </span>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="*|text()" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="*[@id='assertionsummary']">
	  <xsl:if test="$showAssertions='true'">
	    <xsl:apply-templates/>
	  </xsl:if>
	</xsl:template>

	<!-- Name the assertion summary table entry and link back to the assertion. -->
	<xsl:template match="assert-summary">
		<a name="{@ref}-summary" href="#{@ref}">
			<xsl:value-of select="@ref" />
		</a>
	</xsl:template>

	<xsl:template match="assert-ref">
		[<a name="{@ref}-summary" href="#{@ref}"><xsl:value-of select="@ref" /></a>]
	</xsl:template>

	<xsl:template match="dl">
		<dl>
			<xsl:apply-templates />
		</dl>
	</xsl:template>

	<xsl:template match="dt">
		<dt>
			<xsl:apply-templates />
		</dt>
	</xsl:template>

	<xsl:template match="dd">
		<dd>
			<xsl:apply-templates />
		</dd>
	</xsl:template>

	<xsl:template match="assert">
		<xsl:choose>
			<xsl:when test="$showAssertions='true'">
			  <xsl:variable name="classname">
				<xsl:choose>
				  <xsl:when test="/spec/@role='editors-copy'">
				    <xsl:text>test-assertion</xsl:text>
				  </xsl:when>
				  <xsl:otherwise>
				    <xsl:text>test-assertion-tr</xsl:text>		    
				  </xsl:otherwise>
				</xsl:choose>
			  </xsl:variable>
			  <span id="{@id}" class="{$classname}">
			    <xsl:apply-templates select="*|text()" />
				<sup><a href="#{@id}-summary"
					title="Link to assertion {@id} summary">
					<xsl:text>&#x2020;</xsl:text>
				</a></sup>
			  </span>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="*|text()" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="include-explanation">
	    <xsl:variable name="ref" select="@ref"/> 
	    <xsl:if test="//explanation[@xml:id=$ref]">
		<xsl:apply-templates select="//explanation[@xml:id=$ref]/*"/>
	    </xsl:if>
	</xsl:template>

	<xsl:template match="explanation"/>

	<xsl:template match="*[@id='assertionsummary']">
	  <xsl:if test="$showAssertions='true'">
	    <xsl:apply-templates/>
	  </xsl:if>
	</xsl:template>

	<!-- Name the assertion summary table entry and link back to the assertion. -->
	<xsl:template match="assert-summary">
		<a name="{@ref}-summary" href="#{@ref}">
			<xsl:value-of select="@ref" />
		</a>
	</xsl:template>

	<!-- Name the pattern summary table entry and link back to the pattern. -->
	<xsl:template match="pattern-summary">
		<a name="{@ref}-summary" href="#{@ref}">
			<xsl:value-of select="@ref" />
		</a>
		<xsl:if test="@eg">
		<xsl:text> </xsl:text>
		<a name="{@eg}-summary" href="#{@eg}">&#8225;</a>
		</xsl:if>
	</xsl:template>

 <xsl:template match="altlocs">
    <p>
      <xsl:text>This document is also available </xsl:text>
      <xsl:text>in these non-normative formats: </xsl:text>
      <xsl:for-each select="loc">
        <xsl:if test="position() &gt; 1">
          <xsl:if test="last() &gt; 2">
            <xsl:text>, </xsl:text>
          </xsl:if>
          <xsl:if test="last() = 2">
            <xsl:text> </xsl:text>
          </xsl:if>
        </xsl:if>
        <xsl:if test="position() = last() and position() &gt; 1">and&#160;</xsl:if>
        <a href="{@href}"><xsl:apply-templates/></a>
      </xsl:for-each>
      <xsl:text>.</xsl:text>
    </p>
  </xsl:template>

  <xsl:template match="eg">
        <div class="exampleInner">
	<pre>
          <xsl:copy-of select="node()"/>
	</pre>
        </div>
  </xsl:template>

  <xsl:template match='inform-div1[@id="schemasummary"]'>
    <div class="div1">
      <xsl:apply-templates/>

	<xsl:variable name='schema'
		      select='document("http://www.w3.org/2001/XMLSchema.xsd")'/>
	<xsl:variable name='xpaths'
		      select="/spec/body/div1[@id='patterns']//xpath" />
	
	<table id='schema_elements' border='1' width='100%'>
	     <caption><xsl:text>Table </xsl:text>
	      <xsl:apply-templates select="." mode="divnum"/>
	  List of supported and non-supported schema elements</caption>
	 <thead>
	   <tr>
	     <th>Element</th>
	     <th>Patterns</th>
	   </tr>
	 </thead>
	 <tbody>
	   <xsl:for-each select='$schema//xs:element[@name and @id]'>
	     <xsl:sort select='@name'/>
	     <xsl:variable name='name' select='concat("xs:", @name)'/>
	     <tr id='xsd_element_{@name}'>
	       <th><xsl:value-of select='$name'/></th>
	       <td>
		 <xsl:variable name='xpaths_name'
			       select="$xpaths[contains(., $name)]" />
		 <xsl:variable name='count'
			       select="count($xpaths_name)" />
		 <xsl:choose>
		   <xsl:when test='$count=0'>
		     <xsl:text>N/A</xsl:text>
		   </xsl:when>
		   <xsl:otherwise>
		     <xsl:for-each select='$xpaths_name'>
		       <xsl:variable name='div3' select='ancestor::div3'/>
		       <a href='#{$div3/@id}'><xsl:value-of select='$div3/head'/></a>
		       <xsl:if test='position()!=$count'>
			 <xsl:text>, </xsl:text>
		       </xsl:if>
		     </xsl:for-each>
		   </xsl:otherwise>
		 </xsl:choose>
	       </td>
	     </tr>
	   </xsl:for-each>
	 </tbody>
       </table>

	<table id='schema_attributes' border='1' width='100%'>
	     <caption><xsl:text>Table </xsl:text>
	      <xsl:apply-templates select="." mode="divnum"/>
	 List of supported and non-supported schema attributes</caption>
	 <thead>
	   <tr>
	     <th>Attribute</th>
	     <th>Patterns</th>
	   </tr>
	 </thead>
	 <tbody>
	   <xsl:call-template name='uniq-xsd-attribute'>
	     <xsl:with-param name='xpaths' select='$xpaths'/>
	     <xsl:with-param name='attributes'>
	       <xsl:for-each select='$schema//xs:attribute[@name]'>
		 <xsl:sort select='@name'/>
		 <xsl:value-of select='@name'/><xsl:text>,</xsl:text>
	       </xsl:for-each>		 
	     </xsl:with-param>
	   </xsl:call-template>
	 </tbody>
	</table>
       

	<xsl:variable name='start_string'>xs:QName('</xsl:variable>
	<xsl:variable name='end_string'>')</xsl:variable>

	<table id='schema_types' border='1' width='100%'>
	     <caption><xsl:text>Table </xsl:text>
	      <xsl:apply-templates select="." mode="divnum"/>
	 List of supported and non-supported schema simple types</caption>
	 <thead>
	   <tr>
	     <th>Simple Type</th>
	     <th>Patterns</th>
	   </tr>
	 </thead>
	 <tbody>
	   <xsl:for-each select='$schema//xs:simpleType[@name and @id and not(starts-with(@name, "xml:"))]'>
	     <xsl:sort select='@name'/>
	     <xsl:variable name='name' select='concat("xs:", @name)'/>
	     <tr id='xsd_simple_type_{@name}'>
	       <th><xsl:value-of select='$name'/></th>
	       <td>
		 <xsl:variable name='xpaths_name'
			       select="$xpaths[contains(., concat($start_string, $name, $end_string))]" />
		 <xsl:variable name='count'
			       select="count($xpaths_name)" />
		 <xsl:choose>
		   <xsl:when test='$count=0'>
		     <xsl:text>N/A</xsl:text>
		   </xsl:when>
		   <xsl:otherwise>
		     <xsl:for-each select='$xpaths_name'>
		       <xsl:variable name='div3' select='ancestor::div3'/>
		       <a href='#{$div3/@id}'><xsl:value-of select='$div3/head'/></a>
		       <xsl:if test='position()!=$count'>
			 <xsl:text>, </xsl:text>
		       </xsl:if>
		     </xsl:for-each>
		   </xsl:otherwise>
		 </xsl:choose>
	       </td>
	     </tr>
	       </xsl:for-each>
	 </tbody>
       </table>
    </div>
  </xsl:template>

  <xsl:template name='uniq-xsd-attribute'>
    <xsl:param name='xpaths'/>
    <xsl:param name='attributes'/>
    <xsl:param name='accepted'/>

    <xsl:variable name='first' select='substring-before($attributes, ",")'/>
    <xsl:variable name='second' select='substring-before(substring-after($attributes, ","), ",")'/>

    <xsl:if test='$first!=$second'>
      <tr id='xsd_simple_type_{$first}'>
	<xsl:variable name='name' select='concat("@",$first)'/>
	<th><xsl:value-of select='$name'/></th>
	<td>
	  <xsl:variable name='xpaths_name'
			select="$xpaths[contains(., $name)]" />
	  <xsl:variable name='count'
			select="count($xpaths_name)" />
	  <xsl:choose>
	    <xsl:when test='$count=0'>
	      <xsl:text>N/A</xsl:text>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:for-each select='$xpaths_name'>
		<xsl:variable name='div3' select='ancestor::div3'/>
		<a href='#{$div3/@id}'><xsl:value-of select='$div3/head'/></a>
		<xsl:if test='position()!=$count'>
		  <xsl:text>, </xsl:text>
		</xsl:if>
	      </xsl:for-each>
	    </xsl:otherwise>
	  </xsl:choose>
	</td>
      </tr>
    </xsl:if>      
    <xsl:if test='$second!=""'>
      <xsl:call-template name='uniq-xsd-attribute'>
	<xsl:with-param name='xpaths' select='$xpaths'/>
	<xsl:with-param name='attributes' select='substring-after($attributes, ",")'/>
      </xsl:call-template>
    </xsl:if>

  </xsl:template>

<xsl:template match="xpath-idiom-list">
	<table width="100%" border='1'>
	     <caption><xsl:text>Table </xsl:text>
	      <xsl:apply-templates select=".." mode="divnum"/>
	      <xsl:value-of select="@summary"/></caption>
	 <thead>
	   <tr>
	     <th>XPath 2.0 Sample</th>
	     <th>Description</th>
	   </tr>
	 </thead>
	 <tbody>
		<xsl:apply-templates/>
	</tbody>
	</table>
</xsl:template>

<xsl:template match="xpath-idiom">
    <tr id="xpath-idiom-{@id}">
	<td><code><xsl:value-of select="@xpath"/></code></td>
	<td><xsl:apply-templates/></td>
    </tr>
</xsl:template>

<xsl:template match="b">
    <b><xsl:apply-templates/></b>
</xsl:template>

</xsl:stylesheet>

