<?xml version="1.0" encoding="ISO-8859-1" ?>
<!-- 
Purpose: generate a skeleton file thesaurus.xml with XPAth and words-list for rubrics

Copyright J.M. Vanel 2001 - under GNU public licence 
jmvanel@free.fr 

Worldwide Botanical Knowledge Base 
http://wwbota.free.fr/

$Header: /home/jmv/cvsroot/XMLPublication/kernel/publication-magician.xslt,v 1.2 2002/05/08 10:26:46 jmv Exp $ -->

<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
                xmlns:saxon='http://icl.com/saxon'
                exclude-result-prefixes="saxon"
                version="1.1" >

  <xsl:import href='make-words-list.xslt'/>
  <xsl:import href="unique.xslt" />
  <xsl:import href="filter-words-list.xslt" />
  <xsl:import href="related-words-in-words-list.xslt" />

  <xsl:param name='master' select="/" />
  <xsl:variable name='WRAPPERTAG'>div</xsl:variable>

  <xsl:template name="make-rubrics-list" >
    <!-- make a unique list of rubrics across document sources -->
    <xsl:param name="master.xml"></xsl:param>
    <xsl:call-template name="make-unique">
      <xsl:with-param name="nodes" >
        <xsl:for-each select="$master.xml/*/thead/th" >
          <rubric label='{.}' structure='row' >
            <xsl:value-of select="." /> 
          </rubric>
        </xsl:for-each>
        <xsl:for-each select='$master.xml/*/*[local-name()=$WRAPPERTAG]/*[local-name()=$WRAPPERTAG] / *[local-name()="h2"]' >
          <rubric label='{.}' structure='paragraph' >
            <xsl:value-of select="." /> 
          </rubric>
        </xsl:for-each>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:variable name='begin'>*[local-name()=$WRAPPERTAG] [*[local-name()="h2"][contains(node()[local-name()!="h2"],"</xsl:variable>
  <!-- 
  <xsl:variable name='begin'>*[local-name()=$WRAPPERTAG] [*[local-name()="h2"][contains(.,"</xsl:variable>
<xsl:variable name='end'>")]] / *[local-name()=$WRAPPERTAG]</xsl:variable> -->
  <xsl:variable name='end'>")]]</xsl:variable>

  <xsl:template match='rubric' mode="complete-rubric" >
    <xsl:copy>
      <xsl:copy-of select='@*' />
      <xsl:if test='not (@xpath)'>
        <xsl:attribute name='xpath'><xsl:value-of select='concat($begin, @label, $end )' /></xsl:attribute>
      </xsl:if>
      <xsl:if test='not (@item-xpath)'>
        <xsl:attribute name='item-xpath' >/*/*[local-name()=$WRAPPERTAG][@class="h1"]</xsl:attribute>
      </xsl:if>
    </xsl:copy>
  </xsl:template>

  <!-- Implementation of specific connectors, i.e. how to interpret the variants of master.xml file (see "XML basic structure") in presentation.html
  <xsl:template match='rubric[@structure="row"]' mode="complete-rubric" >
    <xsl:copy>
      <xsl:copy-of select='@*' />
      <xsl:if test='not (@xpath)'>
        <xsl:attribute name='xpath'><xsl:value-of select='string(@name)' /></xsl:attribute>
      </xsl:if>
      <xsl:if test='not (@item-xpath)'>
        <xsl:attribute name='item-xpath' >/*/tr</xsl:attribute>
      </xsl:if>
    </xsl:copy>
  </xsl:template>
 -->


<xsl:template match='/'>
 <publication>
  <merge-items according-to-label="Table of contents" group-by="rubric" />

  <xsl:variable name='rubric-labels-list'>
   <rubric name="Species" label="Table of contents" use-keywords="yes" structure="paragraph" item-xpath="/*/*[local-name()=&#34;div&#34;][@class=&#34;h1&#34;]" xpath="*[local-name()=&#34;h1&#34;]"/>
   <!-- CAUTION - TODO: put $WRAPPERTAG instead of "div" above -->
   <xsl:call-template name="make-rubrics-list">
     <xsl:with-param name="master.xml" select="$master"/> 
   </xsl:call-template>
  </xsl:variable>
   <xsl:message> DEBUG publication-magician.xslt: $rubric-labels-list= <xsl:copy-of select="$rubric-labels-list" /> ... </xsl:message>

  <xsl:for-each select="$rubric-labels-list/rubric">
   <xsl:variable name='rubric'>
    <xsl:copy>
      <xsl:copy-of select='@*' />
      <xsl:attribute name='name' >
       <xsl:call-template name="make-tag-name">
        <xsl:with-param name="text">
          <xsl:value-of select="@label"/> 
        </xsl:with-param>
       </xsl:call-template>
      </xsl:attribute>
    </xsl:copy>
   </xsl:variable>
   <xsl:variable name='rubric2'>
    <xsl:apply-templates select='$rubric' mode="complete-rubric" />
   </xsl:variable>
   <!-- <xsl:message> DEBUG $rubric2= <xsl:copy-of select="$rubric2" /> ... </xsl:message> -->

   <rubric use-keywords='yes' use-stopwords='yes' >
     <xsl:copy-of select='$rubric2/*/@*' />
     <xsl:message> Searching words for rubric <xsl:value-of select="@label" /> ... </xsl:message>
     <xsl:variable name='rubric-content0'  >
       <xsl:apply-templates select="saxon:evaluate(concat('$master//', $rubric2/*/@xpath ) )" mode='add-spaces' />
     </xsl:variable>
     <xsl:variable name='rubric-content'  >
       <xsl:apply-templates select="$rubric-content0" mode='suppress-h2-recursive' />
     </xsl:variable>
     <xsl:text>
     </xsl:text>
     <!-- <xsl:message> DEBUG $rubric-content= <xsl:copy-of select="$rubric-content" /> ... </xsl:message> --> -->
     <keywords rubric='{$rubric2/*/@xpath}'>
       <xsl:call-template name="related-words-in-words-list">
        <xsl:with-param name="nodes" >
       <xsl:call-template name="filter-words-list">
        <xsl:with-param name="nodes" >
         <xsl:call-template name="make-words-list">
          <xsl:with-param name="string" >
           <xsl:apply-templates select="$rubric-content" mode= 'suppress-unwanted-chars' />
          </xsl:with-param>
         </xsl:call-template>
        </xsl:with-param>
       </xsl:call-template>
        </xsl:with-param>
       </xsl:call-template>
     </keywords>
   </rubric>
   <xsl:text>
   </xsl:text>
  </xsl:for-each>
 </publication>
</xsl:template>

<xsl:template match='*' mode='add-spaces'>
   <xsl:copy-of select="." />
   <xsl:text>
   </xsl:text>
</xsl:template>

  <!-- Because the index words will become part of file or tag name, we transform these characters -->
  <xsl:template name="make-tag-name" >
    <xsl:param name="text"></xsl:param>
    <xsl:variable name ="match"  > &#xa0;/()"éèêàûôîï&lt;&gt;&amp;°:?</xsl:variable>
    <xsl:variable name ="replace">__----eeeauoii---0--</xsl:variable>
    <xsl:value-of 
         select='translate(normalize-space($text), $match, $replace)'/>
  </xsl:template>

<!-- we  suppress unwanted characters; this is applied before making index words list for each rubric. -->
<xsl:template match='text()' mode='suppress-unwanted-chars'>
 <xsl:variable name='search'  >*"[]:,;?()&amp;&#160;</xsl:variable>
 <xsl:variable name='replace' >            </xsl:variable>
 <xsl:value-of select="translate(., $search, $replace )" />
</xsl:template>

<xsl:template match="h2" mode='suppress-h2-recursive' />
<xsl:template match="@*|*|processing-instruction()|comment()" mode="suppress-h2-recursive" >
 <xsl:copy>
  <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()" mode="suppress-h2-recursive" /> 
  </xsl:copy>
</xsl:template>
</xsl:stylesheet> 
