﻿<?xml version="1.0" encoding="UTF-8"?>
<!--
This file is part of IREL, "Instruments de Recherche En Ligne",
<http://www.archivesnationales.culture.gouv.fr/caom/fr/index_irel.html>.
Copyright © 2008, 2009 : ANOM, PASS-TECH

Archives nationales d’outre-mer (ANOM, France)
29, chemin du moulin Detesta
13090 Aix-en-Provence
<http://www.archivesnationales.culture.gouv.fr/caom/fr/>
anom.aix@culture.gouv.fr

PASS-TECH
26, rue Louis Braille
F-75012 Paris
<http://pass-tech.fr/>
contact@pass-tech.fr

IREL is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

IREL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with IREL application; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

$Date: 2008-09-06 17:25:29 +0200 (sam., 06 sept. 2008) $:
$Author: frederic $:
$Rev: 350 $:
-->
<!--
Cette transformation indexe une notice XML/EAC
vers un CSV indexable dans mysql
Le modèle s'inspire de
http://www.pleade.org/fr/documentation/descriptions/EACtoDC.html

Il sortira une ligne par document

Colonne SQL dans l'ordre

id, type, title, description, text, eacrels, eadrels

-->
<xsl:transform version="1.1"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <!-- plusieurs templates partagés -->
  <xsl:import href="eac2html.xsl"/>
  <xsl:output indent="no" method="xml" encoding="UTF-8" omit-xml-declaration="yes"/>
  <!-- mode, pourra être utilisé pour indexer les relations -->
  <xsl:param name="mode"/>
  <!-- id, on prendra le nom de fichier comme identifiant, passé par l'appeleur
  l'eacid n'est pas obligatoire ni d'unicité garantie -->
  <xsl:param name="id"/>
  <xsl:variable name="apos">'</xsl:variable>
  <xsl:variable name="quot">"</xsl:variable>
  <!-- séparateur de ligne,  -->
  <xsl:variable name="sql-line" select="'&#10;'"/>
  <!-- séparateur de champ, tabulation -->
  <xsl:variable name="sql-field" select="'&#9;'"/>
  <!-- pour échappement sql (attention ce ne sont que des espace, les mettre entre apos)
    passé maintenant en fonction php d'analyse  -->
  <xsl:variable name="sql-from" select="'&#13;&#10;&#9;'"/>
  <xsl:variable name="sql-to" select="'   '"/>

  <!--
