<?xml version="1.0" encoding="ISO-8859-1"?>
<!--=================================================-->
<!--        "CellML" to "SBML" XML stylesheet        -->
<!--                 (c) EBI 2005-2010                  -->
<!--=================================================-->

<!--

    This program 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.

    This program 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 this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


    For any enquiry regarding this file, please contact "BioModels.net Support" <biomodels-net-support@lists.sourceforge.net>

-->
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:cellml="http://www.cellml.org/cellml/1.0#"
xmlns:mathml="http://www.w3.org/1998/Math/MathML"
xmlns:math="http://www.w3.org/1998/Math/MathML"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:cmeta="http://www.cellml.org/metadata/1.0#"
xmlns:bqs="http://www.cellml.org/bqs/1.0#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:vCard="http://www.w3.org/2001/vcard-rdf/3.0#"
xmlns:sbml="http://www.sbml.org/sbml/level2"
xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl"
xmlns:stsim="http://www.sbml.org/2001/ns/stochsim"
xmlns="http://www.sbml.org/sbml/level2"
>

<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>

<!--==============================================-->
<!--       Variable:  listOfUnitDefinitions_1     -->
<!--==============================================-->

<!--++++++++++++++++++++++++++++++++++++++++++++++-->
<!--+ Expand global and local unit definitions.
Template used:
   w_listOfUnits     
Result: 
        all unit based on base unit, but havn't combine
        same kind unit.
Structure:
listOfUnitDefinitions/
    unitDefinition/
        id:
            globalunitid=name
            localunitid=name_componentName
        name: original one
        component: local_unit_definition=component_name
                   global_unit_definition='model'
        listOfUnits/
            unit/
                kind
                multiplier                
                scale
                offset(complex=0)
                exponent(simple=1)
                
Note:
because later will redifine the 'time' and 'substance'
unit, so here test if this two units have already be defined
if so, timeone_position or substanceone_position.
+-->
<!--++++++++++++++++++++++++++++++++++++++++++++++-->
<!--global-->
<xsl:variable name="listOfUnitDefinitions_1">
	<xsl:for-each select="cellml:model/cellml:units[not(@base_units) or @base_units='no']">
		<xsl:variable name="po">
			<xsl:value-of select="position()"/>
		</xsl:variable>
		<xsl:variable name="ID">
			<xsl:choose>
				<xsl:when test="@name='substance' or @name='time'">
					<xsl:value-of select="concat(@name,'one_',$po)"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="@name"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:element name="unitDefinition">
			<xsl:attribute name="id">
				<xsl:value-of select="$ID"/>
			</xsl:attribute>
			<xsl:attribute name="name">
				<xsl:value-of select="@name"/>
			</xsl:attribute>
			<xsl:attribute name="component">
				<xsl:value-of select="'model'"/>
			</xsl:attribute>

			<xsl:element name="listOfUnits">
				<xsl:call-template name="w_listOfUnits">
					<xsl:with-param name="NAME" select="@name"/>
                                        <xsl:with-param name="MorC" select="'M'"/>
				</xsl:call-template>
			</xsl:element>

		</xsl:element>
	</xsl:for-each>

<!--local-->
	<xsl:for-each select="cellml:model/cellml:component/cellml:units[not(@base_units) or @base_units='no']">
		<xsl:variable name="com">
			<xsl:value-of select="../@name"/>
		</xsl:variable>
		<xsl:element name="unitDefinition">
			<xsl:attribute name="id">
				<xsl:value-of select="concat(@name,'_',$com)"/>
			</xsl:attribute>
			<xsl:attribute name="name">
				<xsl:value-of select="@name"/>
			</xsl:attribute>
			<xsl:attribute name="component">
				<xsl:value-of select="$com"/>
			</xsl:attribute>

			<xsl:element name="listOfUnits">
				<xsl:call-template name="w_listOfUnits">
					<xsl:with-param name="NAME" select="@name"/>
                                        <xsl:with-param name="MorC" select="'C'"/>
				</xsl:call-template>
			</xsl:element>

		</xsl:element>
	</xsl:for-each>
</xsl:variable>

<!--==============================================-->
<!--       Variable:  listOfUnitDefinitions_2     -->
<!--==============================================-->
<!--combine the same kind subunits in each unitDefinition
call template 'combindKind'-->
<xsl:variable name="listOfUnitDefinitions_2">
	<xsl:for-each select="exsl:node-set($listOfUnitDefinitions_1)/unitDefinition">
		<xsl:element name="unitDefinition">
			<xsl:attribute name="id">
				<xsl:value-of select="@id"/>
			</xsl:attribute>
			<xsl:attribute name="name">
				<xsl:value-of select="@name"/>
			</xsl:attribute>
			<xsl:attribute name="component">
				<xsl:value-of select="@component"/>
			</xsl:attribute>
			
	<xsl:choose>
		<xsl:when test="count(./listOfUnits/*)='1'">
			<xsl:for-each select="./listOfUnits">
				<xsl:element name="listOfUnits">
					<xsl:for-each select="./unit">
						<xsl:element name="unit">
			<xsl:attribute name="kind">
				<xsl:value-of select="@kind"/>
			</xsl:attribute>
			<xsl:attribute name="multiplier">
				<xsl:value-of select="@multiplier"/>
			</xsl:attribute>
			<xsl:attribute name="scale">
				<xsl:value-of select="@scale"/>
			</xsl:attribute>
			<xsl:attribute name="exponent">
				<xsl:value-of select="@exponent"/>
			</xsl:attribute>
			<xsl:attribute name="offset">
				<xsl:value-of select="@offset"/>
			</xsl:attribute>
						</xsl:element>
					</xsl:for-each>
				</xsl:element>
			</xsl:for-each>
		</xsl:when>
		<xsl:otherwise>
			<xsl:for-each select="./listOfUnits">
				<xsl:element name="listOfUnits">
					<xsl:call-template name="combineKind">
						<xsl:with-param name="current" select="."/>
					</xsl:call-template>
				</xsl:element>
			</xsl:for-each>
		</xsl:otherwise>
	</xsl:choose>
		</xsl:element>
	</xsl:for-each>
</xsl:variable>


<!--=================================-->
<!--  Variable: connection           -->
<!--=================================-->
<!--visit connection, write down each connection
NOTICE: first test if this connection is legal
connection
lu
attribute:
	var (in) (name)
	var_component
	donor (out) (name)
	donor_component-->
<xsl:variable name="connection">
	<xsl:for-each select="cellml:model/cellml:connection">
	   <xsl:for-each select="cellml:map_variables">
		<xsl:variable name="var1">
			<xsl:value-of select="@variable_1"/>
		</xsl:variable>

		<xsl:variable name="comp1">
			<xsl:value-of select="../cellml:map_components/@component_1"/>
		</xsl:variable>

		<xsl:variable name="var2">
			<xsl:value-of select="@variable_2"/>
		</xsl:variable>

		<xsl:variable name="comp2">
			<xsl:value-of select="../cellml:map_components/@component_2"/>
		</xsl:variable>

		<xsl:variable name="v1c1">
			<xsl:for-each select="../../cellml:component[@name=$comp1]">
				<xsl:choose>
					<xsl:when test="cellml:variable[@name=$var1]/@private_interface='in' or cellml:variable[@name=$var1]/@public_interface='in'">
						<xsl:value-of select="'true'"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="'false'"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:for-each>
		</xsl:variable>

		<xsl:variable name="var">
			<xsl:choose>
				<xsl:when test="$v1c1='true'">
					<xsl:value-of select="$var1"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$var2"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="var_component">
			<xsl:choose>
				<xsl:when test="$v1c1='true'">
					<xsl:value-of select="$comp1"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$comp2"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="donor">
			<xsl:choose>
				<xsl:when test="$v1c1='true'">
					<xsl:value-of select="$var2"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$var1"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="donor_component">
			<xsl:choose>
				<xsl:when test="$v1c1='true'">
					<xsl:value-of select="$comp2"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$comp1"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		
		<xsl:variable name="allow">
			<xsl:choose>
				<xsl:when test="key('group',$var_component)/ancestor::cellml:group/cellml:relationship_ref[@relationship='encapsulation']/ancestor::cellml:group//*[name()='component_ref' and @component=$donor_component]">
					<xsl:choose>
						<xsl:when test="ancestor::cellml:model/cellml:component[@name=$var_component]/cellml:variable[@name=$var]/@public_interface='in'">
							<xsl:choose>
								<xsl:when test="key('group',$var_component)/ancestor::cellml:group/cellml:relationship_ref[@relationship='encapsulation']/ancestor::cellml:group//*[@component=$var_component]/child::cellml:component_ref[@component=$donor_component] and ancestor::cellml:model/cellml:component[@name=$donor_component]/cellml:variable[@name=$donor]/@public_interface='out'">
									<xsl:value-of select="'true'"/>
								</xsl:when>
								<xsl:when test="key('group',$var_component)/ancestor::cellml:group/cellml:relationship_ref[@relationship='encapsulation']/ancestor::cellml:group//*[@component=$var_component]/parent::*/cellml:component_ref[@component=$donor_component] and ancestor::cellml:model/cellml:component[@name=$donor_component]/cellml:variable[@name=$donor]/@public_interface='out'">
									<xsl:value-of select="'true'"/>
								</xsl:when>
								<xsl:when test="key('group',$var_component)/ancestor::cellml:group/cellml:relationship_ref[@relationship='encapsulation']/ancestor::cellml:group//*[@component=$var_component]/parent::cellml:component_ref[@component=$donor_component] and ancestor::cellml:model/cellml:component[@name=$donor_component]/cellml:variable[@name=$donor]/@private_interface='out'">
									<xsl:value-of select="'true'"/>
								</xsl:when>
								<xsl:otherwise>
									<xsl:value-of select="'false'"/>
								</xsl:otherwise>
							</xsl:choose>
						</xsl:when>
						<xsl:otherwise>
							<xsl:choose>
								<xsl:when test="key('group',$var_component)/ancestor::cellml:group/cellml:relationship_ref[@relationship='encapsulation']/ancestor::cellml:group//*[@component=$var_component]/child::cellml:component_ref[@component=$donor_component] and ancestor::cellml:model/cellml:component[@name=$donor_component]/cellml:variable[@name=$donor]/@public_interface='out'">
									<xsl:value-of select="'true'"/>
								</xsl:when>
								<xsl:otherwise>
									<xsl:value-of select="'false'"/>
								</xsl:otherwise>
							</xsl:choose>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="'true'"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

<xsl:choose>
	<xsl:when test="$allow='true'">
<xsl:element name="lu">
	<xsl:attribute name="var">
		<xsl:value-of select="$var"/>
	</xsl:attribute>
	
	<xsl:attribute name="var_component">
		<xsl:value-of select="$var_component"/>
	</xsl:attribute>

	<xsl:attribute	name="donor">
		<xsl:value-of select="$donor"/>
	</xsl:attribute>

	<xsl:attribute name="donor_component">
		<xsl:value-of select="$donor_component"/>
	</xsl:attribute>
</xsl:element>
	</xsl:when>
	<xsl:otherwise>
          <xsl:message>
            <xsl:value-of select="$newline"/>
            <xsl:value-of select="'The Connection is not valid !!!!!!!! '"/>
            <xsl:value-of select="$newline"/>
            <xsl:value-of select="concat('var = ', $var, ' ')"/>
            <xsl:value-of select="$newline"/>
            <xsl:value-of select="concat('var_component = ', $var_component, ' ')"/>
            <xsl:value-of select="$newline"/>
            <xsl:value-of select="concat('donor = ', $donor, ' ')"/>
            <xsl:value-of select="$newline"/>
            <xsl:value-of select="concat('donor_component = ', $donor_component, ' ')"/>
            <xsl:value-of select="$newline"/>
          </xsl:message>
<xsl:choose>
	<xsl:when test="$var=$donor">
<xsl:message>
            <xsl:value-of select="$newline"/>
            <xsl:value-of select="'donor and var are equal '"/>
            <xsl:value-of select="$newline"/>
