<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
		xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
		xmlns:xs="http://www.w3.org/2001/XMLSchema" 
		xmlns:fo="http://www.w3.org/1999/XSL/Format">

  <xsl:output method="html"/>

  <xsl:variable name="schemaPrefix" select="concat(/xs:schema/xs:annotation/xs:appinfo/schemaPrefix, ':')"/>
  <xsl:variable name="targetPrefix" select="concat(/xs:schema/xs:annotation/xs:appinfo/targetPrefix, ':')"/>
<!--
  <xsl:variable name="schemaPrefix" select="'xs:'"/>
  <xsl:variable name="targetPrefix" select="'voq:'"/>
-->  

  <xsl:template name="briefDoc">
    <xsl:choose>
      <xsl:when test="contains(., '.')">
        <xsl:value-of select="concat(substring-before(., '.'), '.')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="."/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="briefElement">
    <xsl:param name="elname" select="substring-after(@ref,':')"/>

    <xsl:text>  </xsl:text><dd><xsl:text> </xsl:text><dl><xsl:text> 
         </xsl:text><dt><table border="0" width="95%" cellspacing="0">
         <tr><xsl:text>
	     </xsl:text><td align="left" width="20%"><xsl:text>
             </xsl:text><strong><a href="#el:{$elname}">
             <xsl:text>&lt;</xsl:text>
  	     <xsl:value-of select="$elname"/><xsl:text>&gt;</xsl:text>
   	     </a></strong><xsl:text>
             </xsl:text></td><td align="left" width="35%">

      <xsl:choose>
        <xsl:when test="starts-with(@type, $schemaPrefix)">
          <xsl:text>
	     </xsl:text><strong><xsl:text>Type: </xsl:text></strong>
          <xsl:value-of select="substring-after(@type, ':')"/><xsl:text>
             </xsl:text>
        </xsl:when> 
        <xsl:when test="@type">
          <xsl:text>
	     </xsl:text><strong><xsl:text>Type: </xsl:text></strong>
          <a href="#tp:{substring-after(@type, ':')}">
          <xsl:value-of select="substring-after(@type, ':')"/></a><xsl:text>
             </xsl:text>
        </xsl:when>
      </xsl:choose>

	     </td><td align="left"><xsl:text>
             </xsl:text><em><xsl:call-template name="elementNotes"/></em><xsl:text>
             </xsl:text></td>
         </tr></table></dt><xsl:text>
         </xsl:text><dd>
    <xsl:choose>
      <xsl:when test="xs:annotation/xs:documentation">
        <xsl:value-of select="xs:annotation/xs:documentation."/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:for-each select="/xs:schema/xs:element[@name=$elname]/xs:annotation/xs:documentation[1]">
          <xsl:call-template name="briefDoc"/>
        </xsl:for-each>
      </xsl:otherwise>
    </xsl:choose>
    </dd><xsl:text>
       </xsl:text></dl></dd><xsl:text>
