<?xml version="1.0" encoding="UTF-8"?>
<!--
# XSLT stylesheet for VOTable-sosyson
# Version: 1.4
# Author: J. Berthier - IMCCE
# Date: November, 2006
#
# Changelog:
#   1.4 (2006-11-25)
#          Ajout des hyperliens sur les references croises des objets...
#   1.3 (2006-11-01)
#          Affichage vignette 'snapshot' pour les donnees spectrales...
#   1.2 (2006-10-16)
#          Perfectionnement et raffinement...
#   1.1 (2006-10-15)
#          Adaptation pour SOSYSON
#   1.0 (2006-10-13)
           Download from http://us-vo.org/xml/VOTable-basic.xsl
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" xmlns:VOT="http://www.ivoa.net/xml/VOTable/v1.1">

  <xsl:output method="html" indent="yes" encoding="UTF-8" />

  <!-- ======================================================== -->
  <!-- Transformation du document -->
  <!-- ======================================================== -->

  <xsl:template match="/">
    <xsl:element name="html">
      <xsl:element name="head">
        <xsl:apply-templates select="VOTABLE/RESOURCE" mode="head" />
      </xsl:element>
      <xsl:element name="body">
        <xsl:call-template name="intro" />
        <xsl:call-template name="data">
          <xsl:with-param name="tables">
            <xsl:apply-templates select="VOTABLE/RESOURCE/PARAM" mode="tables" />
          </xsl:with-param>
        </xsl:call-template>
      </xsl:element>
    </xsl:element>
  </xsl:template>

  <!-- ======================================================== -->

  <!-- Creation de l'entete de la page -->
  <xsl:template mode="head" match="RESOURCE">
    <xsl:element name="title">
      <xsl:value-of select="@name" />
    </xsl:element>
    <xsl:element name="meta">
      <xsl:attribute name="http-equiv">Content-Type</xsl:attribute>
      <xsl:attribute name="content">text/xhtml; charset=UTF-8</xsl:attribute>
    </xsl:element>
    <xsl:element name="link">
      <xsl:attribute name="rel">stylesheet</xsl:attribute>
      <xsl:attribute name="type">text/css</xsl:attribute>
      <xsl:attribute name="href">/webservices/sosyson/sosyson.css</xsl:attribute>
    </xsl:element>
  </xsl:template>

  <!-- Creation du titre de la page -->
  <xsl:template mode="head" match="PARAM">
    <xsl:if test="@name!='id' and @name!='tables'">
      <xsl:value-of select="concat(@value,' ')" />
    </xsl:if>
  </xsl:template>

  <!-- Creation de la liste des tables demandees -->
  <xsl:template mode="tables" match="PARAM">
    <xsl:if test="@name='tables'">
      <xsl:value-of select="@value" />
    </xsl:if>
  </xsl:template>

  <!-- Creation du texte de legende des tables -->
  <xsl:template name="write_legend">
    <xsl:for-each select="DESCRIPTION">
      <xsl:element name="legend">
        <xsl:value-of select="." />
      </xsl:element>
    </xsl:for-each>
  </xsl:template>

  <!-- ======================================================== -->

  <!-- Creation du titre -->
  <xsl:template name="intro">
    <div class="titre_intro">
      <table width="100%">
        <tr>
          <td class="titre">
            <xsl:apply-templates select="VOTABLE/RESOURCE/PARAM" mode="head" />
          </td>
        </tr>
      </table>
    </div>
  </xsl:template>

  <!--  Creation des tables DATA -->
  <xsl:template name="data">
    <xsl:param name="tables" />
    <xsl:element name="div">
      <xsl:attribute name="class">data</xsl:attribute>
      <xsl:for-each select="VOTABLE">
        <xsl:for-each select="RESOURCE">
          <xsl:for-each select="TABLE">
            <xsl:element name="p">
              <xsl:element name="fieldset">
                <xsl:call-template name="write_legend" />
                <xsl:element name="table">
                  <!-- Title -->
                  <xsl:element name="tr">
                    <xsl:for-each select="FIELD">
                      <xsl:element name="th">
                        <xsl:attribute name="class">title</xsl:attribute>
                        <xsl:element name="a">
                          <xsl:attribute name="class">info</xsl:attribute>
                          <xsl:attribute name="href">#</xsl:attribute>
                          <xsl:value-of select="@name" />
                          <xsl:element name="div">
                            <xsl:for-each select="DESCRIPTION">
                              <xsl:value-of select="." />
                            </xsl:for-each>
                          </xsl:element>
                        </xsl:element>
                      </xsl:element>
                      <xsl:for-each select="LINK">
                        <xsl:variable name="flink">
                          <xsl:value-of select="@href" />
                        </xsl:variable>
                      </xsl:for-each>
                    </xsl:for-each>
                  </xsl:element>
                  <!-- subTitle -->
                  <xsl:element name="tr">
                    <xsl:for-each select="FIELD">
                      <xsl:element name="th">
                        <xsl:attribute name="class">subtitle</xsl:attribute>
                        <xsl:value-of select="@unit" />
                      </xsl:element>
                    </xsl:for-each>
                  </xsl:element>
                  <!-- Data -->
                  <xsl:for-each select="DATA">
                    <xsl:for-each select="TABLEDATA">
                      <xsl:for-each select="TR">
                        <xsl:element name="tr">
                          <xsl:attribute name="class">impair</xsl:attribute>
                          <xsl:attribute name="onmouseover">this.className='over'</xsl:attribute>
                          <xsl:attribute name="onmouseout">this.className='impair'</xsl:attribute>
                          <xsl:attribute name="onclick">this.className='pair'</xsl:attribute>
                          <xsl:for-each select="TD">
                            <xsl:variable name="col" select="position()" />
                            <xsl:variable name="option">
                              <xsl:text disable-output-escaping="yes">&amp;snapshot</xsl:text>
                            </xsl:variable>
                            <xsl:variable name="mime">
                              <xsl:text disable-output-escaping="yes">&amp;-schema=1</xsl:text>
                            </xsl:variable>
                            <xsl:variable name="optabs">
                              <xsl:text disable-output-escaping="yes">&amp;-tables=</xsl:text>
                              <xsl:value-of select="$tables" />
                            </xsl:variable>
                            <xsl:element name="td">
                              <xsl:choose>
                                <!-- Gestion des hyperliens dans les tables -->
                                <xsl:when test="ancestor::*/child::FIELD[position()=$col]/LINK[@type='location']">
                                  <xsl:choose>
                                    <!-- Vignette et hyperliens pour les noeuds d'attribut Snapshot (SMASS par ex.) -->
                                    <xsl:when test="ancestor::*/child::FIELD[position()=$col][@name='Snapshot']">
                                      <xsl:element name="a">
                                        <xsl:attribute name="href">
                                          <xsl:value-of select="ancestor::*/child::FIELD[position()=$col]/LINK/@href" />
                                          <xsl:value-of select="." />
                                        </xsl:attribute>
                                        <xsl:attribute name="target">_blank</xsl:attribute>
                                        <xsl:element name="img">
                                          <xsl:attribute name="src">
                                            <xsl:value-of select="ancestor::*/child::FIELD[position()=$col]/LINK/@href" />
                                            <xsl:value-of select="." />
                                            <xsl:value-of select="$option" />
                                          </xsl:attribute>
                                          <xsl:attribute name="alt">Spectrum snapshot</xsl:attribute>
                                          <xsl:attribute name="style">border-style:hidden;</xsl:attribute>
                                        </xsl:element>
                                      </xsl:element>
                                    </xsl:when>
                                    <xsl:otherwise>
                                      <xsl:choose>
                                        <!-- Hyperlien generique pour les champs Data -->
                                        <xsl:when test="ancestor::*/child::FIELD[position()=$col][@name='Data']">
                                          <xsl:element name="a">
                                            <xsl:attribute name="href">
                                              <xsl:value-of select="substring-before(ancestor::*/child::FIELD[position()=$col]/LINK/@href,'${ID}')" />
                                              <xsl:value-of select="preceding::TD[position()=1]" />
                                            </xsl:attribute>
                                            <xsl:attribute name="target">_blank</xsl:attribute>
                                            <xsl:value-of select="." />
                                          </xsl:element>
                                        </xsl:when>
                                        <xsl:otherwise>
                                          <xsl:choose>
                                            <!-- Hyperlien specifique dans la table satellites -->
                                            <xsl:when test="ancestor::*/child::TABLE[position()=$col][@name='satellites']">
                                              <xsl:element name="a">
                                                <xsl:attribute name="href">
                                                  <xsl:value-of select="substring-before(ancestor::*/child::FIELD[position()=$col]/LINK/@href,'${ID}')" />
                                                  <xsl:value-of select="preceding::TD[position()=1]" />
                                                  <xsl:value-of select="$mime" />
                                                  <xsl:value-of select="$optabs" />
                                                </xsl:attribute>
                                                <xsl:attribute name="target">_blank</xsl:attribute>
                                                <xsl:value-of select="." />
                                              </xsl:element>
                                            </xsl:when>
                                            <!-- Hyperlien generique dans les tables -->
                                            <xsl:otherwise>
                                              <xsl:element name="a">
                                                <xsl:attribute name="href">
                                                  <xsl:value-of select="ancestor::*/child::FIELD[position()=$col]/LINK/@href" />
                                                  <xsl:value-of select="$mime" />
                                                  <xsl:value-of select="$optabs" />
                                                </xsl:attribute>
                                                <xsl:attribute name="target">_blank</xsl:attribute>
                                                <xsl:value-of select="." />
                                              </xsl:element>
                                            </xsl:otherwise>
                                          </xsl:choose>
                                        </xsl:otherwise>
                                      </xsl:choose>
                                    </xsl:otherwise>
                                  </xsl:choose>
                                </xsl:when>
                                <!-- Ou affichage des donnees -->
                                <xsl:otherwise>
                                  <xsl:value-of select="." />
                                </xsl:otherwise>
                              </xsl:choose>
                            </xsl:element>
                          </xsl:for-each>
                        </xsl:element>
                      </xsl:for-each>
                    </xsl:for-each>
                  </xsl:for-each>
                </xsl:element>
              </xsl:element>
            </xsl:element>
          </xsl:for-each>
        </xsl:for-each>
      </xsl:for-each>
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>