</xsl:message>
</xsl:when>
	<xsl:otherwise>
<xsl:message>
            <xsl:value-of select="$newline"/>
            <xsl:value-of select="'donor and var are not equal !!!!?!!?! '"/>
            <xsl:value-of select="$newline"/>
</xsl:message>
 </xsl:otherwise>
</xsl:choose>
        </xsl:otherwise>
</xsl:choose>
           </xsl:for-each>
	</xsl:for-each>

</xsl:variable>

<!--===============================-->
<!--      Variable: variable_dic_1 -->
<!--===============================-->

<!--     Template: traceBack       -->
<!--sent in the 'in' variable, return
the original one-->
<!--traceBack_var, return ori varible name -->
<!--traceBack_component, return ori component name, where the ori variable come from-->
<!--the above 2's combination is the ori_id-->
<xsl:template name="traceBack_var">
	<xsl:param name="variable"/>
	<xsl:param name="variable_component"/>


	<xsl:choose>
		<xsl:when test="exsl:node-set($connection)/lu[@var=$variable and @var_component=$variable_component]">
			<xsl:variable name="new_var">
				<xsl:value-of select="exsl:node-set($connection)/lu[@var=$variable and @var_component=$variable_component]/@donor"/>
			</xsl:variable>
			<xsl:variable name="new_var_comp">
				<xsl:value-of select="exsl:node-set($connection)/lu[@var=$variable and @var_component=$variable_component]/@donor_component"/>
			</xsl:variable>
			<xsl:call-template name="traceBack_var">
				<xsl:with-param name="variable" select="$new_var"/>
				<xsl:with-param name="variable_component" select="$new_var_comp"/>
			</xsl:call-template>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="$variable"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--===============================-->

<xsl:template name="traceBack_component">
	<xsl:param name="variable"/>
	<xsl:param name="variable_component"/>


	<xsl:choose>
		<xsl:when test="exsl:node-set($connection)/lu[@var=$variable and @var_component=$variable_component]">
			<xsl:variable name="new_var">
				<xsl:value-of select="exsl:node-set($connection)/lu[@var=$variable and @var_component=$variable_component]/@donor"/>
			</xsl:variable>
			<xsl:variable name="new_var_comp">
				<xsl:value-of select="exsl:node-set($connection)/lu[@var=$variable and @var_component=$variable_component]/@donor_component"/>
			</xsl:variable>
			<xsl:call-template name="traceBack_component">
				<xsl:with-param name="variable" select="$new_var"/>
				<xsl:with-param name="variable_component" select="$new_var_comp"/>
			</xsl:call-template>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="$variable_component"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--===============================-->
<xsl:variable name="variable_dic_1">
	<xsl:for-each select="cellml:model/cellml:component/cellml:variable[@public_interface='in' or @private_interface='in']">
		<xsl:variable name="origin">
			<xsl:call-template name="traceBack_var">
				<xsl:with-param name="variable" select="@name"/>
				<xsl:with-param name="variable_component" select="../@name"/>
			</xsl:call-template>
		</xsl:variable>

		<xsl:variable name="origin_component">
                        <xsl:call-template name="traceBack_component">
                                <xsl:with-param name="variable" select="@name"/>
                                <xsl:with-param name="variable_component" select="../@name"/>
                        </xsl:call-template>
                </xsl:variable>

		<xsl:variable name="variable_component">
			<xsl:value-of select="../@name"/>
		</xsl:variable>

		<xsl:element name="lu">
			<xsl:attribute name="var_name">
				<xsl:value-of select="@name"/>
			</xsl:attribute>
			<xsl:attribute name="var_component">
				<xsl:value-of select="$variable_component"/>
			</xsl:attribute>
			<xsl:attribute name="var_id">
				<xsl:value-of select="concat(@name,'_',$variable_component)"/>
			</xsl:attribute>
			<xsl:attribute name="ori_name">
				<xsl:value-of select="$origin"/>
			</xsl:attribute>
			<xsl:attribute name="ori_component">
				<xsl:value-of select="$origin_component"/>
			</xsl:attribute>
			<xsl:attribute name="ori_id">
				<xsl:value-of select="concat($origin,'_',$origin_component)"/>
			</xsl:attribute>
		</xsl:element>		

	</xsl:for-each>
</xsl:variable>
<!--=================================-->
<!--   Variable: variable_dic_1_1    -->
<!--=================================-->
<!--dictionary for all variable, for find their origine-->
<!--based on dic_1, -->
<xsl:variable name="variable_dic_1_1">
	<xsl:for-each select="cellml:model/cellml:component/cellml:variable">
		<xsl:variable name="var">
			<xsl:value-of select="@name"/>
		</xsl:variable>
		<xsl:variable name="var_com">
			<xsl:value-of select="../@name"/>
		</xsl:variable>
		<xsl:element name="lu">
			<xsl:choose>
				<xsl:when test="exsl:node-set($variable_dic_1)/lu[@var_name=$var and @var_component=$var_com]">
					<xsl:for-each select="exsl:node-set($variable_dic_1)/lu[@var_name=$var and @var_component=$var_com]">
                        <xsl:attribute name="var_name">
                                <xsl:value-of select="@var_name"/>
                        </xsl:attribute>
                        <xsl:attribute name="var_component">
                                <xsl:value-of select="@var_component"/>
                        </xsl:attribute>
                        <xsl:attribute name="var_id">
                                <xsl:value-of select="@var_id"/>
                        </xsl:attribute>
                        <xsl:attribute name="ori_name">
                                <xsl:value-of select="@ori_name"/>
                        </xsl:attribute>
                        <xsl:attribute name="ori_component">
                                <xsl:value-of select="@ori_component"/>
                        </xsl:attribute>
                        <xsl:attribute name="ori_id">
                                <xsl:value-of select="@ori_id"/>
                        </xsl:attribute>
					</xsl:for-each>

				</xsl:when>
				<xsl:otherwise>
				
                        <xsl:attribute name="var_name">
                                <xsl:value-of select="$var"/>
                        </xsl:attribute>
                        <xsl:attribute name="var_component">
                                <xsl:value-of select="$var_com"/>
                        </xsl:attribute>
                        <xsl:attribute name="var_id">
                                <xsl:value-of select="concat($var,'_',$var_com)"/>
                        </xsl:attribute>
                        <xsl:attribute name="ori_name">
                                <xsl:value-of select="$var"/>
                        </xsl:attribute>
                        <xsl:attribute name="ori_component">
                                <xsl:value-of select="$var_com"/>
                        </xsl:attribute>
                        <xsl:attribute name="ori_id">
                                <xsl:value-of select="concat($var,'_',$var_com)"/>
                        </xsl:attribute>

	
				</xsl:otherwise>
			</xsl:choose>
		</xsl:element>
	</xsl:for-each>
</xsl:variable>


<!--===========================================-->
<!--        Variable: name_dic_1               -->
<!--===========================================-->
<!--based on variable_dic_1_1, copy out all the unique ori_id, and its ori_name
lu/
	ori_id
	ori_name-->
<xsl:variable name="name_dic_1">
	<xsl:for-each select="exsl:node-set($variable_dic_1_1)/lu[not(@ori_id=preceding-sibling::lu/@ori_id)]">
		<xsl:element name="lu">
			<xsl:attribute name="ori_id">
				<xsl:value-of select="@ori_id"/>
			</xsl:attribute>
			<xsl:attribute name="ori_name">
				<xsl:value-of select="@ori_name"/>
			</xsl:attribute>
		</xsl:element>
	</xsl:for-each>
</xsl:variable>

<!--===========================================-->
<!--        Variable: name_dic_2               -->
<!--===========================================-->
<!--based on name_dic_1, if the ori_name is unique, then change the ori_id as name
lu/
	ori_id
	ori_id_real-->
<xsl:variable name="name_dic_2">
	<xsl:for-each select="exsl:node-set($name_dic_1)/lu">
		<xsl:variable name="name">
			<xsl:value-of select="@ori_name"/>
		</xsl:variable>
		<xsl:element name="lu">
			<xsl:attribute name="ori_id">
				<xsl:value-of select="@ori_id"/>
			</xsl:attribute>
			<xsl:attribute name="ori_id_real">
				<xsl:choose>
					<xsl:when test="count(exsl:node-set($name_dic_1)/lu[@ori_name=$name])='1' and not($name='time')">
						<xsl:value-of select="$name"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="@ori_id"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
		</xsl:element>
	</xsl:for-each>
</xsl:variable>
<!--===========================================-->
<!--        Variable: variable_dic_2           -->
<!--===========================================-->
<!--based on variable_dic_1_1, and replace the ori_id as the ori_id_real in name_dic_2
lu/
	var_id
	var_name
	var_component
	
	ori_id       (from the name_dic_2/@ori_id_real)
	ori_name
	ori_component-->
<xsl:variable name="variable_dic_2">
	<xsl:for-each select="exsl:node-set($variable_dic_1_1)/lu">
		<xsl:variable name="ID">
			<xsl:value-of select="@ori_id"/>
		</xsl:variable>
		<xsl:element name="lu">
			<xsl:attribute name="var_id">
				<xsl:value-of select="@var_id"/>
			</xsl:attribute>
			<xsl:attribute name="var_name">
				<xsl:value-of select="@var_name"/>
			</xsl:attribute>
			<xsl:attribute name="var_component">
				<xsl:value-of select="@var_component"/>
			</xsl:attribute>
			<xsl:attribute name="ori_id">
				<xsl:value-of select="exsl:node-set($name_dic_2)/lu[@ori_id=$ID]/@ori_id_real"/>
			</xsl:attribute>
			<xsl:attribute name="ori_name">
				<xsl:value-of select="@ori_name"/>
			</xsl:attribute>
			<xsl:attribute name="ori_component">
				<xsl:value-of select="@ori_component"/>
			</xsl:attribute>
		</xsl:element>
	</xsl:for-each>
</xsl:variable>
<!--===========================================-->
<!--        Variable: delta_variable           -->
<!--===========================================-->
<!--from component/reaction/variable_ref/role @delta_variable
correct.
attri:  var_name
	var_component
	var_id
	correspond_var
	
	ori_id
	ori_name
	ori_component
	
suggest, at any circumstances, use ori_id.
But, if use var_id si ok, because I choose from reaction, all the delta from reaction so it is the origin-->
<xsl:variable name="delta_variable">
	<xsl:for-each select="cellml:model/cellml:component">
		<xsl:if test="cellml:reaction/cellml:variable_ref/cellml:role[@delta_variable]">
			<xsl:variable name="comp">
				<xsl:value-of select="@name"/>
			</xsl:variable>
			<xsl:for-each select="cellml:reaction/cellml:variable_ref/cellml:role[@delta_variable]">
				<xsl:variable name="var">
					<xsl:value-of select="@delta_variable"/>
				</xsl:variable>
				<xsl:variable name="ori_name">
					<xsl:value-of select="exsl:node-set($variable_dic_2)/lu[@var_name=$var and @var_component=$comp]/@ori_name"/>
				</xsl:variable>
				<xsl:variable name="ori_component">
					<xsl:value-of select="exsl:node-set($variable_dic_2)/lu[@var_name=$var and @var_component=$comp]/@ori_component"/>
				</xsl:variable>
				<xsl:variable name="ori_id">
					<xsl:value-of select="exsl:node-set($variable_dic_2)/lu[@var_name=$var and @var_component=$comp]/@ori_id"/>
				</xsl:variable>
				<xsl:element name="lu">
					<xsl:attribute name="var_name">
						<xsl:value-of select="@delta_variable"/>
					</xsl:attribute>
					<xsl:attribute name="var_component">
						<xsl:value-of select="$comp"/>
					</xsl:attribute>
					<xsl:attribute name="var_id">
						<xsl:value-of select="concat(@delta_variable,'_',$comp)"/>
					</xsl:attribute>
					<xsl:attribute name="correspond_var">
						<xsl:value-of select="../@variable"/>
					</xsl:attribute>
					<xsl:attribute name="ori_id">
						<xsl:value-of select="$ori_id"/>
					</xsl:attribute>
					<xsl:attribute name="ori_name">
						<xsl:value-of select="$ori_name"/>
					</xsl:attribute>
					<xsl:attribute name="ori_component">
						<xsl:value-of select="$ori_component"/>
					</xsl:attribute>
				</xsl:element>
			</xsl:for-each>
		</xsl:if>
	</xsl:for-each>