</xsl:text>
  </xsl:template>

  <xsl:template name="elementNotes">
    <xsl:choose>
      <xsl:when test="@minOccurs=0">
        <xsl:text>optional, </xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>required, </xsl:text>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:choose>
      <xsl:when test="@maxOccurs='unbounded'">
        <xsl:text>repeatable</xsl:text>
      </xsl:when>
      <xsl:when test="@maxOccurs">
        <xsl:text>max: </xsl:text>
	<xsl:value-of select="@maxOccurs"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>max: 1</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="extendedType">
    <xsl:param name="tpname" select="substring-after(@base, ':')"/>
    <dl><xsl:text>
      </xsl:text><dt><xsl:text>Content:</xsl:text></dt><xsl:text>
      </xsl:text><dd><table border="0" width="100%"><xsl:text>
      </xsl:text>
      <xsl:for-each select="/xs:schema/xs:complexType[@name=$tpname]">
        <xsl:call-template name="typeContents"/>
      </xsl:for-each>
      </table></dd><xsl:text>
    </xsl:text></dl>
  </xsl:template>

  <xsl:template name="briefTypeContents">
    <xsl:choose>
      <xsl:when test="xs:complexContent/xs:extension">
        <xsl:for-each select="xs:complexContent/xs:extension">
	  <xsl:variable name="tpname" select="substring-after(@base, ':')"/>
	  <xsl:for-each select="/xs:schema/xs:complexType[@name=$tpname]">
	    <xsl:call-template name="briefTypeContents"/>
	  </xsl:for-each>
	  <xsl:for-each select="xs:sequence/xs:element">
	    <xsl:choose>
	      <xsl:when test="@name">
	        <xsl:call-template name="briefElement">
	          <xsl:with-param name="elname" select="@name"/>
		</xsl:call-template>
	      </xsl:when>
	      <xsl:otherwise>
	        <xsl:call-template name="briefElement"/>
	      </xsl:otherwise>
	    </xsl:choose>
	  </xsl:for-each>
        </xsl:for-each>
      </xsl:when>
      <xsl:otherwise>
        <xsl:for-each select="xs:sequence/xs:element">
          <xsl:choose>
	    <xsl:when test="@ref">
	      <xsl:call-template name="briefElement"/>
      	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:call-template name="briefElement">
  	        <xsl:with-param name="elname" select="@name"/>
	      </xsl:call-template>
	    </xsl:otherwise>
          </xsl:choose>
        </xsl:for-each>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="typeAttributes">
    <xsl:choose>
      <xsl:when test="xs:complexContent/xs:extension">
        <xsl:for-each select="xs:complexContent/xs:extension">
	  <xsl:variable name="tpname" select="substring-after(@base, ':')"/>
	  <xsl:for-each select="/xs:schema/xs:complexType[@name=$tpname]">
	    <xsl:call-template name="typeAttributes"/>
	  </xsl:for-each>
	  <xsl:for-each select="xs:attribute">
	    <xsl:call-template name="attribute"/>
	  </xsl:for-each>
	</xsl:for-each>
      </xsl:when>
      <xsl:otherwise>
	<xsl:for-each select="xs:attribute">
	  <xsl:choose>
            <xsl:when test="@ref">
            <xsl:call-template name="attribute">
              <xsl:with-param name="attname" select="substring-after(@ref, ':')"/>
            </xsl:call-template>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:call-template name="attribute"/>
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:for-each>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="attribute">
    <xsl:param name="attname" select="@name"/>

    <xsl:text>  </xsl:text><dd><xsl:text> </xsl:text><dl><xsl:text> 
         </xsl:text><dt><table border="0" width="95%" cellspacing="0">
         <tr><xsl:text>
	     </xsl:text><td align="left" width="20%"><xsl:text>
             </xsl:text><strong>
             <xsl:value-of select="$attname"/>
   	     </strong><xsl:text>
	     </xsl:text></td><td align="left" width="35%">

      <xsl:choose>
        <xsl:when test="starts-with(@type, $schemaPrefix)">
          <xsl:text>
	     </xsl:text><strong><xsl:text>Type: </xsl:text></strong>
          <xsl:value-of select="substring-after(@type, ':')"/><xsl:text>
             </xsl:text>
        </xsl:when> 
        <xsl:when test="@type">
          <xsl:text>
	     </xsl:text><strong><xsl:text>Type: </xsl:text></strong>
          <a href="#tp:{substring-after(@type, ':')}">
          <xsl:value-of select="substring-after(@type, ':')"/></a><xsl:text>
             </xsl:text>
        </xsl:when>
      </xsl:choose>

             </td><td align="left"><xsl:text>
             </xsl:text><em><xsl:call-template name="attNotes"/></em><xsl:text>
             </xsl:text></td>
         </tr></table></dt><xsl:text>
         </xsl:text><dd>
    <xsl:choose>
      <xsl:when test="xs:annotation/xs:documentation">
        <xsl:value-of select="xs:annotation/xs:documentation"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:for-each select="/xs:schema/xs:attribute[@name=$attname]/xs:annotation/xs:documentation[1]">
          <xsl:call-template name="briefDoc"/>
        </xsl:for-each>
      </xsl:otherwise>
    </xsl:choose>
    </dd><xsl:text>
       </xsl:text></dl></dd><xsl:text>
