File:  [LON-CAPA] / modules / damieng / schema_documentation / XMLSchema.xsl
Revision 1.2: download - view: text, annotated - select for diffs
Wed Jan 18 18:46:10 2017 UTC (7 years, 3 months ago) by damieng
Branches: MAIN
CVS tags: HEAD
misc documentation stylesheet improvements

<?xml version="1.0" encoding='UTF-8' ?>

<xsl:stylesheet version="1.0"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                exclude-result-prefixes="xs">
  
  <xsl:output method="html" indent="yes"
              doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"/>

    <xsl:param name="titre"/>
    <xsl:param name="jaxe-uri-xsl"/>
    <xsl:variable name="uri-xsl"><xsl:call-template name="rep-uri"><xsl:with-param name="chemin" select="$jaxe-uri-xsl"/></xsl:call-template></xsl:variable>
    <xsl:variable name="langue">
      <xsl:choose>
          <xsl:when test="/xs:schema/@xml:lang"><xsl:value-of select="translate(/xs:schema/@xml:lang, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')"/></xsl:when>
          <xsl:otherwise>fr</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="messages" select="document(concat($uri-xsl, '/XMLSchema_messages_', $langue, '.xml'))/messages"/>
    
  <xsl:template match="xs:schema">
    <html>
      <head>
        <title><xsl:value-of select="$titre"/></title>
        <style type="text/css">