</xsl:variable>



<!--===========================================-->
<!--        Variable: rate_variable            -->
<!--===========================================-->
<!--from component/reaction/variable_ref/role @role=rate
collect.
attri: var_name  (from ../@variable)
        var_component
	var_unit (unit name)
	var_unit_component
        var_id (no origianl)

   	var_id is still fetched from variable_dic_2 ori_id, because new name system. ignore the following sentence.	
	(rate just come reaction and it should not have interface,so I will not try to trace that back just use var_id)-->

<xsl:variable name="rate_variable">
        <xsl:for-each select="cellml:model/cellml:component">
                <xsl:if test="cellml:reaction/cellml:variable_ref/cellml:role[@role='rate']">
                        <xsl:variable name="comp">
                                <xsl:value-of select="@name"/>
                        </xsl:variable>
			<xsl:for-each select="cellml:reaction">
				<xsl:variable name="position">
					<xsl:value-of select="position()"/>
				</xsl:variable>
                        <xsl:for-each select="cellml:variable_ref/cellml:role[@role='rate']">
				<xsl:variable name="var">
					<xsl:value-of select="../@variable"/>
				</xsl:variable>
				<xsl:variable name="unit">
					<xsl:value-of select="../../../cellml:variable[@name=$var]/@units"/>
				</xsl:variable>
                                <xsl:element name="lu">
                                        <xsl:attribute name="var_name">
                                                <xsl:value-of select="$var"/>
                                        </xsl:attribute>
					<xsl:attribute name="var_component">
						<xsl:value-of select="$comp"/>
					</xsl:attribute>
					<xsl:attribute name="var_unit">
						<xsl:value-of select="$unit"/>
					</xsl:attribute>
					<xsl:attribute name="var_unit_component">
						<xsl:choose>
							<xsl:when test="../../../cellml:units[@name=$unit]">
								<xsl:value-of select="$comp"/>
							</xsl:when>
							<xsl:otherwise>
								<xsl:value-of select="'model'"/>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:attribute>
					<xsl:attribute name="var_id">
						<!--
						<xsl:value-of select="concat($var,'_',$comp)"/>
						-->
						<xsl:value-of select="exsl:node-set($variable_dic_2)/lu[@var_name=$var and @var_component=$comp]/@ori_id"/>
					</xsl:attribute>
					<xsl:attribute name="possibleName">
						<xsl:value-of select="concat($comp,'_RAC_',$position)"/>
					</xsl:attribute>
                                </xsl:element>
                        </xsl:for-each>
			</xsl:for-each>
                </xsl:if>
        </xsl:for-each>
</xsl:variable>
<!--==========================================-->
<!--       Variable: time_variable            -->
<!--==========================================-->
<!--attribute:var_name
		var_component
		var_id
		var_unit
		var_unit_component 
		
		ori_id
		ori_name
		ori_component-->
<!--when doing the time unit consistency check, should trace back to original.
and if they all come form the same time variable. it is consistency-->

<xsl:variable name="time_variable">
	<xsl:for-each select="cellml:model/cellml:component/cellml:variable">
		<xsl:variable name="comp">
			<xsl:value-of select="../@name"/>
		</xsl:variable>
		<xsl:variable name="var">
			<xsl:value-of select="@name"/>
		</xsl:variable>
		<xsl:variable name="unit">
			<xsl:value-of select="@units"/>
		</xsl:variable>
		<xsl:if test="key('mathml_bvar',$var) and key('mathml_bvar',$var)/ancestor::cellml:component/@name=$comp">
			<xsl:variable name="ori_name">
				<xsl:value-of select="exsl:node-set($variable_dic_2)/lu[@var_name=$var and @var_component=$comp]/@ori_name"/>
			</xsl:variable>
			<xsl:variable name="ori_component">
				<xsl:value-of select="exsl:node-set($variable_dic_2)/lu[@var_name=$var and @var_component=$comp]/@ori_component"/>
			</xsl:variable>
			<xsl:variable name="ori_id">
				<xsl:value-of select="exsl:node-set($variable_dic_2)/lu[@var_name=$var and @var_component=$comp]/@ori_id"/>
			</xsl:variable>

			<xsl:variable name="ori_unit">
				<xsl:for-each select="ancestor::cellml:model/cellml:component[@name=$ori_component]">
					<xsl:value-of select="./cellml:variable[@name=$ori_name]/@units"/>
				</xsl:for-each>
			</xsl:variable>
			<xsl:variable name="ori_unit_component">
				
				<xsl:for-each select="ancestor::cellml:model/cellml:component[@name=$ori_component]">
					<xsl:choose>
						<xsl:when test="./cellml:units[@name=$ori_unit]">
							<xsl:value-of select="$ori_component"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="'model'"/>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:for-each>
				
			</xsl:variable>
			<xsl:variable name="ori_unit_id">
				<xsl:value-of select="exsl:node-set($listOfUnitDefinitions_2)/unitDefinition[@name=$ori_unit and @component=$ori_unit_component]/@id"/>
			</xsl:variable>

			<xsl:element name="lu">
				<xsl:attribute name="var_name">
					<xsl:value-of select="$var"/>
				</xsl:attribute>
				<xsl:attribute name="var_component">
					<xsl:value-of select="$comp"/>
				</xsl:attribute>
				<xsl:attribute name="var_id">
					<xsl:value-of select="concat($var,'_',$comp)"/>
				</xsl:attribute>
				<xsl:attribute name="var_unit">
					<xsl:value-of select="@units"/>
				</xsl:attribute>
				<xsl:attribute name="var_unit_component">
					<xsl:choose>
						<xsl:when test="../cellml:units[@name=$unit]">
							<xsl:value-of select="$comp"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="'model'"/>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:attribute>
				<xsl:attribute name="ori_id">
					<xsl:value-of select="$ori_id"/>
				</xsl:attribute>
				<xsl:attribute name="ori_component">
					<xsl:value-of select="$ori_component"/>
				</xsl:attribute>
				<xsl:attribute name="ori_name">
					<xsl:value-of select="$ori_name"/>
				</xsl:attribute>
				<xsl:attribute name="ori_unit">
					<xsl:value-of select="$ori_unit"/>
				</xsl:attribute>
				<xsl:attribute name="ori_unit_component">
					<xsl:value-of select="$ori_unit_component"/>
				</xsl:attribute>
				<xsl:attribute name="ori_unit_id">
					<xsl:value-of select="$ori_unit_id"/>
				</xsl:attribute>
			</xsl:element>
		</xsl:if>

	</xsl:for-each>
</xsl:variable>

<!--=================================-->
<!--  Variable: species_1            -->
<!--=================================-->

<xsl:variable name="species_1">
        <xsl:for-each select="cellml:model/cellml:component">
		<xsl:if test="./cellml:reaction">
			<xsl:variable name="com">
				<xsl:value-of select="@name"/>
			</xsl:variable>
			<xsl:for-each select="./cellml:variable">
				<xsl:variable name="var">
					<xsl:value-of select="@name"/>
				</xsl:variable>

				<xsl:if test="ancestor::cellml:component/cellml:reaction/cellml:variable_ref[@variable=$var and (cellml:role/@role='reactant' or cellml:role/@role='product' or cellml:role/@role='catalyst' or cellml:role/@role='inhibitor' or cellml:role/@role='activator' or cellml:role/@role='modifier')]">

						<xsl:variable name="ori_name">
                                                        <xsl:value-of select="exsl:node-set($variable_dic_2)/lu[@var_name=$var and @var_component=$com]/@ori_name"/>
                                                </xsl:variable>

						<xsl:variable name="ori_component">
                                                        <xsl:value-of select="exsl:node-set($variable_dic_2)/lu[@var_name=$var and @var_component=$com]/@ori_component"/>
                                                </xsl:variable>

			<xsl:for-each select="ancestor::cellml:model/cellml:component[@name=$ori_component]">
				<xsl:element name="lu">

						<xsl:attribute name="id">
							<xsl:value-of select="exsl:node-set($variable_dic_2)/lu[@var_name=$var and @var_component=$com]/@ori_id"/>
						</xsl:attribute>
                                        
                                                <xsl:attribute name="name">
                                                        <xsl:value-of select="exsl:node-set($variable_dic_2)/lu[@var_name=$var and @var_component=$com]/@ori_name"/>
                                                </xsl:attribute>
                                        
                                                <xsl:attribute name="component">
                                                        <xsl:value-of select="exsl:node-set($variable_dic_2)/lu[@var_name=$var and @var_component=$com]/@ori_component"/>
                                                </xsl:attribute>
						<xsl:attribute name="spe_name">
							<xsl:value-of select="$var"/>
						</xsl:attribute>
						<xsl:attribute name="spe_component">
                                                        <xsl:value-of select="$com"/>
                                                </xsl:attribute>
						<xsl:attribute name="spe_id">
                                                        <xsl:value-of select="concat($var,'_',$com)"/>
                                                </xsl:attribute>

						<xsl:variable name="unit">
							<xsl:value-of select="./cellml:variable[@name=$ori_name]/@units"/>
						</xsl:variable>

						<xsl:attribute name="unit_name">
							<xsl:value-of select="$unit"/>
						</xsl:attribute>
						<xsl:attribute name="unit_component">
							<xsl:choose>
								<xsl:when test="./cellml:units[@name=$unit]">
									<xsl:value-of select="$ori_component"/>
								</xsl:when>
								<xsl:otherwise>
									<xsl:value-of select="'model'"/>
								</xsl:otherwise>
							</xsl:choose>
						</xsl:attribute>

						<xsl:attribute name="unit_id">
                                                        <xsl:choose>
                                                                <xsl:when test="./cellml:units[@name=$unit]">
                                                                        <xsl:value-of select="concat($unit,'_',$ori_component)"/>
                                                                </xsl:when>
                                                                <xsl:otherwise>
                                                                        <xsl:value-of select="$unit"/>
                                                                </xsl:otherwise>
                                                        </xsl:choose>
                                                </xsl:attribute>
						
                                                        <xsl:choose>
                                                                <xsl:when test="./cellml:variable[@name=$ori_name]/@initial_value">
									<xsl:attribute name="initial_value">
                                                                        	<xsl:value-of select="./cellml:variable[@name=$ori_name]/@initial_value"/>
									</xsl:attribute>
                                                                </xsl:when>
                                                                <xsl:otherwise/>
                                                        </xsl:choose> 
					</xsl:element>
					</xsl:for-each>
				</xsl:if>
			</xsl:for-each>
		</xsl:if>

	</xsl:for-each>
</xsl:variable>
<!--ASSUME: in all rate variable, the substance and time unit part all unique, which is tested at the beginning of the convertion.-->

<!--ASSUME all the rate variable based on the same substance and time unit, which is checked at the beginning-->
<!--detect the substace  unit, and time of rate and, then define directly, then add in listOfUnitDefinitions_2 , but before add check if they have the
same name's unit, if have, change that one's name -->

<!--=================================-->
<!--  Variable: rate_substance       -->
<!--=================================-->
<!--search in rate_variable, through var_unit and var_unit_component, find the unit in listOfUnitDefinitions_2,
decompose it, and find the substance part (position=1). Just write it out.
lu/
	var_id
	var_unit
	var_unit_component
	
	kind
	multiplier
	scale
	exponent
	offset-->