</xsl:text>
  </xsl:template>

  <xsl:template name="attNotes">
    <xsl:choose>
      <xsl:when test="@use='fixed'">
        <xsl:value-of select="@use"/>
      </xsl:when>
      <xsl:when test="@use">
        <xsl:value-of select="@use"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>optional</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:if test="@default">
      <xsl:text>, default=</xsl:text>
      <xsl:value-of select="@default"/>
    </xsl:if>
  </xsl:template>

  <xsl:template name="typeDescription">
    <dl><xsl:text>
  </xsl:text><dt><xsl:text>Content:</xsl:text></dt><xsl:text>
</xsl:text>
    <xsl:call-template name="briefTypeContents"/>
    <xsl:text>  </xsl:text><dt><xsl:text>Attributes:</xsl:text></dt><xsl:text>
</xsl:text>
    <xsl:call-template name="typeAttributes"/>
    </dl><xsl:text>
</xsl:text>    
  </xsl:template>

<!-- ***************************************** -->
  <xsl:template name="originalType">
    <dl>
    <dt><xsl:text>Content:</xsl:text></dt>
    <xsl:for-each select="xs:sequence/xs:element">
      <dd>
      <xsl:choose>
      <xsl:when test="@name">
        <xsl:call-template name="subElement"/>
      </xsl:when>
      <xsl:when test="@ref">
        <a href="#el:{substring-after(@ref,':')}"><xsl:value-of select="substring-after(@ref,':')"/></a><br/>
      </xsl:when>
      </xsl:choose>
      </dd>
    </xsl:for-each>
    <xsl:if test="xs:attribute">
         <dt><xsl:text>Attributes:</xsl:text></dt>
      <xsl:for-each select="xs:attribute">
      <dd>
          <xsl:value-of select="@name"/>
          <xsl:value-of select="@ref"/>
           <xsl:text> (type:  </xsl:text>
           <xsl:choose>
      <xsl:when test="starts-with(@ref, $schemaPrefix)">
          <xsl:value-of select="substring-after(@ref, ':')"/>
      </xsl:when>
                <xsl:when test="@ref">
                    <a href="#at:{substring-after(@ref, ':')}"><xsl:value-of select="substring-after(@ref, ':')"/></a>
                </xsl:when>
      <xsl:when test="starts-with(@type, $schemaPrefix)">
          <xsl:value-of select="substring-after(@type, ':')"/>
      </xsl:when>
      <xsl:when test="contains(@type,':')">
          <a href="#tp:{substring-after(@type,':')}"><xsl:value-of select="substring-after(@type,':')"/></a>
      </xsl:when>
      <xsl:otherwise>
          <xsl:value-of select="@type"/>
      </xsl:otherwise>
      </xsl:choose>
           <xsl:text>)</xsl:text>
           <xsl:if test="xs:annotation/xs:documentation">
              <xsl:text>:  </xsl:text>
              <xsl:for-each select="xs:annotation/xs:documentation">
                <xsl:value-of select="."/><xsl:text>  </xsl:text>
              </xsl:for-each>
           </xsl:if>
         </dd>
       </xsl:for-each>
    </xsl:if>
    </dl>
  </xsl:template>
  
  <xsl:template name="subElement">
    <dl>
      <dt><xsl:value-of select="@name"/>
            <xsl:text> (type:  </xsl:text><xsl:value-of select="substring-after(@type, $schemaPrefix)"/><xsl:text>)</xsl:text>
            <xsl:if test="xs:annotation/xs:documentation">
                <xsl:text>:  </xsl:text>
                <xsl:for-each select="xs:annotation/xs:documentation">
                  <xsl:value-of select="."/><xsl:text>  </xsl:text>
              </xsl:for-each>
            </xsl:if>
      </dt>
    </dl>
  </xsl:template>
  
  <xsl:template match="xs:element">
