<?xml version="1.0"?>
<!--
     Copyright (c) 2003  Reinout van Rees, reinout@vanrees.org
     
     This software is free software; you can redistribute it and/or
     modify it under the terms of the GNU Lesser General Public
     License as published by the Free Software Foundation; either
     version 2.1 of the License, or (at your option) any later
     version. 

     This software 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
     Lesser General Public License for more details.

     See file "COPYING" that contains the license (or go to
     http://gnu.org/).
-->

<xsl:stylesheet 
  xmlns:bc1="http://www.bcXML.org/2002/bcXML"
  xmlns    ="http://www.bcXML.org/2003/bcXML"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xml="http://www.w3.org/XML/1998/namespace"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"	
  exclude-result-prefixes="bc1"
  version="1.0">
  <xsl:strip-space elements="*"/>
  <xsl:output 
    method="xml" 
    encoding="UTF-8"
    indent="yes"/>

  <xsl:template match="/">
    <xsl:apply-templates select="/bc1:Taxonomy"/>
  </xsl:template>

  <xsl:template match="bc1:Taxonomy">
    <!-- Allmost 1-on-1 match Taxonomy->Taxonomy, just handle contents. -->
    <Taxonomy
      xsi:schemaLocation="http://www.bcXML.org/2003/bcXML http://www.bcXML.org/2003/xtd_2003-01-27.xsd">
      <xsl:attribute name="xml:lang">
        <xsl:value-of select="bc1:NativeLanguage"/>
      </xsl:attribute>
      <xsl:apply-templates select="bc1:Translation"/>
      <xsl:if test="not(bc1:Translation[
                    bc1:Language=current()/bc1:NativeLanguage
                    ]/bc1:Name)">
        <Name>
          <xsl:attribute name="xml:lang">
            <xsl:value-of select="bc1:NativeLanguage"/>
          </xsl:attribute>
          <xsl:value-of select="bc1:NativeName"/>
        </Name>
      </xsl:if>
      <xsl:apply-templates select="bc1:Object"/>
    </Taxonomy>
  </xsl:template>

  <xsl:template match="bc1:Object">
    <!-- Apart from NativeName and Translation it's a 1-on-1. -->
    <Object>
      <xsl:call-template name="id"/>
      <xsl:apply-templates select="bc1:Translation"/>
      <xsl:for-each select="bc1:SupertypeRef">
        <SupertypeRef>
          <xsl:value-of select="."/>
        </SupertypeRef>
      </xsl:for-each>
      <xsl:for-each select="bc1:PartRef">
        <PartRef>
          <xsl:value-of select="."/>
        </PartRef>
      </xsl:for-each>
      <xsl:for-each select="bc1:ExternalReference">
        <ExternalReference>
          <xsl:value-of select="."/>
        </ExternalReference>
      </xsl:for-each>
      <xsl:apply-templates select="bc1:Relationship"/>
      <xsl:apply-templates select="bc1:Property"/>
    </Object>
  </xsl:template>

  <xsl:template match="bc1:Relationship">
    <xsl:variable name="my-nativename">
      <xsl:value-of select="bc1:NativeName"/>
    </xsl:variable>
    <!-- Only run if there isn't a previous Relationship with the same
         nativename. -->
    <xsl:if
      test="not(preceding-sibling::bc1:Relationship[bc1:NativeName=$my-nativename])"> 
      <!-- Mapped to more generic Property. -->
      <Property>
        <xsl:call-template name="localid"/>
        <xsl:apply-templates select="bc1:Translation"/>
        <xsl:for-each select="bc1:TargetObjectRef">
          <AllowedObjectRef>
            <xsl:value-of select="."/>
          </AllowedObjectRef>
        </xsl:for-each>
        <xsl:for-each select="bc1:MinCardinality|bc1:MaxCardinality">
          <xsl:message>
            WARNING: MinCardinality has been deprecated. Are you sure
            you wanted to attach min/max cardinality to objects in the
            GENERIC taxonomy?
          </xsl:message>
        </xsl:for-each>
        <!-- Check if there are more Relationships with the same
             nativename. If true, include their targetobjectrefs
             here. -->
        <xsl:for-each
          select="following-sibling::bc1:Relationship[bc1:NativeName=$my-nativename]">
          <xsl:for-each select="bc1:TargetObjectRef">
            <AllowedObjectRef>
              <xsl:value-of select="."/>
            </AllowedObjectRef>
          </xsl:for-each>
        </xsl:for-each>
      </Property>
    </xsl:if>
  </xsl:template>

  <xsl:template match="bc1:Property">
    <!-- Apart from NativeName and Translation it's a 1-on-1. -->    
    <Property>
      <xsl:call-template name="localid"/>
      <xsl:apply-templates select="bc1:Translation"/>
      <xsl:apply-templates select="bc1:Measure"/>
      <xsl:for-each select="bc1:RestrictedValue">
        <xsl:message>
          WARNING: RestrictedValue wasn't supported on
          Property. At least not in the xsd, it *was* unclear in the
          UML diagram. In the new (and old) format, you *can* attach
          it to Measure. Mail Michel and Reinout.
        </xsl:message>
      </xsl:for-each>
    </Property>
  </xsl:template>

  <xsl:template match="bc1:Measure">
    <!-- It's a 1-on-1. -->    
    <Measure>
      <xsl:for-each select="bc1:Context">
        <Context>
          <xsl:value-of select="."/>
        </Context>
      </xsl:for-each>
      <xsl:for-each select="bc1:Unit">
        <Unit>
          <xsl:value-of select="."/>
        </Unit>
      </xsl:for-each>
      <xsl:for-each select="bc1:RestrictedValue">
        <xsl:message>
          WARNING: RestrictedValue is tricky. It wasn't clear whether
          it was being used and how. Now it's allowed on Measure. See
          the documentation for clear examples. Mail Michel and
          Reinout.
        </xsl:message>
      </xsl:for-each>
    </Measure>
  </xsl:template>

  <xsl:template name="id">
    <!-- Convert the NativeName to an id. -->
    <xsl:attribute name="id">
      <xsl:value-of select="bc1:NativeName"/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template name="localid">
    <!-- Convert the NativeName to localid. -->
    <xsl:attribute name="localid">
      <xsl:value-of select="bc1:NativeName"/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="bc1:Translation">
    <!-- Handle the Name/Explanation duo. -->
    <xsl:variable name="language">
      <xsl:value-of select="bc1:Language"/>
    </xsl:variable>
    <Name>
      <xsl:attribute name="xml:lang">
        <xsl:value-of select="$language"/>
      </xsl:attribute>
      <xsl:value-of select="bc1:Name"/>
    </Name>
    <xsl:for-each select="bc1:Explanation">
      <Explanation>
        <xsl:attribute name="xml:lang">
          <xsl:value-of select="$language"/>
        </xsl:attribute>
        <xsl:value-of select="."/>
      </Explanation>
    </xsl:for-each>
    <xsl:for-each select="bc1:Synonym">
      <Synonym>
        <xsl:attribute name="xml:lang">
          <xsl:value-of select="$language"/>
        </xsl:attribute>
        <xsl:value-of select="."/>
      </Synonym>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>