<xsl:variable name="rate_substance">
	<xsl:for-each select="exsl:node-set($rate_variable)/lu">
		<xsl:variable name="var_id">
			<xsl:value-of select="@var_id"/>
		</xsl:variable>
		<xsl:variable name="var_unit">
			<xsl:value-of select="@var_unit"/>
		</xsl:variable>
		<xsl:variable name="var_unit_component">
			<xsl:value-of select="@var_unit_component"/>
		</xsl:variable>

		<xsl:variable name="subU">
			<xsl:value-of select="@possibleName"/>
		</xsl:variable>

		<xsl:for-each select="exsl:node-set($listOfUnitDefinitions_2)/unitDefinition[@name=$var_unit and @component=$var_unit_component]">				<xsl:choose>

				<xsl:when test="./listOfUnits/unit[(@kind='dimensionless' or @kind='gram' or @kind='kilogram' or @kind='mole') and (@exponent='1' or not(@exponent))]">
					<xsl:for-each select="./listOfUnits/unit[(@kind='dimensionless' or @kind='gram' or @kind='kilogram' or @kind='mole') and (@exponent='1' or not(@exponent))]">
						<xsl:if test="position()='1'">

		<xsl:element name="lu">
			<xsl:attribute name="var_id">
				<xsl:value-of select="$var_id"/>
			</xsl:attribute>
			<xsl:attribute name="var_unit">
				<xsl:value-of select="$var_unit"/>
			</xsl:attribute>
			<xsl:attribute name="var_unit_component">
				<xsl:value-of select="$var_unit_component"/>
			</xsl:attribute>

			<xsl:attribute name="kind">
				<xsl:value-of select="@kind"/>
			</xsl:attribute>
			<xsl:attribute name="multiplier">
				<xsl:value-of select="@multiplier"/>
			</xsl:attribute>
			<xsl:attribute name="scale">
				<xsl:value-of select="@scale"/>
			</xsl:attribute>
			<xsl:attribute name="exponent">
				<xsl:value-of select="@exponent"/>
			</xsl:attribute>
			<xsl:attribute name="offset">
				<xsl:value-of select="@offset"/>
			</xsl:attribute>

			<xsl:attribute name="possibleName">
				<xsl:value-of select="$subU"/>
			</xsl:attribute>
		</xsl:element>

						</xsl:if>
					</xsl:for-each>
				</xsl:when>
				<xsl:otherwise>

		<xsl:element name="lu">
			<xsl:attribute name="var_id">
				<xsl:value-of select="$var_id"/>
			</xsl:attribute>
			<xsl:attribute name="var_unit">
				<xsl:value-of select="$var_unit"/>
			</xsl:attribute>
			<xsl:attribute name="var_unit_component">
				<xsl:value-of select="$var_unit_component"/>
			</xsl:attribute>

			<xsl:attribute name="kind">
				<xsl:value-of select="'NAN'"/>
			</xsl:attribute>
			<xsl:attribute name="multiplier">
				<xsl:value-of select="'NAN'"/>
			</xsl:attribute>
			<xsl:attribute name="scale">
				<xsl:value-of select="'NAN'"/>
			</xsl:attribute>
			<xsl:attribute name="exponent">
				<xsl:value-of select="'NAN'"/>
			</xsl:attribute>
			<xsl:attribute name="offset">
				<xsl:value-of select="'NAN'"/>
			</xsl:attribute>

			<xsl:attribute name="possibleName">
				<xsl:value-of select="$subU"/>
			</xsl:attribute>
		</xsl:element>

					
				</xsl:otherwise>
			</xsl:choose>
		</xsl:for-each>
			

	</xsl:for-each>
</xsl:variable>

<!--===========================-->
<!-- Variable: substance_unit  -->
<!--===========================-->
<!--because consistency check has already been done at the beginning, using the above variable,
here just use the first one's substance unit as the substance_unit and define the SBML built in unit, substance.
substance_unit/
			unit/
		kind
		multiplier
		scale
		exponent
		offset-->
<xsl:variable name="substance_unit">
	<xsl:variable name="consistant">
		<xsl:call-template name="rateSub"/>
	</xsl:variable>
	<xsl:choose>
		<xsl:when test="$consistant='true'">
	<xsl:for-each select="exsl:node-set($rate_substance)/lu">
		<xsl:if test="position()='1'">
			<xsl:element name="unit">
				<xsl:attribute name="kind">
					<xsl:value-of select="@kind"/>
				</xsl:attribute>
				<xsl:attribute name="multiplier">
					<xsl:value-of select="@multiplier"/>
				</xsl:attribute>
				<xsl:attribute name="scale">
					<xsl:value-of select="@scale"/>
				</xsl:attribute>
				<xsl:attribute name="exponent">
					<xsl:value-of select="@exponent"/>
				</xsl:attribute>
				<xsl:attribute name="offset">
					<xsl:value-of select="@offset"/>
				</xsl:attribute>

				<xsl:attribute name="unit_name">
					<xsl:value-of select="'substance'"/>
				</xsl:attribute>
			</xsl:element>
		</xsl:if>
	</xsl:for-each>
		</xsl:when>
		<xsl:otherwise>
        <xsl:for-each select="exsl:node-set($rate_substance)/lu">
                        <xsl:element name="unit">
                                <xsl:attribute name="kind">
                                        <xsl:value-of select="@kind"/>
                                </xsl:attribute>
                                <xsl:attribute name="multiplier">
                                        <xsl:value-of select="@multiplier"/>
                                </xsl:attribute>
                                <xsl:attribute name="scale">
                                        <xsl:value-of select="@scale"/>
                                </xsl:attribute>
                                <xsl:attribute name="exponent">
                                        <xsl:value-of select="@exponent"/>
                                </xsl:attribute>
                                <xsl:attribute name="offset">
                                        <xsl:value-of select="@offset"/>
                                </xsl:attribute>

				<xsl:attribute name="unit_name">
					<xsl:value-of select="concat(@possibleName,'_subU')"/>
				</xsl:attribute>
                        </xsl:element>
        </xsl:for-each>

		</xsl:otherwise>
	</xsl:choose>
</xsl:variable>

<!--===========================-->
<!-- Variable:  rate_time      -->
<!--===========================-->
<xsl:variable name="rate_time">
	<xsl:for-each select="exsl:node-set($rate_variable)/lu">
		<xsl:variable name="var_id">
			<xsl:value-of select="@var_id"/>
		</xsl:variable>
		<xsl:variable name="var_unit">
			<xsl:value-of select="@var_unit"/>
		</xsl:variable>
		<xsl:variable name="var_unit_component">
			<xsl:value-of select="@var_unit_component"/>
		</xsl:variable>

		<xsl:for-each select="exsl:node-set($listOfUnitDefinitions_2)/unitDefinition[@name=$var_unit and @component=$var_unit_component]">				<xsl:choose>

				<xsl:when test="./listOfUnits/unit[@kind='second' and @exponent='-1']">
					<xsl:for-each select="./listOfUnits/unit[@kind='second' and @exponent='-1']">
						<xsl:if test="position()='1'">

		<xsl:element name="lu">
			<xsl:attribute name="var_id">
				<xsl:value-of select="$var_id"/>
			</xsl:attribute>
			<xsl:attribute name="var_unit">
				<xsl:value-of select="$var_unit"/>
			</xsl:attribute>
			<xsl:attribute name="var_unit_component">
				<xsl:value-of select="$var_unit_component"/>
			</xsl:attribute>

			<xsl:attribute name="kind">
				<xsl:value-of select="@kind"/>
			</xsl:attribute>
			<xsl:attribute name="multiplier">
				<xsl:value-of select="@multiplier"/>
			</xsl:attribute>
			<xsl:attribute name="scale">
				<xsl:value-of select="@scale"/>
			</xsl:attribute>
			<xsl:attribute name="exponent">
				<xsl:value-of select="@exponent"/>
			</xsl:attribute>
			<xsl:attribute name="offset">
				<xsl:value-of select="@offset"/>
			</xsl:attribute>
		</xsl:element>

						</xsl:if>
					</xsl:for-each>
				</xsl:when>
				<xsl:otherwise>

		<xsl:element name="lu">
			<xsl:attribute name="var_id">
				<xsl:value-of select="$var_id"/>
			</xsl:attribute>
			<xsl:attribute name="var_unit">
				<xsl:value-of select="$var_unit"/>
			</xsl:attribute>
			<xsl:attribute name="var_unit_component">
				<xsl:value-of select="$var_unit_component"/>
			</xsl:attribute>

			<xsl:attribute name="kind">
				<xsl:value-of select="'NAN'"/>
			</xsl:attribute>
			<xsl:attribute name="multiplier">
				<xsl:value-of select="'NAN'"/>
			</xsl:attribute>
			<xsl:attribute name="scale">
				<xsl:value-of select="'NAN'"/>
			</xsl:attribute>
			<xsl:attribute name="exponent">
				<xsl:value-of select="'NAN'"/>
			</xsl:attribute>
			<xsl:attribute name="offset">
				<xsl:value-of select="'NAN'"/>
			</xsl:attribute>
		</xsl:element>

					
				</xsl:otherwise>
			</xsl:choose>
		</xsl:for-each>
			

	</xsl:for-each>
</xsl:variable>

<!--===========================-->
<!-- Variable:  time_unit      -->
<!--===========================-->
<!--time consistancy check has already been done, both for bvar time_variable, and rate time units.
persume the bvar time should be the same as rate time unit part. (this should be sure by cellML). Get the time unit from bvar unique original one, and then change the sbml built in time
time_unit/
	unit/
		kind
		multiplier
		scale
		exponent (REMEMBER, should NOT be (-1)*@exponent)
		offset-->
<xsl:variable name="time_unit">
	<xsl:variable name="var">
		<xsl:for-each select="exsl:node-set($time_variable)/lu[not(@ori_id=preceding-sibling::lu/@ori_id)]">
	     		<xsl:if test="position()='1'">
				<xsl:value-of select="@ori_name"/>
	     		</xsl:if>
		</xsl:for-each>
	</xsl:variable>
	<xsl:variable name="comp">
		<xsl:for-each select="exsl:node-set($time_variable)/lu[not(@ori_id=preceding-sibling::lu/@ori_id)]">
			<xsl:if test="position()='1'">
				<xsl:value-of select="@ori_component"/>
			</xsl:if>
		</xsl:for-each>
	</xsl:variable>
		<xsl:variable name="unit">
			<xsl:for-each select="cellml:model/cellml:component[@name=$comp]">
				<xsl:value-of select="./cellml:variable[@name=$var]/@units"/>
			</xsl:for-each>
		</xsl:variable>
		<xsl:variable name="unit_comp">
			<xsl:for-each select="cellml:model/cellml:component[@name=$comp]">
				<xsl:choose>
					<xsl:when test="./cellml:units[@name=$unit]">
						<xsl:value-of select="$comp"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="'model'"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:for-each>
		</xsl:variable>
		   <xsl:for-each select="exsl:node-set($listOfUnitDefinitions_2)/unitDefinition[@name=$unit and @component=$unit_comp]">
			<xsl:for-each select="./listOfUnits/unit[@kind='second']">
			<xsl:element name="unit">
				<xsl:attribute name="kind">
					<xsl:value-of select="@kind"/>
				</xsl:attribute>
				<xsl:attribute name="multiplier">
					<xsl:value-of select="@multiplier"/>
				</xsl:attribute>
				<xsl:attribute name="scale">
					<xsl:value-of select="@scale"/>
				</xsl:attribute>
				<xsl:attribute name="exponent">
					<xsl:value-of select="@exponent"/>
				</xsl:attribute>
				<xsl:attribute name="offset">
					<xsl:value-of select="@offset"/>
				</xsl:attribute>
			</xsl:element>
			</xsl:for-each>
		   </xsl:for-each>
</xsl:variable>