body { color: #000000; background: #FFFFFF }
.documentation { font-family: monospace; color: #005500; white-space: pre-line; margin-bottom: 1em }
.grandlien { font-size: 120%; font-weight: bold }
.nomtitre { color: #550000 }
.nomattribut { color: #000055; font-weight: bold }
.indentation { margin-left: 2em; margin-top: 1em; margin-bottom: 1em }
ul { margin-top: 0.5em }
        </style>
      </head>
      <body>
        <div align="center">
        <h1><xsl:value-of select="$titre"/></h1>
        </div>
        <br/>
        <span class="grandlien">
        <a href="#index"><xsl:value-of select="$messages/message[@label='Aller à l index']"/></a><br/>
        </span>
        <hr/>
        <xsl:apply-templates/>
        <br/>
        <a name="index"/>
        <h3><xsl:value-of select="$messages/message[@label='Index']"/></h3>
        <xsl:for-each select="//*[local-name()!='attribute' and @name!='']">
          <xsl:sort select="@name"/>
          <xsl:variable name="numero" select="count(preceding::*[@name=current()/@name])"/>
          <xsl:variable name="total" select="count(//*[@name=current()/@name])"/>
          <a href="#{@name}_{$numero}"><xsl:value-of select="@name"/></a>
          <xsl:if test="$total &gt; 1 and ancestor::*[@name!=''][1]">
            (<xsl:value-of select="$messages/message[@label='sous']"/><xsl:text> </xsl:text>
            <xsl:value-of select="ancestor::*[@name!=''][1]/@name"/>)
          </xsl:if>
          <br/>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>
  
  <xsl:template match="xs:element">
    <xsl:choose>
    <xsl:when test="@ref">
      <xsl:variable name="localref">
        <xsl:choose>
          <xsl:when test="contains(@ref,':')">
            <xsl:value-of select="substring-after(@ref,':')"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="@ref"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:variable name="numero">
        <xsl:for-each select="/xs:schema/xs:element[@name=$localref][1]">
          <xsl:value-of select="count(preceding::*[@name=current()/@name])"/>
        </xsl:for-each>
      </xsl:variable>
      <a href="#{$localref}_{$numero}"><xsl:value-of select="@ref"/></a>
      <br/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:variable name="numero" select="count(preceding::*[@name=current()/@name])"/>
      <a name="{@name}_{$numero}"/>
      <h3><xsl:value-of select="$messages/message[@label='Elément']"/><xsl:text> </xsl:text>
      <span class="nomtitre"><xsl:value-of select="@name"/></span></h3>
      <xsl:if test="@type">
        <xsl:value-of select="$messages/message[@label='type:']"/><xsl:text> </xsl:text>
        <xsl:call-template name="nomtype">
          <xsl:with-param name="type" select="@type"/>
        </xsl:call-template>
        <br/>
      </xsl:if>
      <xsl:apply-templates/>
      <br/>
      <xsl:if test="parent::xs:schema">
        <xsl:for-each select="//xs:element[.//xs:element[@ref=current()/@name or substring-after(@ref,':')=current()/@name]] | //xs:group[.//xs:element[@ref=current()/@name or substring-after(@ref,':')=current()/@name]]">
          <xsl:if test="position() = 1">
            <xsl:value-of select="$messages/message[@label='Parents:']"/><xsl:text> </xsl:text>
          </xsl:if>
          <xsl:if test="position() != 1">, </xsl:if>
          <xsl:variable name="numero2" select="count(preceding::*[@name=current()/@name])"/>
          <a href="#{@name}_{$numero2}"><xsl:value-of select="@name"/></a> 
        </xsl:for-each>
        <br/>
      </xsl:if>
      <xsl:if test="not(ancestor::xs:choice or ancestor::xs:sequence)">
        <hr/>
      </xsl:if>
    </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="xs:group">
    <xsl:choose>
    <xsl:when test="@ref">
      <xsl:variable name="localref">
        <xsl:choose>
          <xsl:when test="contains(@ref,':')">
            <xsl:value-of select="substring-after(@ref,':')"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="@ref"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:variable name="numero">
        <xsl:for-each select="/xs:schema/xs:group[@name=$localref][1]">
          <xsl:value-of select="count(preceding::*[@name=current()/@name])"/>
        </xsl:for-each>
      </xsl:variable>
      <a href="#{$localref}_{$numero}"><xsl:value-of select="@ref"/></a>
      <br/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:variable name="numero" select="count(preceding::*[@name=current()/@name])"/>
      <a name="{@name}_{$numero}"/>
      <h3><xsl:value-of select="$messages/message[@label='Groupe']"/><xsl:text> </xsl:text>
      <span class="nomtitre"><xsl:value-of select="@name"/></span></h3>
      <xsl:apply-templates/>
      <br/>
      <xsl:for-each select="//xs:element[.//xs:group[@ref=current()/@name or substring-after(@ref,':')=current()/@name]] | //xs:group[.//xs:group[@ref=current()/@name or substring-after(@ref,':')=current()/@name]]">
        <xsl:if test="position() = 1">
          <xsl:value-of select="$messages/message[@label='Parents:']"/><xsl:text> </xsl:text>
        </xsl:if>
        <xsl:if test="position() != 1">, </xsl:if>
        <xsl:variable name="numero2" select="count(preceding::*[@name=current()/@name])"/>
        <a href="#{@name}_{$numero2}"><xsl:value-of select="@name"/></a> 
      </xsl:for-each>
      <br/>
      <hr/>
    </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="xs:documentation">
    <div class="documentation"><xsl:choose>
      <xsl:when test="starts-with(., '&#10;')">
        <xsl:value-of select="substring-after(., '&#10;')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="."/>
      </xsl:otherwise>
    </xsl:choose></div>
    <xsl:if test="parent::xs:annotation/parent::xs:schema">
      <hr/>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="xs:choice">
    <xsl:value-of select="$messages/message[@label='Choix parmi:']"/>
    <ul>
      <xsl:for-each select="*">
        <li><xsl:apply-templates select="."/></li>
      </xsl:for-each>
    </ul>
  </xsl:template>
  
  <xsl:template match="xs:sequence">
    <xsl:value-of select="$messages/message[@label='Séquence parmi:']"/>
    <ul>
      <xsl:for-each select="*">
        <li><xsl:apply-templates select="."/></li>
      </xsl:for-each>
    </ul>
  </xsl:template>
  
  <xsl:template match="xs:complexType">
    <!--Type complexe-->
    <xsl:if test="@name">
      <xsl:variable name="numero" select="count(preceding::*[@name=current()/@name])"/>
      <a name="{@name}_{$numero}"/>
      <h3><xsl:value-of select="$messages/message[@label='Type complexe']"/><xsl:text> </xsl:text>
      <span class="nomtitre"><xsl:value-of select="@name"/></span></h3>
    </xsl:if>
    <xsl:if test="@ref">
      <xsl:variable name="numero" select="count(preceding::*[@name=current()/@name])"/>
      <a href="{@ref}_{$numero}"><xsl:value-of select="@ref"/></a><br/>
    </xsl:if>
    <xsl:if test="@mixed='true'">
      <xsl:value-of select="$messages/message[@label='Peut contenir du texte']"/><br/>
    </xsl:if>
    <div class="indentation">
      <xsl:apply-templates/>
    </div>
    <xsl:if test="@name">
      <hr/>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="xs:attribute">
    <xsl:value-of select="$messages/message[@label='Attribut']"/><xsl:text> </xsl:text>
    <span class="nomattribut"><xsl:value-of select="@name"/></span>:
    <div class="indentation">
    <xsl:choose>
    <xsl:when test="@use='required'">
      <xsl:value-of select="$messages/message[@label='obligatoire']"/><br/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$messages/message[@label='facultatif']"/><br/>
    </xsl:otherwise>
    </xsl:choose>
    <xsl:if test="@type">
      <xsl:value-of select="$messages/message[@label='type:']"/><xsl:text> </xsl:text>
      <xsl:call-template name="nomtype">
        <xsl:with-param name="type" select="@type"/>
      </xsl:call-template>
      <br/>
    </xsl:if>
    <xsl:apply-templates/>
    </div>
  </xsl:template>
  
  <xsl:template match="xs:simpleType">
    <xsl:choose>
      <xsl:when test="@name!=''">
        <xsl:variable name="numero" select="count(preceding::*[@name=current()/@name])"/>
        <a name="{@name}_{$numero}"/>
        <h3><xsl:value-of select="$messages/message[@label='Type simple']"/><xsl:text> </xsl:text>
        <span class="nomtitre"><xsl:value-of select="@name"/></span></h3>
        <xsl:apply-templates/>
        <hr/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template match="xs:restriction">
    <xsl:value-of select="$messages/message[@label='Restriction des valeurs']"/>
    <xsl:if test="@base!=''">
      <xsl:text> </xsl:text>
      <xsl:value-of select="$messages/message[@label='basée sur']"/>
      <xsl:text> </xsl:text>
      <xsl:value-of select="@base"/>
    </xsl:if>
    <xsl:if test="*">
      <ul>
        <xsl:apply-templates/>
      </ul>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="xs:union">
    <xsl:value-of select="$messages/message[@label='Union des types']"/>
    <ul>
      <xsl:if test="@memberTypes!=''">
        <xsl:call-template name="union-members">
          <xsl:with-param name="types" select="@memberTypes"/>
        </xsl:call-template>
      </xsl:if>
      <xsl:for-each select="xs:simpleType">
        <li><xsl:apply-templates select="."/></li>
      </xsl:for-each>
    </ul>
  </xsl:template>
  
  <xsl:template match="xs:enumeration">
    <li><tt><xsl:value-of select="@value"/></tt></li>
  </xsl:template>
  
  <xsl:template match="xs:pattern">
    <li><tt><xsl:value-of select="@value"/></tt></li>
  </xsl:template>
  
  <xsl:template match="xs:minInclusive">
    <li><tt>&gt;= <xsl:value-of select="@value"/></tt></li>
  </xsl:template>
  
  <xsl:template match="xs:minExclusive">
    <li><tt>&gt; <xsl:value-of select="@value"/></tt></li>
  </xsl:template>
  
  <xsl:template match="xs:maxInclusive">
    <li><tt>&lt;= <xsl:value-of select="@value"/></tt></li>
  </xsl:template>
  
  <xsl:template match="xs:maxExclusive">
    <li><tt>&lt; <xsl:value-of select="@value"/></tt></li>
  </xsl:template>
  
  <xsl:template match="xs:attributeGroup">
    <xsl:choose>
    <xsl:when test="@ref">
      <xsl:variable name="localref">
        <xsl:choose>
          <xsl:when test="contains(@ref,':')">
            <xsl:value-of select="substring-after(@ref,':')"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="@ref"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:variable name="numero">
        <xsl:for-each select="/xs:schema/xs:attributeGroup[@name=$localref][1]">
          <xsl:value-of select="count(preceding::*[@name=current()/@name])"/>
        </xsl:for-each>
      </xsl:variable>
      <a href="#{$localref}_{$numero}"><xsl:value-of select="@ref"/></a>
      <br/>
      <br/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:variable name="numero" select="count(preceding::*[@name=current()/@name])"/>
      <a name="{@name}_{$numero}"/>
      <h3><xsl:value-of select="$messages/message[@label='Groupe d attributs:']"/><xsl:text> </xsl:text>
      <span class="nomtitre"><xsl:value-of select="@name"/></span></h3>
      <xsl:apply-templates/>
      <br/>
      <xsl:for-each select="//xs:element[.//xs:attributeGroup[@ref=current()/@name or substring-after(@ref,':')=current()/@name]] | //xs:attributeGroup[.//xs:attributeGroup[@ref=current()/@name or substring-after(@ref,':')=current()/@name]] | //xs:complexType[.//xs:attributeGroup[@ref=current()/@name or substring-after(@ref,':')=current()/@name] and @name]">
        <xsl:if test="position() = 1">
          <xsl:value-of select="$messages/message[@label='Parents:']"/><xsl:text> </xsl:text>
        </xsl:if>
        <xsl:if test="position() != 1">, </xsl:if>
        <xsl:variable name="numero2" select="count(preceding::*[@name=current()/@name])"/>
        <a href="#{@name}_{$numero2}"><xsl:value-of select="@name"/></a> 
      </xsl:for-each>
      <br/>
      <hr/>
    </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="xs:import">
  </xsl:template>
  
  <xsl:template name="nomtype">
    <xsl:param name="type"/>
    <xsl:variable name="localType">
      <xsl:choose>
        <xsl:when test="contains($type,':')">
          <xsl:value-of select="substring-after($type,':')"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$type"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:choose>
        <xsl:when test="/xs:schema/xs:simpleType[@name=$localType]|/xs:schema/xs:complexType[@name=$localType]">
          <xsl:for-each select="/xs:schema/xs:simpleType[@name=$localType]|/xs:schema/xs:complexType[@name=$localType]">
            <xsl:variable name="numero" select="count(preceding::*[@name=current()/@name])"/>
            <a href="#{@name}_{$numero}"><xsl:value-of select="@name"/></a>
          </xsl:for-each>
        </xsl:when>
        <xsl:otherwise>
          <tt><xsl:value-of select="$type"/></tt>
        </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template name="rep-uri">
    <!-- renvoit le chemin du répertoire d'un fichier à partir de l'URI complète, sous forme d'URI  -->
    <xsl:param name="chemin"/>
    <!-- séparateur de chemins ( normalement / mais parfois \ avec file:// sur Windows ? ) -->
    <xsl:variable name="sepuri"><xsl:choose>
      <xsl:when test="contains($chemin, '\')">\</xsl:when>
      <xsl:otherwise>/</xsl:otherwise>
    </xsl:choose></xsl:variable>
    <xsl:choose>
      <xsl:when test="contains($chemin,$sepuri) and contains(substring-after($chemin,$sepuri),$sepuri)">
        <xsl:value-of select="substring-before($chemin,$sepuri)"/><xsl:value-of select="$sepuri"/><xsl:call-template name="rep-uri"><xsl:with-param name="chemin" select="substring-after($chemin,$sepuri)"/></xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="substring-before($chemin,$sepuri)"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
    
  <xsl:template name="union-members">
    <xsl:param name="types"/>
    <xsl:variable name="nt" select="normalize-space($types)"/>
    <xsl:if test="$nt!=''">
      <xsl:choose>
        <xsl:when test="contains($nt,' ')">
          <xsl:call-template name="union-members">
            <xsl:with-param name="types" select="substring-before($nt, ' ')"/>
          </xsl:call-template>
          <xsl:call-template name="union-members">
            <xsl:with-param name="types" select="substring-after($nt, ' ')"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <li>
            <xsl:call-template name="nomtype">
              <xsl:with-param name="type" select="$nt"/>
            </xsl:call-template>
          </li>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:if>
  </xsl:template>
 
</xsl:stylesheet>

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>