<xsl:text>
</xsl:text><dl><xsl:text>
  </xsl:text>
    <dt><table border="0" width="95%" cellspacing="0"><tr><xsl:text>
      </xsl:text><td align="left" width="30%">
      <font size="+1">
      <a name="el:{@name}"><strong><xsl:value-of select="@name"/></strong></a>
      </font><xsl:text>
      </xsl:text></td><td align="left">

      <xsl:choose>
        <xsl:when test="starts-with(@type, $schemaPrefix)">
          <xsl:text>
	     </xsl:text><strong><xsl:text>Type: </xsl:text></strong>
          <xsl:value-of select="substring-after(@type, ':')"/><xsl:text>
             </xsl:text>
        </xsl:when> 
        <xsl:when test="@type">
          <xsl:text>
	     </xsl:text><strong><xsl:text>Type: </xsl:text></strong>
          <a href="#tp:{substring-after(@type, ':')}">
          <xsl:value-of select="substring-after(@type, ':')"/></a><xsl:text>
             </xsl:text>
        </xsl:when>
      </xsl:choose>

	     </td></tr></table></dt><xsl:text>
    </xsl:text><dd>
    <xsl:for-each select="xs:annotation/xs:documentation">
      <xsl:value-of select="normalize-space(.)"/> <p/>
      <xsl:text>
      
</xsl:text>
    </xsl:for-each>
    <xsl:text>
</xsl:text><p/><xsl:text>
</xsl:text>

    <xsl:choose>
      <xsl:when test="starts-with(@type, $targetPrefix)">
        <xsl:variable name="tpname" select="substring-after(@type, ':')"/>
        <xsl:for-each select="/xs:schema/xs:complexType[@name=$tpname]">
          <xsl:call-template name="typeDescription"/>
        </xsl:for-each>
      </xsl:when>
      <xsl:otherwise>
        <xsl:for-each select="xs:complexType[1]">
          <xsl:call-template name="typeDescription"/>
        </xsl:for-each>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:text>
  </xsl:text></dd><xsl:text>
</xsl:text></dl><hr/><xsl:text>
</xsl:text>
  </xsl:template>
  
  <xsl:template match="xs:simpleType">
    <dt><a name="tp:{@name}"><xsl:value-of select="@name"/></a></dt>
    <dd>
    <xsl:if test="xs:annotation/xs:documentation">
       <xsl:for-each select="xs:annotation/xs:documentation">
                  <xsl:value-of select="."/><xsl:text>  </xsl:text>
              </xsl:for-each> <br/>
    </xsl:if>
    <xsl:apply-templates select="xs:restriction"/>
    </dd><p/>
  </xsl:template>
  
  <xsl:template match="xs:complexType">
    <dt><a name="tp:{@name}"><xsl:value-of select="@name"/></a></dt>
    <dd>
    <xsl:for-each select="xs:annotation/xs:documentation">
                 <xsl:value-of select="."/><xsl:text>  </xsl:text>
         </xsl:for-each> 
    <xsl:if test="xs:annotation/xs:documentation and (xs:sequence/xs:element or xs:attribute)"><br/></xsl:if>
    <xsl:call-template name="originalType"/>
    </dd><p/>
  </xsl:template>
  
  <xsl:template match="/">
    <html>
      <head>
        <title>Dictionary</title>
      </head>
      <body bgcolor="white">
        <h1>Dictionary for <xsl:value-of select="xs:schema/xs:annotation/xs:appinfo/shortName"/></h1>
        <xsl:value-of select="xs:schema/xs:annotation/xs:documentation"/>
        <xsl:apply-templates select="xs:schema/xs:element">
          <xsl:sort select="@name"/>
        </xsl:apply-templates>
        <h3>Reusable Types</h3>
        <dl>
        <xsl:apply-templates select="xs:schema/xs:simpleType"/>
        <xsl:apply-templates select="xs:schema/xs:complexType"/>
        </dl>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