<!--===================================-->
<!--Variable: listOfUnitDefinitions_2_2-->
<!--===================================-->
<!--add the substance_unit and time_unit to listOfUnitDefinition_2
with the id, and name both as substance or time.
in listOfUnitDefinitions_1, it is impossible have any units id=substance or time-->
<xsl:variable name="listOfUnitDefinitions_2_2">
	<xsl:for-each select="exsl:node-set($listOfUnitDefinitions_2)/unitDefinition">
		<xsl:element name="unitDefinition">
			<xsl:attribute name="id">
				<xsl:value-of select="@id"/>
			</xsl:attribute>
			<xsl:attribute name="name">
				<xsl:value-of select="@name"/>
			</xsl:attribute>
			<xsl:attribute name="component">
				<xsl:value-of select="@component"/>
			</xsl:attribute>
			
			<xsl:element name="listOfUnits">
				<xsl:for-each select="./listOfUnits/unit">
					<xsl:element name="unit">
						<xsl:attribute name="kind">
							<xsl:value-of select="@kind"/>
						</xsl:attribute>
						<xsl:attribute name="multiplier">
							<xsl:value-of select="@multiplier"/>
						</xsl:attribute>
						<xsl:attribute name="scale">
							<xsl:value-of select="@scale"/>
						</xsl:attribute>
						<xsl:attribute name="exponent">
							<xsl:value-of select="@exponent"/>
						</xsl:attribute>
						<xsl:attribute name="offset">
							<xsl:value-of select="@offset"/>
						</xsl:attribute>
					</xsl:element>
				</xsl:for-each>
			</xsl:element>
		</xsl:element>
	</xsl:for-each>
<!--substance unit-->
	<xsl:for-each select="exsl:node-set($substance_unit)/unit">
		<xsl:element name="unitDefinition">
			<xsl:attribute name="id">
				<xsl:value-of select="@unit_name"/>
			</xsl:attribute>
			<xsl:attribute name="name">
				<xsl:value-of select="'substance'"/>
			</xsl:attribute>
			<xsl:attribute name="component">
				<xsl:value-of select="'model'"/>
			</xsl:attribute>

			<xsl:element name="listOfUnits">
				<xsl:element name="unit">
					<xsl:attribute name="kind">
						<xsl:value-of select="@kind"/>
					</xsl:attribute>
					<xsl:attribute name="multiplier">
						<xsl:value-of select="@multiplier"/>
					</xsl:attribute>
					<xsl:attribute name="scale">
						<xsl:value-of select="@scale"/>
					</xsl:attribute>
					<xsl:attribute name="exponent">
						<xsl:value-of select="@exponent"/>
					</xsl:attribute>
					<xsl:attribute name="offset">
						<xsl:value-of select="@offset"/>
					</xsl:attribute>
				</xsl:element>
			</xsl:element>
		</xsl:element>
	</xsl:for-each>
<!--time unit-->
	<xsl:for-each select="exsl:node-set($time_unit)/unit">
		<xsl:element name="unitDefinition">
			<xsl:attribute name="id">
				<xsl:value-of select="'time'"/>
			</xsl:attribute>
			<xsl:attribute name="name">
				<xsl:value-of select="'time'"/>
			</xsl:attribute>
			<xsl:attribute name="component">
				<xsl:value-of select="'model'"/>
			</xsl:attribute>

			<xsl:element name="listOfUnits">
				<xsl:element name="unit">
					<xsl:attribute name="kind">
						<xsl:value-of select="@kind"/>
					</xsl:attribute>
					<xsl:attribute name="multiplier">
						<xsl:value-of select="@multiplier"/>
					</xsl:attribute>
					<xsl:attribute name="scale">
						<xsl:value-of select="@scale"/>
					</xsl:attribute>
					<xsl:attribute name="exponent">
						<xsl:value-of select="@exponent"/>
					</xsl:attribute>
					<xsl:attribute name="offset">
						<xsl:value-of select="@offset"/>
					</xsl:attribute>
				</xsl:element>
			</xsl:element>
		</xsl:element>
	</xsl:for-each>
</xsl:variable>
<!--===========================-->
<!-- Variable:   unit_addition -->
<!--===========================-->
<!--from species_1, find all unique unit definition (unit_id) 
call template, find substanceUnit and spatialSizeUnit. the name is spatial_position
substance_position

unit_addition_1
unitDefinition 
	id   spatial_position or substance_position
	name
	component=model
	
	listOfUnits
		unit
			kind
			multiplier
			scale
			exponent
			offset
-->
<xsl:variable name="unit_addition_1">
	<xsl:for-each select="exsl:node-set($species_1)/lu[not(@unit_id=preceding-sibling::lu/@unit_id)]">
	

		<xsl:variable name="position">
			<xsl:value-of select="position()"/>
		</xsl:variable>
		<xsl:variable name="unitId">
			<xsl:value-of select="@unit_id"/>
		</xsl:variable>
		<xsl:variable name="substanceUnits">
			<xsl:call-template name="judgeSubU">
				<xsl:with-param name="unitId" select="@unit_id"/>
			
			</xsl:call-template>
		</xsl:variable>
	
		<xsl:if test="exsl:node-set($substanceUnits)/subUnit[@kind]">
		   <xsl:for-each select="exsl:node-set($substanceUnits)/subUnit">
			<xsl:choose>
				<xsl:when test="(@kind='dimensionless' or @kind='gram' or @kind='kilogram' or @kind='mole') and @multiplier='1' and @scale='0' and @exponent='1' and @offset='0'"/>
				<xsl:otherwise>
			<xsl:element name="unitDefinition">
				<xsl:attribute name="id">
					<xsl:value-of select="concat('substance_',$position)"/>
				</xsl:attribute>
				<xsl:attribute name="name">
					<xsl:value-of select="concat('substance_',$position)"/>
				</xsl:attribute>
				<xsl:attribute name="component">
					<xsl:value-of select="'model'"/>
				</xsl:attribute>

				<xsl:element name="listOfUnits">
					<xsl:element name="unit">
						<xsl:attribute name="kind">
							<xsl:value-of select="@kind"/>
						</xsl:attribute>
						<xsl:attribute name="multiplier">
							<xsl:value-of select="@multiplier"/>
						</xsl:attribute>
						<xsl:attribute name="scale">
							<xsl:value-of select="@scale"/>
						</xsl:attribute>
						<xsl:attribute name="exponent">
							<xsl:value-of select="@exponent"/>
						</xsl:attribute>
						<xsl:attribute name="offset">
							<xsl:value-of select="@offset"/>
						</xsl:attribute>
					</xsl:element>
				</xsl:element>
			</xsl:element>
				</xsl:otherwise>
			</xsl:choose>
		   </xsl:for-each>
		</xsl:if>
	</xsl:for-each>

	<xsl:for-each select="exsl:node-set($species_1)/lu[not(@unit_id=preceding-sibling::lu/@unit_id)]">
		<xsl:variable name="position">
			<xsl:value-of select="position()"/>
		</xsl:variable>
		<xsl:variable name="spatialSizeUnits">
			<xsl:call-template name="judgeSpaU">
				<xsl:with-param name="unitId" select="@unit_id"/>
			
			</xsl:call-template>
		</xsl:variable>
		
		<xsl:if test="exsl:node-set($spatialSizeUnits)/spaUnit[@kind]">
		<xsl:for-each select="exsl:node-set($spatialSizeUnits)/spaUnit">
		<xsl:choose>
			<xsl:when test="((@kind='liter' or @kind='litre') and @multiplier='1' and @offset='0' and @exponent='1' and @scale='0') or ((@kind='meter' or @kind='metre') and @multiplier=1 and @scale='0' and @exponent='1' and @offset='0')">
			</xsl:when>
			<xsl:otherwise>
		<xsl:element name="unitDefinition">
			<xsl:attribute name="id">
				<xsl:value-of select="concat('spatial_',$position)"/>
			</xsl:attribute>
			<xsl:attribute name="name">
				<xsl:value-of select="concat('spatial_',$position)"/>
			</xsl:attribute>
			<xsl:attribute name="component">
				<xsl:value-of select="'model'"/>
			</xsl:attribute>

			<xsl:element name="listOfUnits">
				<xsl:element name="unit">
					<xsl:attribute name="kind">
						<xsl:value-of select="@kind"/>
					</xsl:attribute>
					<xsl:attribute name="multiplier">
						<xsl:value-of select="@multiplier"/>
					</xsl:attribute>
					<xsl:attribute name="scale">
						<xsl:value-of select="@scale"/>
					</xsl:attribute>
					<xsl:attribute name="exponent">
						<xsl:value-of select="@exponent"/>
					</xsl:attribute>
					<xsl:attribute name="offset">
						<xsl:value-of select="@offset"/>
					</xsl:attribute>
				</xsl:element>
			</xsl:element>
		</xsl:element>
			</xsl:otherwise>
		</xsl:choose>
		</xsl:for-each>
		</xsl:if>
	</xsl:for-each>


</xsl:variable>

<!--==============================-->
<!--  Variable: unit_addition_2   -->
<!--==============================-->
<!--delete duplication unit -->
<xsl:variable name="unit_addition_2">
	<xsl:for-each select="exsl:node-set($unit_addition_1)/unitDefinition">
		<xsl:variable name="k">
			<xsl:value-of select="./listOfUnits/unit/@kind"/>
		</xsl:variable>
		<xsl:variable name="m">
			<xsl:value-of select="./listOfUnits/unit/@multiplier"/>
		</xsl:variable>
		<xsl:variable name="s">
			<xsl:value-of select="./listOfUnits/unit/@scale"/>
		</xsl:variable>
		<xsl:variable name="e">
			<xsl:value-of select="./listOfUnits/unit/@exponent"/>
		</xsl:variable>
		<xsl:variable name="o">
			<xsl:value-of select="./listOfUnits/unit/@offset"/>
		</xsl:variable>
	
		<xsl:if test="not(preceding-sibling::unitDefinition/listOfUnits/unit[@kind=$k and @multiplier=$m and @scale=$s and @exponent=$e and @offset=$o])">
			<xsl:element name="unitDefinition">
				<xsl:attribute name="id">
					<xsl:value-of select="@id"/>
				</xsl:attribute>
				<xsl:attribute name="name">
					<xsl:value-of select="@name"/>
				</xsl:attribute>
				<xsl:attribute name="component">
					<xsl:value-of select="@component"/>
				</xsl:attribute>
	
				<xsl:element name="listOfUnits">
					<xsl:element name="unit">
						<xsl:attribute name="kind">
							<xsl:value-of select="$k"/>
						</xsl:attribute>
						<xsl:attribute name="multiplier">
							<xsl:value-of select="$m"/>
						</xsl:attribute>
						<xsl:attribute name="scale">
							<xsl:value-of select="$s"/>
						</xsl:attribute>
						<xsl:attribute name="exponent">
							<xsl:value-of select="$e"/>
						</xsl:attribute>
						<xsl:attribute name="offset">
							<xsl:value-of select="$o"/>
						</xsl:attribute>
					</xsl:element>
				</xsl:element>
			</xsl:element>	
		</xsl:if>
	</xsl:for-each>
</xsl:variable>

<!--===================================-->
<!--  Variable: listOfUnitDefinitions_3-->
<!--===================================-->
<!--add addition unit above in listOfUnitDefinitions_2-->
<!--
<xsl:template match="@* | node()" mode="copy">
	<xsl:copy>
		<xsl:apply-templates select="@*" mode="copy"/>
		<xsl:apply-templates mode="copy"/>
	</xsl:copy>
</xsl:template>
-->
<!--======================================-->
<xsl:variable name="listOfUnitDefinitions_3">
	<xsl:for-each select="exsl:node-set($listOfUnitDefinitions_2_2)/unitDefinition | exsl:node-set($unit_addition_2)/unitDefinition">
		<xsl:element name="unitDefinition">
			<xsl:attribute name="id">
				<xsl:value-of select="@id"/>
			</xsl:attribute>
			<xsl:attribute name="name">
				<xsl:value-of select="@name"/>
			</xsl:attribute>		
			<xsl:attribute name="component">
				<xsl:value-of select="@component"/>
			</xsl:attribute>
			<xsl:apply-templates select="./*"/>
		</xsl:element>
	</xsl:for-each>

</xsl:variable>
<!--=======================================-->
<!--  Variable: species_2                  -->
<!--=======================================-->
<!--from species_1, find unique id (original),
which represents the species.
copy id, name, component
from unit_id jugde spa and sub unit
then if have initial_value, judge if it is substance or concentration
also from the judge find the substanceUnits and spatialSizeUnits
if can not find, it must be the base unit, it shuld also represent in one attribute
-->
<!--if just has substanceUntis without spatialSizeUnits, then hasOnlySubstanceUnits=true -->

