<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method='xml'/>
  <!-- Title: Metadata INI generation Stylesheet -->
	<!-- version $Revision  $ -->
  <!-- Created: July 10, 2002 by Luis Fernando Mesa -->
  <!-- Company: Ovid Technologies inc. -->
  <!-- Parameters:  None-->
  <!-- Description: This Stylesheet reads a Metadata XML file and extracts all necessary
  information to create an Ovid ini metadata stanza. This stylesheet creates all the necessary
  output in XML format.
  It is meant to be run in conjuntion with the INIformatter.xsl, whose job is to format the
  output in the ovid ini legacy structure.
  -->  
  <!-- changes: LM07312002 Luis Fernando Mesa
  Addded dependency flag to all sections. It is needed to make ini's compatible with older
  systems.
  Fixed bug where only one dbfield_snames was being produced. 
  -->  
  <xsl:variable name="xsltsl-str-lower" select="'abcdefghijklmnopqrstuvwxyz'"/>
  <xsl:variable name="xsltsl-str-upper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
  <xsl:variable name="xsltsl-str-digits" select="'0123456789'"/>
  <!-- space (#x20) characters, carriage returns, line feeds, or tabs. -->
  <xsl:variable name="xsltsl-str-ws" select="'&#x20;&#x9;&#xD;&#xA;'"/>
  <xsl:variable name='DatabaseName' select='translate(/meta/header/familyid, $xsltsl-str-upper, $xsltsl-str-lower)'/>
  
  <!-- root template -->  
  <xsl:template match="/meta">
		<Ini>
      <!-- Process all metadata elements and generate Main Metadata Stanza
      as well as the related regular expression stanzas
      -->
      <xsl:apply-templates select="elements"/>
    </Ini>
	</xsl:template>
  
  <!-- Generate Main Metadata Stanza with all Key/value pairs -->  
  <xsl:template match='elements'> 
    <Section name="{concat('[database.',$DatabaseName,'.metadata.fields]')}">
      <xsl:for-each select='*'>
        <xsl:choose>
          <!-- Metadata that contains Attribute elements requires special processing 
          Additional Keys need to be created and the Main element uses a different format
          Example: 
          subc.1=SUBJECTS_CONTROLLED||NO|Regexp(sh,[database.psycdb.metadata.regexp.subc.1_1])
          subc.2=SUBJECTS_CONTROLLED||NO|Regexp(po,[database.psycdb.metadata.regexp.subc.1_2])
          subc.3=SUBJECTS_CONTROLLED||NO|ag
          subc=SUBJECTS_CONTROLLED|HasAttributes|YES|vc#apa=subc.1|vc#apo=subc.2|vc#aag=subc.3 -->
          <xsl:when test='child::attributes'>
            <xsl:apply-templates select='.' mode='ProcessAttributes'/>
          </xsl:when>
          <xsl:otherwise>
            <!-- Metadata that does not contain Attribute elements is processed here -->
            <xsl:apply-templates select="." mode='ProcessElements'/>                    
          </xsl:otherwise>
        </xsl:choose>         
      </xsl:for-each> 
    </Section>
  </xsl:template>
  
  <!-- This template creates the key/value[s] pairs for metadata that does not contain attribute elements -->
  <xsl:template mode='ProcessElements' match="*">
    <xsl:variable name='KeyName' select='translate(@SN, $xsltsl-str-upper, $xsltsl-str-lower)'/>
    
    <xsl:call-template name='GenerateKey'>
      <xsl:with-param name='KeyName' select='$KeyName'/>
      <xsl:with-param name='NodeName' select='local-name()'/>
      <xsl:with-param name='Visibility' select='"YES"'/>
      <xsl:with-param name='DatabaseFields' select='dbfields'/>
    </xsl:call-template>
    
	</xsl:template>
  
  <!-- special processing is required for Metadata elements that contain child attribute nodes -->
  <xsl:template mode='ProcessAttributes' match="*">
    <xsl:variable name='KeyName' select='translate(@SN, $xsltsl-str-upper, $xsltsl-str-lower)'/>
    <xsl:variable name='AttrNodeName' select='local-name(.)'/>
    
    <!-- processing  every individual attribute node within the Metadata element -->
    <xsl:for-each select='child::attributes/*'>
      
      <!-- Key names for every non visible attribute follows the following naming convention:
      Name of the Visible Key, a period, followed by a sequence number
      i.e subc.1 -->              
      <xsl:variable name='KeyAttrName'>                 
        <xsl:value-of select='$KeyName'/>
        <xsl:text>.</xsl:text>
        <xsl:value-of select='position()'/>
      </xsl:variable>  
      
      <xsl:call-template name='GenerateKey'>
        <xsl:with-param name='KeyName' select='$KeyAttrName'/>
        <xsl:with-param name='NodeName' select='$AttrNodeName'/>
        <xsl:with-param name='Visibility' select='"NO"'/>
        <xsl:with-param name='DatabaseFields' select='id(@dbfields_ref)'/>
      </xsl:call-template>
      
    </xsl:for-each>
    <!-- end of processing  every individual attribute node within the Metadata element -->
    
    <!-- Begin processing the parent Element with Attributes -->
    <Key name='{$KeyName}'>
      <Value name='metaname'>
        <xsl:value-of select='$AttrNodeName'/>
      </Value>
      
      <!-- If the Metadata elements contains any child attribute nodes, then this value is set
      to "Has Attributes" otherwise it is empty -->
      <Value name='attributes'>
        <xsl:choose>
          <xsl:when test='child::attributes'>
            <xsl:text>HasAttributes</xsl:text>
          </xsl:when>
          <xsl:otherwise></xsl:otherwise>
        </xsl:choose>
      </Value>
      
      <!-- every element is visible, except the ones created by the attr loop -->
      <Value name='visibility'>
        <xsl:text>YES</xsl:text>
      </Value>
      
      <!-- this value element contains references to previous attribute keys -->
      <Value name='definition'>
        <xsl:for-each select='child::attributes/*'>
          <xsl:if test='position() > 1'>
            <xsl:text>|</xsl:text>
          </xsl:if>
          <xsl:call-template name="to-lower">
            <xsl:with-param name="text" select='concat(@SN,"#",*/@SN,"=")'/>
          </xsl:call-template>
          <xsl:value-of select ='$KeyName'/>
          <xsl:text>.</xsl:text>
          <xsl:value-of select='position()'/>
        </xsl:for-each>
      </Value>
    </Key>
  </xsl:template>
  
  <xsl:template name="to-lower">
    <xsl:param name="text"/>
    <xsl:value-of select="translate($text, $xsltsl-str-upper, $xsltsl-str-lower)"/>
  </xsl:template>
  
  <!-- this template generates the regular expression stanza names -->
  <xsl:template name='GetRegexpStanza'>
    <xsl:param name='DatabaseName'/>
    <xsl:param name='KeyName'/>
    <xsl:text>[database.</xsl:text>
    <xsl:value-of select='$DatabaseName'/>
    <xsl:text>.metadata.regexp.</xsl:text>
    <xsl:value-of select='$KeyName'/>
    <xsl:text>_</xsl:text>
    <xsl:value-of select='position()'/> 
    <xsl:text>]</xsl:text>                  
  </xsl:template>
  
  <!-- this template generates the standard Key for Metadata elements and Non-visible
  Metadata attribute elements -->
  <xsl:template name='GenerateKey'>
    <xsl:param name='KeyName'/>
    <xsl:param name='NodeName'/>
    <xsl:param name='Visibility'/>
    <xsl:param name='DatabaseFields'/>
    
    <Key name='{$KeyName}'>     
      <!-- this value node contains the Name of the Metadata Element i.e. ABSTRACT -->
      <Value name='metaname'>
        <xsl:value-of select='$NodeName'/>
      </Value>      
      
      <!-- If the Metadata elements contains any child attribute nodes, then this value is set
      to "Has Attributes" otherwise it is empty -->
      <Value name='attributes'>
        <xsl:choose>
          <xsl:when test='child::attributes'>
            <xsl:text>HasAttributes</xsl:text>
          </xsl:when>
          <xsl:otherwise></xsl:otherwise>
        </xsl:choose>      
      </Value>     
      
      <!-- every element is visible, except the ones created by the attr loop
      In this case, all elements that aren't created by the attribute processing are visible
      so this flag is set to true. -->
      <Value name='visibility'>
        <xsl:value-of select='$Visibility'/>
      </Value>
      
      <!--iterate every dbfields element and generate a 'Value' element for each one found -->
      <xsl:for-each select="$DatabaseFields">       
        <xsl:choose>
          <xsl:when test='child::regexp'>
            <!-- generate individual 'Value' elements for any regexp element found -->
            <xsl:variable name='ShortName'>
              <xsl:call-template name="to-lower">
                <xsl:with-param name="text" select='dbfield_sname'/>
              </xsl:call-template>
            </xsl:variable>
            <xsl:variable name='RegexpSection'>
              <xsl:call-template name='GetRegexpStanza'>
                <xsl:with-param name='DatabaseName' select='$DatabaseName'/>
                <xsl:with-param name='KeyName' select='$KeyName'/>
              </xsl:call-template>
            </xsl:variable>
            <Value name='definition'>
              <xsl:value-of select='concat("Regexp(",$ShortName,",",$RegexpSection,")")'/>
            </Value>
            <!-- generate Related Stanza element. It will contain the full stanza name
            plus the regexp associated with it. -->
            <RelatedStanza name='{$RegexpSection}'>
              <Key>
                <Value name='regexp'>
                  <xsl:value-of select='regexp'/>
                </Value>
              </Key>
            </RelatedStanza>
          </xsl:when>
          <xsl:when test='child::dbfield_sname'>
            <!-- concatenata all individual dbfield_sname that are part of a dbfields under one value -->
            <Value name='definition'>
              <!-- begin changes: LM07312002 -->
              <xsl:for-each select='child::dbfield_sname'>
                <xsl:if test='position() > 1'>
                  <xsl:text>|</xsl:text>
                </xsl:if>
                <xsl:call-template name="to-lower">
                  <xsl:with-param name="text" select='.'/>
                </xsl:call-template>
              </xsl:for-each>
              <!-- end changes: LM07312002 -->
            </Value>
          </xsl:when>
          <xsl:when test='child::fs'>
            <!-- do nothing at all. We don't need this field -->
          </xsl:when>
          <xsl:when test='child::perl_ref'>
            <!-- do nothing at all. We don't need this field -->
          </xsl:when>
          <xsl:when test='child::fld_formatter'>
            <!-- do nothing for now -->
          </xsl:when>
        </xsl:choose>
      </xsl:for-each>     
    </Key>
  </xsl:template>
</xsl:stylesheet><!-- Stylus Studio meta-information - (c)1998-2002 eXcelon Corp.
<metaInformation>
<scenarios ><scenario default="no" name="XML Generation" userelativepaths="yes" externalpreview="no" url="OvidPY_test.xml" htmlbaseurl="" processortype="xalan" commandline="" additionalpath="" additionalclasspath="" postprocessortype="none" postprocesscommandline="" postprocessadditionalpath="" postprocessgeneratedext=""/><scenario default="yes" name="INI Generation" userelativepaths="yes" externalpreview="no" url="OVID_embase.xml" htmlbaseurl="" processortype="xalan" commandline="" additionalpath="" additionalclasspath="" postprocessortype="custom" postprocesscommandline="java &#x2D;cp  &quot;C:\Program Files\eXcelon Corp\Stylus Studio\bin\xalan.jar&quot;;&quot;C:\Program Files\eXcelon Corp\Stylus Studio\bin\xml&#x2D;apis.jar&quot;;&quot;C:\Program Files\eXcelon Corp\Stylus Studio\bin\xerces.jar&quot;  org.apache.xalan.xslt.Process &#x2D;IN %1 &#x2D;XSL INIformatter.xsl &#x2D;OUT %2 " postprocessadditionalpath="file://c:\XML\OMetadata\" postprocessgeneratedext=".ini"/></scenarios><MapperInfo srcSchemaPath="metadata.dtd" srcSchemaRoot="meta" srcSchemaPathIsRelative="yes" srcSchemaInterpretAsXML="no" destSchemaPath="INISchema.xsd" destSchemaRoot="Ini" destSchemaPathIsRelative="yes" destSchemaInterpretAsXML="no"/>
</metaInformation>
-->