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

<!-- Synchronisation des fichiers docbook avec les notices -->

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dc="http://purl.org/dc/elements/1.1/">

 <xsl:template match="/">
  <Changes>
   <xsl:apply-templates>
    <xsl:with-param name="path" select="''"/>
   </xsl:apply-templates>
  </Changes>
 </xsl:template>

 <!-- Traitement des répertoires -->
 <xsl:template match="Folder">
  <xsl:param name="path"/>
  <xsl:variable name="newpath">
   <xsl:choose>
    <xsl:when test="$path=''"><xsl:value-of select="@path"/></xsl:when>
    <xsl:otherwise><xsl:value-of select="concat($path,'\',@name)"/></xsl:otherwise>
   </xsl:choose>
  </xsl:variable>

  <xsl:variable name="n" select="@name"/>

  <xsl:choose>
   <xsl:when test="count(File[@name=concat($n,'.docbook')])=0">
    <!-- Pas de fichier .docbook existant -->
    <xsl:if test="count(File[substring(@name,string-length(@name) - 2) = '.dc']) != 0 or count(Folder) != 0">
     <!-- Des notices existent -->
     <File action="write" path="{concat($newpath,'\',@name,'.docbook')}">
      <xsl:variable name="section">
       <section>
       <!-- Modifie par Guillaume : ne pas éliminer les 3 premiers chiffres -->
        <title><xsl:value-of select="@name"/></title>
        <xsl:if test="count(File[substring(@name,string-length(@name) - 2) = '.dc']) != 0">
         <itemizedlist>
          <xsl:for-each select="File[substring(@name,string-length(@name) - 2) = '.dc']">
           <xsl:variable name="metadata" select="document(concat($path,'\',$n,'\',@name))/metadata"/>
           <listitem>
            <para>
             <ulink url="{$metadata/identifier}"><xsl:value-of select="$metadata/title"/></ulink>
            </para>
           </listitem>
          </xsl:for-each>
         </itemizedlist>
        </xsl:if>
        <xsl:for-each select="Folder">
         <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"  href="{concat(@name,'\',@name,'.docbook')}" />
        </xsl:for-each>
       </section>
      </xsl:variable>
      <xsl:choose>
       <xsl:when test="count(ancestor::*)=0">
        <article>
         <title>Your title here</title>
         <articleinfo>
          <date>2005-01-01</date>
          <abstract>
           <title>Your subtitle 1 here</title>
           <para>your subtitle 2 here</para>
          </abstract>
          <releaseinfo><ulink url=" "></ulink></releaseinfo>
          <corpauthor>ул. “Екзарх Йосиф” № 14, 1301 София, България тел: (+359 2) 93 59 100, факс (+359 2) 93 59 101
Email: nij@nij.bg
WEB: http://nij.bg</corpauthor>
          <corpname>Национален Институт на Правосъдието</corpname>
          <copyright>
           <year>2005</year>
           <holder>Национален Институт на Правосъдието</holder>
          </copyright>
          <edition>1</edition>
          <publisher>
           <publishername>Национален Институт на Правосъдието</publishername>
           <address>
            <street></street>
            <postcode></postcode>
            <city></city>
            <country></country>
            <phone></phone>
           </address>
          </publisher>
         </articleinfo>
         <warning>
          <title></title>
          <para><ulink url=" "></ulink></para>
         </warning>
         <xsl:copy-of select="$section"/>
        </article>
       </xsl:when>
       <xsl:otherwise>
        <xsl:copy-of select="$section"/>
       </xsl:otherwise>
      </xsl:choose>
     </File>
    </xsl:if>
   </xsl:when>
   <xsl:otherwise>
    <!-- Un fichier .docbook existe -->
   </xsl:otherwise>
  </xsl:choose>

  <xsl:apply-templates>
   <xsl:with-param name="path" select="$newpath"/>
  </xsl:apply-templates>
 </xsl:template>

</xsl:stylesheet>