<xsl:variable name="species_2">
	<xsl:for-each select="exsl:node-set($species_1)/lu[not(@id=preceding-sibling::lu/@id)]">
		<xsl:variable name="substanceUnits">
			<xsl:call-template name="judgeSubU_list3">
				<xsl:with-param name="unitId" select="@unit_id"/>
			</xsl:call-template>
		</xsl:variable>

		<xsl:variable name="spatialSizeUnits">
			<xsl:call-template name="judgeSpaU_list3">
				<xsl:with-param name="unitId" select="@unit_id"/>
			</xsl:call-template>
		</xsl:variable>

	
		<xsl:element name="lu">
			<xsl:attribute name="id">
				<xsl:value-of select="@id"/>
			</xsl:attribute>
			<xsl:attribute name="name">
				<xsl:value-of select="@name"/>
			</xsl:attribute>
			<xsl:attribute name="component">
				<xsl:value-of select="@component"/>
			</xsl:attribute>
			<xsl:attribute name="unit_id">
				<xsl:value-of select="@unit_id"/>
			</xsl:attribute>
			<xsl:attribute name="unit_name">
				<xsl:value-of select="@unit_name"/>
			</xsl:attribute>
			<xsl:attribute name="unit_component">
				<xsl:value-of select="@unit_component"/>
			</xsl:attribute>
			<xsl:choose>
				<xsl:when test="@initial_value">
					<xsl:choose>
						<xsl:when test="not($substanceUnits='false') and $spatialSizeUnits='false'">
							<xsl:attribute name="initialAmount">
								<xsl:value-of select="@initial_value"/>
							</xsl:attribute>
						</xsl:when>
						<xsl:when test="not($substanceUnits='false') and not($spatialSizeUnits='false')">
							<xsl:attribute name="initialConcentration">
								<xsl:value-of select="@initial_value"/>
							</xsl:attribute>
						</xsl:when>
					</xsl:choose>
				</xsl:when>
				<xsl:otherwise/>
			</xsl:choose>

			<xsl:choose>
				<xsl:when test="$substanceUnits='false'">
				</xsl:when>
				<xsl:otherwise>
					<xsl:choose>
						<xsl:when test="exsl:node-set($substanceUnits)/subUnit[@id]">
							<xsl:attribute name="substanceUnits">
								<xsl:value-of select="exsl:node-set($substanceUnits)/subUnit/@id"/>
							</xsl:attribute>
						</xsl:when>
						<xsl:otherwise>
							<xsl:attribute name="substanceUnits">
								<xsl:value-of select="exsl:node-set($substanceUnits)/subUnit/@kind"/>
							</xsl:attribute>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:choose>
				<xsl:when test="$spatialSizeUnits='false'">
				</xsl:when>
				<xsl:otherwise>
					<xsl:choose>
						<xsl:when test="exsl:node-set($spatialSizeUnits)/spaUnit[@id]">
							<xsl:attribute name="spatialSizeUnits">
								<xsl:value-of select="exsl:node-set($spatialSizeUnits)/spaUnit/@id"/>
							</xsl:attribute>
						</xsl:when>
						<xsl:otherwise>
							<xsl:attribute name="spatialSizeUnits">
								<xsl:value-of select="exsl:node-set($spatialSizeUnits)/spaUnit/@kind"/>
							</xsl:attribute>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:otherwise>
			</xsl:choose>
		<!--add attribute hasOnlySubstanceUnits. -->
			<xsl:if test="not($substanceUnits='false') and $spatialSizeUnits='false'">
				<xsl:attribute name="hasOnlySubstanceUnits">
					<xsl:value-of select="'true'"/>
				</xsl:attribute>
			</xsl:if>
		</xsl:element>
	</xsl:for-each>
</xsl:variable>

<!--=============================================-->
<!--          Variable: dimension                -->
<!--=============================================-->
<!--check the consistency of dimension of reaction based
units. mainly focus on spatialSizeUnit item (neglect offset).
lu/
rate_id	rate_name rate_component unit_id	
spaUnit/
	 kind		exponent	dimension	convertor	multiplier	 scale 
	none		0		0	 			1		0
	meter/metre	-1		1
	meter/metre	-2		2
	meter/metre	-3		3
	liter/litre	-1		3        -->
<!--think about if rate_variable is empty, may be in one cellML model,there is no reaction (very common!)-->


<xsl:variable name="dimension">
	<xsl:choose>
		<xsl:when test="count(exsl:node-set($rate_variable)/lu)='0'">
			<xsl:element name="lu">
				<xsl:attribute name="rate_id">
					<xsl:value-of select="'none'"/>
				</xsl:attribute>	
				<xsl:attribute name="rate_name">
					<xsl:value-of select="'none'"/>
				</xsl:attribute>
				<xsl:attribute name="rate_component">
					<xsl:value-of select="'none'"/>
				</xsl:attribute>
				<xsl:attribute name="unit_id">
					<xsl:value-of select="'none'"/>
				</xsl:attribute>
				<xsl:element name="spaUnit">
					<xsl:attribute name="kind">
						<xsl:value-of select="'litre'"/>
					</xsl:attribute>
					<xsl:attribute name="multiplier">
						<xsl:value-of select="'1'"/>
					</xsl:attribute>
					<xsl:attribute name="scale">
						<xsl:value-of select="'0'"/>
					</xsl:attribute>
					<xsl:attribute name="exponent">
						<xsl:value-of select="'1'"/>
					</xsl:attribute>
					<xsl:attribute name="offset">
						<xsl:value-of select="'0'"/>
					</xsl:attribute>
					<xsl:attribute name="dimension">
						<xsl:value-of select="'3'"/>
					</xsl:attribute>
					<xsl:attribute name="convertor">
						<xsl:value-of select="'1'"/>
					</xsl:attribute>
				</xsl:element>
			</xsl:element>
		</xsl:when>
		<xsl:otherwise>
	<xsl:for-each select="exsl:node-set($rate_variable)/lu">
		<xsl:variable name="unit_name">
			<xsl:value-of select="@var_unit"/>
		</xsl:variable>
		<xsl:variable name="unit_component">
			<xsl:value-of select="@var_unit_component"/>
		</xsl:variable>

		<xsl:element name="lu">
			<xsl:attribute name="rate_id">
				<xsl:value-of select="@var_id"/>
			</xsl:attribute>
			<xsl:attribute name="rate_name">
				<xsl:value-of select="@var_name"/>
			</xsl:attribute>
			<xsl:attribute name="rate_component">
				<xsl:value-of select="@var_component"/>
			</xsl:attribute>
			<xsl:attribute name="unit_id">
				<xsl:value-of select="exsl:node-set($listOfUnitDefinitions_3)/unitDefinition[@name=$unit_name and @component=$unit_component]/@id"/>
			</xsl:attribute>
			<xsl:for-each select="exsl:node-set($listOfUnitDefinitions_3)/unitDefinition[@name=$unit_name and @component=$unit_component]">
				<xsl:element name="spaUnit">
					<xsl:choose>
						<xsl:when test="./listOfUnits/unit[((@kind='meter' or @kind='metre') and @exponent &lt; '0.0' and not(@exponent &lt; '-3.0')) or ((@kind='liter' or @kind='litre') and @exponent='-1')]">
							<xsl:for-each select="./listOfUnits/unit[((@kind='liter' or @kind='litre') and @exponent='-1') or ((@kind='meter' or @kind='metre') and (@exponent &lt; '0.0' and not(@exponent &lt; '-3.0')))]">
								<xsl:if test="position()='1'">
									<xsl:attribute name="kind">
										<xsl:value-of select="@kind"/>
									</xsl:attribute>
									<xsl:attribute name="multiplier">
										<xsl:value-of select="@multiplier"/>
									</xsl:attribute>
									<xsl:attribute name="scale">
										<xsl:value-of select="@scale"/>
									</xsl:attribute>
									<xsl:attribute name="exponent">
										<xsl:value-of select="@exponent"/>
									</xsl:attribute>
									<xsl:attribute name="dimension">
										<xsl:choose>
											<xsl:when test="(@kind='meter' or @kind='metre') and @exponent='-1'">
												<xsl:value-of select="'1'"/>
											</xsl:when>
											<xsl:when test="(@kind='meter' or @kind='metre') and @exponent='-2'">
												<xsl:value-of select="'2'"/>
											</xsl:when>
											<xsl:when test="(@kind='meter' or @kind='metre') and @exponent='-3'">
												<xsl:value-of select="'3'"/>
											</xsl:when>
											<xsl:otherwise>
												<xsl:value-of select="'3'"/>
											</xsl:otherwise>
										</xsl:choose>
									</xsl:attribute>
									<xsl:attribute name="convertor">
										<xsl:variable name="factorS">
											<xsl:call-template name="math_power_f">
												<xsl:with-param name="base" select="'10'"/>
												<xsl:with-param name="power" select="@scale"/>
											</xsl:call-template>
										</xsl:variable>
										<xsl:variable name="factorMS">
											<xsl:value-of select="@multiplier * $factorS"/>
										</xsl:variable>
										<xsl:choose>
											<xsl:when test="(@kind='meter' or @kind='metre') and @exponent='-1'">
												<xsl:value-of select="$factorMS"/>
											</xsl:when>
											<xsl:when test="(@kind='meter' or @kind='metre') and @exponent='-2'">
												<xsl:variable name="power2">
													<xsl:call-template name="math_power_f">
														<xsl:with-param name="base" select="$factorMS"/>
														<xsl:with-param name="power" select="'2'"/>
													</xsl:call-template>
												</xsl:variable>
                                                                                        	<xsl:value-of select="$power2"/>
											</xsl:when>
											<xsl:when test="(@kind='meter' or @kind='metre') and @exponent='-3'">
												<xsl:variable name="power3">
													<xsl:call-template name="math_power_f">
														<xsl:with-param name="base" select="$factorMS"/>
														<xsl:with-param name="power" select="'3'"/>
													</xsl:call-template>
												</xsl:variable>
												<xsl:value-of select="1000 * $power3"/>
											</xsl:when>
											<xsl:otherwise>
												<xsl:value-of select="$factorMS"/>
											</xsl:otherwise>
										</xsl:choose>
									</xsl:attribute>
								</xsl:if>
							</xsl:for-each>
						</xsl:when>
						<xsl:otherwise>
							<xsl:attribute name="kind">
								<xsl:value-of select="'none'"/>
							</xsl:attribute>
							<xsl:attribute name="multiplier">
								<xsl:value-of select="'1'"/>
							</xsl:attribute>
							<xsl:attribute name="scale">
								<xsl:value-of select="'0'"/>
							</xsl:attribute>
							<xsl:attribute name="exponent">
								<xsl:value-of select="'1'"/>
							</xsl:attribute>
							<xsl:attribute name="offset">
								<xsl:value-of select="'0'"/>
							</xsl:attribute>
							<xsl:attribute name="dimension">
								<xsl:value-of select="'0'"/>
							</xsl:attribute>
							<xsl:attribute name="convertor">
								<xsl:value-of select="'1'"/>
							</xsl:attribute>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:element>
			</xsl:for-each>
		</xsl:element>
	</xsl:for-each>
		</xsl:otherwise>
	</xsl:choose>
</xsl:variable>



<!--xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-->
<!--xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-->
<!--                        LEVEL2: variable                           -->
<!--xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-->
<!--xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-->
<!--useful variable:
1	variable_dic_2
			all the variables (include delta, rate, and time),
			var_id
			var_compoennt
					find: ori_id
2	delta_variable
			var_name
			var_id
			var_comonent
			correspond_var
3	rate_variable
			var_name
			var_id
			var_component
			var_unit
			var_unit_component
4	time_variable
			var_name
			var_id
			var_component
			var_unit
			var_unit_component
5	listOfUnitDefinitions_3
		unitDefinition
			id
			name
			component
			listOfUnits
				unit
					kind
					multiplier
					scale
					exponent
					offset