Une ligne :  id, type, name, title, description, text, start, end, eacrels, eadrels
-->
  <xsl:template match="eac">
    <!-- on pourrait mettre une logique plus fine pour l'id (ex traitement de plusieurs fichiers EAC dans un <condescgrp>
    <xsl:value-of select="translate($id, concat(' ?#*/\:;|&gt;&lt;', $apos, $quot), '_______________')"/>
    -->
    <xsl:value-of select="$id"/>
    <xsl:value-of select="$sql-field"/>
    <xsl:value-of select="@type"/>
    <!-- le nom principal pour recherche -->
    <xsl:value-of select="$sql-field"/>
    <xsl:variable name="title">
      <xsl:apply-templates select="condesc/identity" mode="title"/>
    </xsl:variable>
    <!-- sortir s'il y a des parenthèses ? -->
    <xsl:choose>
      <xsl:when test="contains($title, '(')">
        <xsl:value-of select="normalize-space(substring-before($title, '('))"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="normalize-space($title)"/>
      </xsl:otherwise>
    </xsl:choose>
    <!-- une ligne de titre avec dates -->
    <xsl:value-of select="$sql-field"/>
    <xsl:value-of select="$title"/>
    <!-- description extraite pour résultat bref, en html -->
    <xsl:value-of select="$sql-field"/>
    <xsl:call-template name="description"/>
    <!-- plein texte -->
    <xsl:value-of select="$sql-field"/>
    <xsl:apply-templates mode="text"/>
    <xsl:variable name="period">
      <xsl:call-template name="period">
        <xsl:with-param name="nodeset" select="condesc/desc/persdesc/existdesc/existdate/@normal | condesc/desc/persdesc/funactdesc//date/@normal"/>
      </xsl:call-template>
    </xsl:variable>
    <!-- start, première date possible -->
    <xsl:value-of select="$sql-field"/>
    <xsl:value-of select="substring-before($period, '/')"/>
    <xsl:text>-00-00</xsl:text>
    <!-- end, dernière date possible -->
    <xsl:value-of select="$sql-field"/>
    <xsl:value-of select="substring-after($period, '/')"/>
    <xsl:text>-00-00</xsl:text>
    <!-- relation internes inrel -->
    <xsl:value-of select="$sql-field"/>
    <xsl:for-each select="condesc/eacrels/eacrel/@syskey">
      <xsl:value-of select="substring-before(concat(., '.xml'), '.xml')"/>
      <xsl:text> </xsl:text>
    </xsl:for-each>
    <!-- relations externes exrel -->
    <xsl:value-of select="$sql-field"/>
    <xsl:for-each select="condesc/resourcerels/resourcerel/@syskey">
      <xsl:value-of select="substring-before(concat(., '.xml'), '.xml')"/>
      <xsl:text> </xsl:text>
    </xsl:for-each>
  </xsl:template>

  <!-- récupère des dates mini-maxi d'un nodeset -->
  <xsl:template name="period">
    <xsl:param name="nodeset"/>
    <xsl:param name="min"/>
    <xsl:param name="max"/>
    <xsl:choose>
      <xsl:when test="count($nodeset) = 0">
        <xsl:value-of select="$min"/>
        <xsl:text>/</xsl:text>
        <xsl:value-of select="$max"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="date1">
          <xsl:choose>
            <xsl:when test="contains($nodeset[1], '/')">
              <xsl:value-of select="substring-before( substring-before(concat($nodeset[1], '-'), '-') , '/')"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="substring-before(concat($nodeset[1], '-'), '-')"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:variable name="date2">
          <xsl:choose>
            <xsl:when test="contains($nodeset[1], '/')">
              <xsl:value-of select="substring-after( substring-before(concat($nodeset[1], '-'), '-') , '/')"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="substring-before(concat($nodeset[1], '-'), '-')"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:variable name="_min">
          <xsl:choose>
            <xsl:when test="string(number($date1)) = 'NaN' or number($date1) = 0">
              <xsl:value-of select="$min"/>
            </xsl:when>
            <xsl:when test="string(number($min)) = 'NaN' or number($min) = 0">
              <xsl:value-of select="$date1"/>
            </xsl:when>
            <xsl:when test="$date1 &lt; $min">
              <xsl:value-of select="$date1"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="$min"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:variable name="_max">
          <xsl:choose>
            <xsl:when test="string(number($date2)) = 'NaN' or number($date2) = 0">
              <xsl:value-of select="$max"/>
            </xsl:when>
            <xsl:when test="string(number($max)) = 'NaN' or number($max) = 0">
              <xsl:value-of select="$date2"/>
            </xsl:when>
            <xsl:when test="$date2 &gt; $max">
              <xsl:value-of select="$date2"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="$max"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:call-template name="period">
          <xsl:with-param name="min" select="$_min"/>
          <xsl:with-param name="max" select="$_max"/>
          <xsl:with-param name="nodeset" select="$nodeset[position() &gt; 1]"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="description">
    <xsl:apply-templates select="condesc/desc/persdesc | condesc/desc/corpdesc | condesc/desc/famdesc" mode="description"/>
  </xsl:template>

  <!-- supprimer les sauts de lignes et tabulations -->
  <xsl:template match="text()" mode="description">
    <xsl:value-of select="translate(., $sql-from, $sql-to)"/>
  </xsl:template>
  <!-- dates et fonctions -->
  <xsl:template match=" corpdesc | persdesc | famdesc" mode="description">
    <xsl:apply-templates select="existdesc" mode="description"/>
    <xsl:apply-templates select="funactdesc" mode="description"/>
  </xsl:template>
  <!-- pas besoin d'une ligne de tableau -->
  <xsl:template match="existdesc" mode="description">
    <xsl:call-template name="existdate"/>
  </xsl:template>
  <xsl:template match="funactdesc" mode="description">
    <xsl:apply-templates select="*"/>
  </xsl:template>
  <xsl:template match="text()">
    <xsl:value-of select="translate(., $sql-from, $sql-to)"/>
  </xsl:template>

  <!-- divisions non indexées -->
  <xsl:template match="eacheader" mode="text"/>
  <!-- quelques divisions pouvant être traversées -->
  <xsl:template match="eac | condesc | identity | persgrp | corpgrp | famgrp" mode="text">
    <xsl:apply-templates select="*" mode="text"/>
  </xsl:template>
  <!-- par défaut, sortir le texte -->
  <xsl:template match="*" mode="text" name="text-apply">
    <xsl:text> </xsl:text>
    <xsl:choose>
      <xsl:when test="text()[normalize-space(.) != '']">
        <xsl:apply-templates mode="text"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="*" mode="text"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <!-- blocs à séparer d'un saut de ligne -->
  <xsl:template match="chronitem" mode="text">
    <xsl:text> \n</xsl:text>
    <xsl:apply-templates select="event | eventgroup" mode="text"/>
    <xsl:if test="place[normalize-space(.) != '']">, </xsl:if>
    <xsl:apply-templates select="place[normalize-space(.) != '']" mode="text"/>
    <xsl:if test="date[normalize-space(.) != '']">, </xsl:if>
    <xsl:apply-templates select="date[normalize-space(.) != '']" mode="text"/>
  </xsl:template>
  <!-- les noms -->
  <xsl:template match="pershead | corphead | famhead" mode="text">
    <xsl:text> \n</xsl:text>
    <xsl:choose>
      <xsl:when test="part[contains(@type, 'full')]">
        <xsl:apply-templates select="part[contains(@type, 'full')]" mode="text"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="text-apply"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- divisions avec double espaces -->
  <xsl:template match="bioghist | resourcerels | eacrels | funactdesc" mode="text">
    <xsl:text>\n</xsl:text>
    <xsl:call-template name="text-apply"/>
  </xsl:template>
  <xsl:template match="bioghist/* | eacrel | resourcerel" mode="text">
    <xsl:text>\n</xsl:text>
    <xsl:call-template name="text-apply"/>
  </xsl:template>
  <!-- supprimer -->
  <!-- comportement par défaut -->
  <xsl:template match="text()" mode="text">
    <xsl:value-of select="translate(., $sql-from, $sql-to)"/>
  </xsl:template>

</xsl:transform>