6	species_2
		id
		name
		component
		unit_id
		unit_name
		unit_compoennt
	
	optional:
		initialConcentration
		initialAmount
		substanceUnits
		spatialSizeUnits
7	dimension
		rate_id
		rate_name
		rate_compoennt
		unit_id
		spaUnit
			kind
			multiplier
			scale
			exponent
			dimension
			convertor

		 -->
<!--==============================================-->
<!--      Variabel: rateRule_1                    -->
<!--==============================================-->
<xsl:variable name="rateRule_1">
	<xsl:for-each select="cellml:model/cellml:component/mathml:math/mathml:apply[mathml:eq/following-sibling::*[1]/mathml:diff]">
		<xsl:variable name="comp">
			<xsl:value-of select="../../@name"/>
		</xsl:variable>
		<xsl:variable name="var">
			<xsl:value-of select="normalize-space(./mathml:eq/following-sibling::*[1]/mathml:diff/following-sibling::*[2])"/>
		</xsl:variable>
		<xsl:variable name="variable">
			<xsl:call-template name="reduce_name">
				<xsl:with-param name="var_name" select="$var"/>
				<xsl:with-param name="var_component" select="$comp"/>
			</xsl:call-template>
		</xsl:variable>
		<xsl:element name="rateRule">
			<xsl:attribute name="variable">
				<xsl:value-of select="$variable"/>
			</xsl:attribute>
			<xsl:element name="math">
				
				<xsl:apply-templates select="./mathml:eq/following-sibling::*[2]" />
		
			</xsl:element>
		</xsl:element>
	</xsl:for-each>
</xsl:variable>

<!--============================================-->
<!--        Variable:rateRule_2                 -->
<!--============================================-->
<!--delete any rateRule_1/rateRule with delta variable in ci-->
<xsl:variable name="rateRule_2">
	<xsl:for-each select="exsl:node-set($rateRule_1)/rateRule">
		<xsl:variable name="variable">
			<xsl:value-of select="@variable"/>
		</xsl:variable>
		<xsl:for-each select="math">
			<xsl:variable name="delta">
				<xsl:value-of select="count(.//ci[exsl:node-set($delta_variable)/lu/@ori_id=normalize-space(.)])"/>
			</xsl:variable>

			<xsl:choose>
				<xsl:when test="not($delta='0') or exsl:node-set($delta_variable)/lu/@var_id=$variable"/>
				<xsl:otherwise>
					<xsl:element name="rateRule">
						<xsl:attribute name="variable">
							<xsl:value-of select="$variable"/>
						</xsl:attribute>
						<xsl:element name="math">
							<xsl:apply-templates select="./*"/>
						</xsl:element>
					</xsl:element>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:for-each>
	</xsl:for-each>
</xsl:variable>

<!--=============================================-->
<!--        Variable: assignmentRule_1           -->
<!--=============================================-->
<xsl:variable name="assignmentRule_1">
	<xsl:for-each select="cellml:model/cellml:component/mathml:math/mathml:apply[mathml:eq/following-sibling::*[1]/self::mathml:ci]">
		<xsl:variable name="comp">
			<xsl:value-of select="../../@name"/>
		</xsl:variable>
		<xsl:variable name="var">
			<xsl:value-of select="normalize-space(./mathml:eq/following-sibling::*[1]/self::mathml:ci/.)"/>
		</xsl:variable>
		<xsl:variable name="variable">
			<xsl:call-template name="reduce_name">
				<xsl:with-param name="var_name" select="$var"/>
				<xsl:with-param name="var_component" select="$comp"/>
			</xsl:call-template>
		</xsl:variable>
		<xsl:element name="assignmentRule">
			<xsl:attribute name="variable">
				<xsl:value-of select="$variable"/>
			</xsl:attribute>
			<xsl:element name="math">
				<xsl:apply-templates select="./mathml:eq/following-sibling::*[2]"/>
			</xsl:element>
		</xsl:element>
	</xsl:for-each>
</xsl:variable>
<!--=============================================-->
<!--         Variable: assignmentRule_2          -->

<!--=============================================-->
<!--draw out the algebraicRule
if the @variable also appear in the math part ci
this should be algebraicRule-->
<xsl:variable name="assignmentRule_2">
	<xsl:for-each select="exsl:node-set($assignmentRule_1)/assignmentRule">
		<xsl:variable name="variable">
			<xsl:value-of select="@variable"/>
		</xsl:variable>
		<xsl:for-each select="math">
		<xsl:choose>
			<xsl:when test=".//ci[normalize-space(.)=$variable]"/>
			<xsl:otherwise>
				<xsl:element name="assignmentRule">
					<xsl:attribute name="variable">
						<xsl:value-of select="$variable"/>
					</xsl:attribute>
					<xsl:element name="math">
						<xsl:apply-templates select="./*"/>
					</xsl:element>
				</xsl:element>
			</xsl:otherwise>
		</xsl:choose>
		</xsl:for-each>
	</xsl:for-each>
</xsl:variable>

<!--=============================================-->
<!--         Variable: algebraicRule             -->
<!--=============================================-->
<xsl:variable name="algebraicRule">
	<xsl:for-each select="exsl:node-set($assignmentRule_1)/assignmentRule">
		<xsl:variable name="variable">
			<xsl:value-of select="@variable"/>
		</xsl:variable>
		<xsl:for-each select="math">
			<xsl:choose>
				<xsl:when test=".//ci[normalize-space(.)=$variable]">
					<xsl:element name="algebraicRule">
						<xsl:element name="math">
							<xsl:element name="apply">
								<xsl:element name="minus"/>
								<xsl:element name="ci">
									<xsl:value-of select="$variable"/>
								</xsl:element>
								<xsl:apply-templates select="./*"/>
							</xsl:element>
						</xsl:element>	
					</xsl:element>
				</xsl:when>
				<xsl:otherwise/>
			</xsl:choose>
		</xsl:for-each>
	</xsl:for-each>
</xsl:variable>

<!--=============================================-->
<!--        Variable: reaction                   -->
<!--=============================================-->
<xsl:variable name="reaction">
	<xsl:variable name="consistant">
		<xsl:call-template name="rateSub"/>
	</xsl:variable>
	<xsl:for-each select="cellml:model/cellml:component[cellml:reaction]">
		<xsl:variable name="comp">
			<xsl:value-of select="@name"/>
		</xsl:variable>
		<xsl:for-each select="./cellml:reaction">

                <xsl:variable name="subU">
                        <xsl:value-of select="concat($comp,'_RAC_',position(),'_subU')"/>
                </xsl:variable>


			<xsl:element name="reaction">
				<xsl:attribute name="id">
					<xsl:value-of select="concat($comp,'_RAC_',position())"/>
				</xsl:attribute>
				<xsl:attribute name="reversible">
					<xsl:choose>
						<xsl:when test="@reversible='no'">
							<xsl:value-of select="'false'"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="'true'"/>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:attribute>
				<xsl:element name="listOfReactants">
					<xsl:for-each select="./cellml:variable_ref[cellml:role/@role='reactant']">
						<xsl:variable name="variable">
							<xsl:call-template name="reduce_name">
								<xsl:with-param name="var_name" select="@variable"/>
								<xsl:with-param name="var_component" select="$comp"/>
							</xsl:call-template>
						</xsl:variable>
						<xsl:element name="speciesReference">
							<xsl:attribute name="species">
								<xsl:value-of select="$variable"/>
							</xsl:attribute>
							<xsl:if test="./cellml:role/@stoichiometry">
								<xsl:attribute name="stoichiometry">
									<xsl:value-of select="./cellml:role/@stoichiometry"/>
								</xsl:attribute>
							</xsl:if>
						</xsl:element>		
					</xsl:for-each>
				</xsl:element>
				<xsl:element name="listOfProducts">
					<xsl:for-each select="./cellml:variable_ref[cellml:role/@role='product']">
						<xsl:variable name="variable">
							<xsl:call-template name="reduce_name">
								<xsl:with-param name="var_name" select="@variable"/>
								<xsl:with-param name="var_component" select="$comp"/>
							</xsl:call-template>
						</xsl:variable>
						<xsl:element name="speciesReference">
							<xsl:attribute name="species">
								<xsl:value-of select="$variable"/>
							</xsl:attribute>
							<xsl:if test="./cellml:role/@stoichiometry">
								<xsl:attribute name="stoichiometry">
									<xsl:value-of select="./cellml:role/@stoichiometry"/>
								</xsl:attribute>
							</xsl:if>
						</xsl:element>
					</xsl:for-each>
				</xsl:element>
				<xsl:element name="listOfModifiers">
					<xsl:for-each select="./cellml:variable_ref[cellml:role/@role='modifier' or cellml:role/@role='catalyst' or cellml:role/@role='inhibitor' or cellml:role/@role='activator']">
						<xsl:variable name="variable">
							<xsl:call-template name="reduce_name">
								<xsl:with-param name="var_name" select="@variable"/>
								<xsl:with-param name="var_component" select="$comp"/>
							</xsl:call-template>
						</xsl:variable>
						<xsl:element name="modifierSpeciesReference">
							<xsl:attribute name="species">
								<xsl:value-of select="$variable"/>
							</xsl:attribute>
						</xsl:element>
					</xsl:for-each>
				</xsl:element>	
				<xsl:element name="kineticLaw">

					

<!--
					<xsl:attribute name="substanceUnits">
						<xsl:choose>
							<xsl:when test="$consistant='true'">
								<xsl:value-of select="'substance'"/>

							</xsl:when>
							<xsl:otherwise>
								<xsl:value-of select="$subU"/>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:attribute>
-->

					<xsl:choose>
						<xsl:when test="$consistant='false'">
							<xsl:attribute name="substanceUnits">
								<xsl:value-of select="$subU"/>
							</xsl:attribute>
						</xsl:when>
						<xsl:otherwise/>

					</xsl:choose>
					<xsl:for-each select="./cellml:variable_ref/cellml:role[@role='rate']">
						<xsl:variable name="var">
							<xsl:value-of select="../@variable"/>
						</xsl:variable>
						<xsl:variable name="convertor">
							<xsl:value-of select="exsl:node-set($dimension)/lu[@rate_name=$var and @rate_component=$comp]/spaUnit/@convertor"/>
						</xsl:variable>
<!--
						<xsl:variable name="compartment">
							<xsl:choose>
								<xsl:when test="exsl:node-set($dimension)/lu[@rate_name=$var and @rate_component=$comp]/spaUnit/@dimension='0'">
									<xsl:value-of select="''"/>
								</xsl:when>
							</xsl:choose>
						</xsl:variable>
-->
						<xsl:element name="math">
							<xsl:element name="apply">
								<xsl:element name="times"/>
									<xsl:choose>
										<xsl:when test="exsl:node-set($dimension)/lu[@rate_name=$var and @rate_component=$comp]/spaUnit/@dimension='0'"/>
										<xsl:otherwise>
									<xsl:element name="ci">
										<xsl:value-of select="'COMpartment'"/>
									</xsl:element>
										</xsl:otherwise>
									</xsl:choose>
									<xsl:element name="cn">
										<xsl:value-of select="$convertor"/>
									</xsl:element>
									<xsl:apply-templates select="./mathml:math/mathml:apply/mathml:eq/following-sibling::*[2]"/>
							
							</xsl:element>
						</xsl:element>
					</xsl:for-each>
				</xsl:element>			
			</xsl:element>	
		</xsl:for-each>
	</xsl:for-each>
</xsl:variable>

<!--=============================================-->
<!--        Variable: compartment                -->
<!--=============================================-->
<!--assume through the dimension consistency check, all the rate
variable's unit's spatial part is based on same dimension or no spatial unit.....(mean just substance)-->
<xsl:variable name="compartment">
	<xsl:choose>
		<xsl:when test="count(exsl:node-set($dimension)/lu[spaUnit/@dimension='0'])=count(exsl:node-set($dimension)/lu)">
			<xsl:element name="compartment">
				<xsl:attribute name="id">
					<xsl:value-of select="'COMpartment'"/>
				</xsl:attribute>
				<xsl:attribute name="spatialDimensions">
					<xsl:value-of select="'3'"/>
				</xsl:attribute>
				<xsl:attribute name="size">
					<xsl:value-of select="'1'"/>
				</xsl:attribute>
				<xsl:attribute name="units">
					<xsl:value-of select="'litre'"/>
				</xsl:attribute>
			</xsl:element>
		</xsl:when>
		<xsl:otherwise>
	<xsl:for-each select="exsl:node-set($dimension)/lu[not(spaUnit/@dimension='0')]">
		<xsl:if test="position()='1'">
			<xsl:variable name="dimension">
				<xsl:value-of select="./spaUnit/@dimension"/>
			</xsl:variable>
			<xsl:variable name="d2_unit">
				<xsl:choose>
				<xsl:when test="$dimension='2'">
					<xsl:choose>
						<xsl:when test="exsl:node-set($listOfUnitDefinitions_3/unitDefinition[count(listOfUnits/unit)='1' and (listOfUnits/unit/@kind='meter' or listOfUnits/unit/@kind='metre') and listOfUnits/unit/@multiplier='1' and listOfUnits/unit/@scale='0' and listOfUnits/unit/@exponent='2' and listOfUnits/unit/@offset='0'])">
					<xsl:value-of select="exsl:node-set($listOfUnitDefinitions_3)/unitDefinition[count(listOfUnits/unit)='1' and (listOfUnits/unit/@kind='meter' or listOfUnits/unit/@kind='metre') and listOfUnits/unit/@multiplier='1' and listOfUnits/unit/@scale='0' and listOfUnits/unit/@exponent='2' and listOfUnits/unit/@offset='0']/@id"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="'square_meter'"/>
						</xsl:otherwise>
					</xsl:choose>				

				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="'none'"/>
				</xsl:otherwise>
				</xsl:choose>
			</xsl:variable>
			<xsl:element name="compartment">
				<xsl:attribute name="id">
					<xsl:value-of select="'COMpartment'"/>
				</xsl:attribute>
				<xsl:attribute name="spatialDimensions">
					<xsl:value-of select="$dimension"/>
				</xsl:attribute>
				<xsl:attribute name="size">
					<xsl:value-of select="'1'"/>
				</xsl:attribute>
				<xsl:attribute name="units">
					<xsl:choose>
						<xsl:when test="$dimension='1'">
							<xsl:value-of select="'meter'"/>
						</xsl:when>
						<xsl:when test="$dimension='2'">
							<xsl:value-of select="$d2_unit"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="'litre'"/>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:attribute>
			</xsl:element>
		</xsl:if>
	</xsl:for-each>
		</xsl:otherwise>
	</xsl:choose>
</xsl:variable>
<!--=============================================-->
<!--       Variable: square_meter                -->
<!--=============================================-->
<!--just in case, if the compartment unit is square_meter, 
then need to add-->

<xsl:variable name="square_meter">
	<xsl:choose>
		<xsl:when test="exsl:node-set($compartment)/compartment/@units='square_meter'">
			<xsl:if test="not(exsl:node-set($listOfUnitDefinitions_3)/unitDefinition[@id='square_meter'])">
				<xsl:element name="unitDefinition">
					<xsl:attribute name="id">
						<xsl:value-of select="'square_meter'"/>
					</xsl:attribute>
					<xsl:attribute name="name">
						<xsl:value-of select="'square_meter'"/>
					</xsl:attribute>
					<xsl:attribute name="component">
						<xsl:value-of select="'model'"/>
					</xsl:attribute>
					<xsl:element name="listOfUnits">
						<xsl:element name="unit">
							<xsl:attribute name="kind">
								<xsl:value-of select="'meter'"/>
							</xsl:attribute>
							<xsl:attribute name="multiplier">
								<xsl:value-of select="'1'"/>
							</xsl:attribute>
							<xsl:attribute name="scale">
								<xsl:value-of select="'0'"/>
							</xsl:attribute>
							<xsl:attribute name="exponent">
								<xsl:value-of select="'2'"/>
							</xsl:attribute>
							<xsl:attribute name="offset">
								<xsl:value-of select="'0'"/>
							</xsl:attribute>
						</xsl:element>
					</xsl:element>
				</xsl:element>
			</xsl:if>
		</xsl:when>
		<xsl:otherwise>
			<xsl:element name="unitDefinition">
				<xsl:attribute name="id">
					<xsl:value-of select="'nomeaning'"/>
				</xsl:attribute>
			</xsl:element>
		</xsl:otherwise>
	</xsl:choose>


</xsl:variable>
<!--=============================================-->
<!--        Variabel:  parameter_1               -->
<!--=============================================-->
<!--unique ori_id from variable_dic_2, and 
not belong to species_2,id
	delta_variable,var_id
	rate_variable, var_id
	time_variable, var_id-->
<!--parameter_1
parameter/
	id
	name
	component-->

<xsl:variable name="parameter_1">
	<xsl:for-each select="exsl:node-set($variable_dic_2)/lu[not(@ori_id=preceding-sibling::lu/@ori_id)]">
		<xsl:variable name="comp">
			<xsl:value-of select="@ori_component"/>
		</xsl:variable>
		<xsl:variable name="var">
			<xsl:value-of select="@ori_name"/>
		</xsl:variable>
		<xsl:variable name="id">
			<xsl:value-of select="@ori_id"/>
		</xsl:variable>

		<xsl:variable name="para">
		   <xsl:choose>
			<xsl:when test="exsl:node-set($species_2)/lu[@id=$id] or exsl:node-set($delta_variable)/lu[@ori_id=$id] or exsl:node-set($rate_variable)/lu[@var_id=$id]">
				<xsl:value-of select="'false'"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="'true'"/>
			</xsl:otherwise>
		   </xsl:choose>
		</xsl:variable>
		

		<xsl:if test="$para='true'">
				<xsl:element name="parameter">
					<xsl:attribute name="name">
						<xsl:value-of select="$var"/>
					</xsl:attribute>
					<xsl:attribute name="id">
						<xsl:value-of select="$id"/>
					</xsl:attribute>
					<xsl:attribute name="component">
						<xsl:value-of select="$comp"/>
					</xsl:attribute>
				</xsl:element>
		</xsl:if>

	</xsl:for-each>
</xsl:variable>

<!--=============================================-->
<!--        Variable:  parameter_2               -->
<!--=============================================-->
<!--parameter_1, list all the parameter
and then search from component/variable, to find each parameter's value and unit
parameter/
	id
	name
	units
	value (opt)
	constant='false' (if parameter be defined by assignmentRule or rateRule)-->
<xsl:variable name="parameter_2">
	<xsl:for-each select="cellml:model/cellml:component">
		<xsl:variable name="comp">
			<xsl:value-of select="@name"/>
		</xsl:variable>
<!-- <xsl:message> 
  <xsl:value-of select="concat('component = ', $comp, '  ')"/>
</xsl:message> -->

	   <xsl:for-each select="cellml:variable">
		<xsl:variable name="var">
			<xsl:value-of select="@name"/>
		</xsl:variable>
<!-- <xsl:message> 
  <xsl:value-of select="concat('variable = ', $var, '  ')"/>
</xsl:message> -->
		<xsl:if test="exsl:node-set($parameter_1)/parameter[@name=$var and @component=$comp]">
			<xsl:variable name="u">
				<xsl:value-of select="@units"/>
			</xsl:variable>
<!-- <xsl:message> 
  <xsl:value-of select="concat('cellml units = ', $u, '  ')"/>
</xsl:message> -->
			<xsl:variable name="unit_us">
                                  <xsl:choose>
					<xsl:when test="../cellml:units[@name=$u]">
						<xsl:value-of select="exsl:node-set($listOfUnitDefinitions_3)/unitDefinition[@name=$u and @component=$comp]/@id"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="$u"/>
					</xsl:otherwise>
				   </xsl:choose>
			</xsl:variable>
<!-- <xsl:message> 
  <xsl:value-of select="concat('sbml units = ', $unit_us, '  ')"/>
</xsl:message> -->
			<xsl:variable name="ID">
				<xsl:value-of select="exsl:node-set($parameter_1)/parameter[@name=$var and @component=$comp]/@id"/>
			</xsl:variable>
			<xsl:element name="parameter">
				<xsl:attribute name="id">
					<xsl:value-of select="exsl:node-set($parameter_1)/parameter[@name=$var and @component=$comp]/@id"/>
				</xsl:attribute>

				<xsl:attribute name="name">
					<xsl:value-of select="exsl:node-set($parameter_1)/parameter[@name=$var and @component=$comp]/@name"/>
				</xsl:attribute>
				<xsl:if test="@initial_value">
					<xsl:attribute name="value">
						<xsl:value-of select="@initial_value"/>
					</xsl:attribute>
				</xsl:if>
				<xsl:attribute name="units">
                                                  <xsl:choose>
                                                    <xsl:when test="@kind='liter'">
							  <xsl:value-of select="'litre'"/>
                                                    </xsl:when>
                                                    <xsl:when test="@kind='meter'">
							  <xsl:value-of select="'metre'"/>
                                                    </xsl:when>
                                                    <xsl:otherwise>
							  <xsl:value-of select="$unit_us"/>
                                                    </xsl:otherwise>
                                                  </xsl:choose>
				</xsl:attribute>
				<xsl:if test="exsl:node-set($rateRule_2)/rateRule[@variable=$ID] or exsl:node-set($assignmentRule_2)/assignmentRule[@variable=$ID]">
					<xsl:attribute name="constant">
						<xsl:value-of select="'false'"/>
					</xsl:attribute>
				</xsl:if>
			</xsl:element>

		</xsl:if>

	   </xsl:for-each>
	</xsl:for-each>
</xsl:variable>
<!--test-->
<!--
<xsl:template match="/">

	<xsl:for-each select="exsl:node-set($rate_time)/lu">
		<xsl:value-of select="concat(@var_id,'  UNIT ',@var_unit,'  DETAIL ',@kind,'  ',@multiplier,'  ',@scale,'  ',@exponent,'   ',@offset)"/>
		<xsl:value-of select="$newline"/>
	</xsl:for-each>
<xsl:value-of select="'this is dimension check'"/>
<xsl:value-of select="$newline"/>
<xsl:variable name="lu">
	<xsl:for-each select="exsl:node-set($species_2)/lu">
		<xsl:element name="lu">
		</xsl:element>
	</xsl:for-each>
</xsl:variable>
<xsl:for-each select="exsl:node-set($dimension)/lu">
	<xsl:value-of select="concat(@rate_id,'   ',@rate_name,'  ',@rate_component)"/>
	<xsl:value-of select="$newline"/>
	<xsl:for-each select="./spaUnit">
		<xsl:value-of select="concat(@kind,'M  ',@multiplier,' S ',@scale,' E  ',@exponent,' O  ',@offset)"/>
		<xsl:value-of select="$newline"/>
	</xsl:for-each>
</xsl:for-each>

<xsl:for-each select="exsl:node-set($time_unit)/unit">
	<xsl:value-of select="concat(@kind,'   ',@multiplier,'  ',@scale,'   ',@exponent,'   ',@offset)"/>
</xsl:for-each>
-->
<!--
<xsl:for-each select="exsl:node-set($listOfUnitDefinitions_2_2)/unitDefinition">
	<xsl:value-of select="@id"/>
	<xsl:value-of select="$newline"/>
	
	<xsl:for-each select="./listOfUnits/unit">
		<xsl:value-of select="concat(@kind,'   ',@multiplier,'  ',@scale,'   ',@exponent,'  ',@offset)"/>
		<xsl:value-of select="$newline"/>
	</xsl:for-each>
</xsl:for-each>

<xsl:value-of select="'ttttttttttttttttttttttttttttt'"/>
<xsl:value-of select="$newline"/>
<xsl:for-each select="exsl:node-set($variable_dic_2)/lu">
	<xsl:value-of select="concat(@var_id,'    ',@ori_id)"/>
	<xsl:value-of select="$newline"/>
</xsl:for-each>

</xsl:template>
-->
</xsl:stylesheet>


