<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!-- ##############################################################
                            cellml_variable.xsl

############################################################## -->
<!--=================================================-->
<!--        "SBML" to "CellML" 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 inquiry regarding this file, please contact Biomodels.net support (biomodels-net-support at lists.sourceforge.net)

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



<!--========================================-->
<!--              Variable: compartment     -->
<!--========================================-->
<!--this is the variable for compartment    -->
<!--element lu, attribute:
	1 id , the original id
	2 size (if has @size)
	3 spatialDimensions (if have, use; not have default=3)
	4 units (if have, use; not have from @spatialDimensions judge)
	5 outside (if has @outside)-->
<!--how to judge compartment unit:
	if have @units,   use its value
	if do not have @units:
		1 has @spatialDimensions,            default unit
				3			volume
				2			area
				1			length
				0			dimensionless
		2 no @spatialDimensions,  
			default spatial is 3, so units is volume-->
<!--afterward use this variable, remember, @size and @outside is optional -->

<xsl:variable name="compartments">
	<xsl:for-each select="descendant::sbml:listOfCompartments/sbml:compartment">
		<xsl:element name="lu">
			<xsl:attribute name="id">
<xsl:value-of select="@id"/>
</xsl:attribute>
			<xsl:if test="@size">
				<xsl:attribute name="size">
<xsl:value-of select="@size"/>
</xsl:attribute>
			</xsl:if>
			<xsl:attribute name="spatialDimensions">
				<xsl:choose>
					<xsl:when test="@spatialDimensions">
						<xsl:value-of select="@spatialDimensions"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="'3'"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
			<xsl:attribute name="units">
				<xsl:choose>
					<xsl:when test="@units">
						<xsl:value-of select="@units"/>
					</xsl:when>
					<xsl:otherwise>
<!--judge from @spacialDimensions-->
						<xsl:choose>
							<xsl:when test="@spatialDimensions">
								<xsl:if test="@spatialDimensions='3'">
									<xsl:value-of select="'volume'"/>
								</xsl:if>
								<xsl:if test="@spatialDimensions='2'">
									<xsl:value-of select="'area'"/>
								</xsl:if>
								<xsl:if test="@spatialDimensions='1'">
									<xsl:value-of select="'length'"/>
								</xsl:if>
								<xsl:if test="@spatialDimensions='0'">
									<xsl:value-of select="'dimensionless'"/>
								</xsl:if>
							</xsl:when>
							<xsl:otherwise>
<!--default spacialDimensions=3-->
								<xsl:value-of select="'volume'"/>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
			<xsl:if test="@outside">
				<xsl:attribute name="outside">
					<xsl:value-of select="@outside"/>
				</xsl:attribute>
			</xsl:if>
		</xsl:element>
	</xsl:for-each>
</xsl:variable>


<!--========================================-->
<!--              Variable: builtin         -->
<!--========================================-->
<!--just put the 5 builtin units' names in -->
<!--element lu:
	attributes: unit,	will be the unit element, follow by unit name
		    units,	will be the unit's attribute, for basic unit
		    prefix,    
		    exponent,
		    multiplier
		    offset,-->
<xsl:variable name="builtin">
	<xsl:element name="lu">
		<xsl:attribute name="unit">
<xsl:value-of select="'volume'"/>
</xsl:attribute>
		<xsl:attribute name="units">
<xsl:value-of select="'litre'"/>
</xsl:attribute>
		<xsl:attribute name="prefix">
<xsl:value-of select="'0'"/>
</xsl:attribute>
		<xsl:attribute name="exponent">
<xsl:value-of select="'1'"/>
</xsl:attribute>
		<xsl:attribute name="multiplier">
<xsl:value-of select="'1'"/>
</xsl:attribute>
		<xsl:attribute name="offset">
<xsl:value-of select="'0'"/>
</xsl:attribute>
	</xsl:element>
	<xsl:element name="lu">
		<xsl:attribute name="unit">
<xsl:value-of select="'substance'"/>
</xsl:attribute>
		<xsl:attribute name="units">
<xsl:value-of select="'mole'"/>
</xsl:attribute>
		<xsl:attribute name="prefix">
<xsl:value-of select="'0'"/>
</xsl:attribute>
		<xsl:attribute name="exponent">
<xsl:value-of select="'1'"/>
</xsl:attribute>
		<xsl:attribute name="multiplier">
<xsl:value-of select="'1'"/>
</xsl:attribute>
		<xsl:attribute name="offset">
<xsl:value-of select="'0'"/>
</xsl:attribute>

	</xsl:element>
	<xsl:element name="lu">
		<xsl:attribute name="unit">
<xsl:value-of select="'area'"/>
</xsl:attribute>
		<xsl:attribute name="units">
<xsl:value-of select="'metre'"/>
</xsl:attribute>
		<xsl:attribute name="prefix">
<xsl:value-of select="'0'"/>
</xsl:attribute>
		<xsl:attribute name="exponent">
<xsl:value-of select="'2'"/>
</xsl:attribute>
		<xsl:attribute name="multiplier">
<xsl:value-of select="'1'"/>
</xsl:attribute>
		<xsl:attribute name="offset">
<xsl:value-of select="'0'"/>
</xsl:attribute>

	</xsl:element>
	<xsl:element name="lu">
		<xsl:attribute name="unit">
<xsl:value-of select="'length'"/>
</xsl:attribute>
		<xsl:attribute name="units">
<xsl:value-of select="'metre'"/>
</xsl:attribute>
		<xsl:attribute name="prefix">
<xsl:value-of select="'0'"/>
</xsl:attribute>
		<xsl:attribute name="exponent">
<xsl:value-of select="'1'"/>
</xsl:attribute>
		<xsl:attribute name="multiplier">
<xsl:value-of select="'1'"/>
</xsl:attribute>
		<xsl:attribute name="offset">
<xsl:value-of select="'0'"/>
</xsl:attribute>

	</xsl:element>
	<xsl:element name="lu">
		<xsl:attribute name="unit">
<xsl:value-of select="'time'"/>
</xsl:attribute>
		<xsl:attribute name="units">
<xsl:value-of select="'second'"/>
</xsl:attribute>
		<xsl:attribute name="prefix">
<xsl:value-of select="'0'"/>
</xsl:attribute>
		<xsl:attribute name="exponent">
<xsl:value-of select="'1'"/>
</xsl:attribute>
		<xsl:attribute name="multiplier">
<xsl:value-of select="'1'"/>
</xsl:attribute>
		<xsl:attribute name="offset">
<xsl:value-of select="'0'"/>
</xsl:attribute>

	</xsl:element>
</xsl:variable>

<!--========================================-->
<!--              Variable: allUnits        -->
<!--========================================-->
<!--all the units:
	1 uninfluenced built-in units
	2 item (cellml do not have, so 1 item=1/(6.023*10^23) mole)
	3 user-defined units (whether or not they influence built in)
	4 all species units, use species id_unit as unit name
	5 flux , (units for delta-variable)
		notice: in SBML, the unit for kineticLaw is substance/time
		so unit for delta-variable is the same.
		and substance and time use the built-in units, unless user redefined it-->
<!--the structure will like cellml units
element units:  
	attribute: name
	element unit:
		attribute: (cellml name and (default))	        (name in SBML and (default))
				units				kind
				prefix(0)			scale(0)
				exponent(1)			exponent(1)
				multiplier(1)			multiplier(1)
				offset(0)			offset(0)		-->
<!--Species Unit
				for species:
					1 compartment's @spatialDimensions!=0
							AND
					  @hasOnlySubstanceUnits='false' (default=false)

					  units=(substanceUnits/spatialSizeUnits)


					2 @spatialDimensions=0
						OR
					  @hasOnlySubstanceUnits='true'

					  units=(substanceUnits)

					NOTE: substanceUnits (default) ='substance'
					      spatialSizeUnits (default)=the unit of the compartment
-->
<!--step:
	1 search in listOfUnitDefinitions, check if this id is the same as one in variable builtin.
	  if true write in the variable, touchedBU
	2 write from builtin, the units that is not touched
	3 write unit for item
	4 write all the self-defined unit
	5 write all the species units, no matter it is only substanceUnit, or substanceUnit/spatialSizeUnit-->
	
<!--intermediate variable, touchedBU, regarding the self-defined units which influenced the built-in Units-->
<xsl:variable name="touchedBU">
	<xsl:for-each select="descendant::sbml:listOfUnitDefinitions/sbml:unitDefinition">
		<xsl:variable name="ID">
			<xsl:value-of select="@id"/>
		</xsl:variable>
		<xsl:if test="exsl:node-set($builtin)/lu[@unit=$ID]">
<!--mean this unit does not influence built-in-->
<!--so write into variable touchedBU-->
			<xsl:element name="lu">
				<xsl:attribute name="unit">
					<xsl:value-of select="$ID"/>
				</xsl:attribute>
			</xsl:element>
		</xsl:if>
	</xsl:for-each>
</xsl:variable>

<!--          variable, allUnits         -->
<xsl:variable name="allUnits">
<!--first, search in builtin, write the untouched-->
	<xsl:for-each select="exsl:node-set($builtin)/lu">
		<xsl:variable name="ID">
			<xsl:value-of select="@unit"/>
		</xsl:variable>
		<xsl:if test="not(exsl:node-set($touchedBU)/lu[@unit=$ID])">
			<xsl:element name="units">
				<xsl:attribute name="name">
					<xsl:value-of select="$ID"/>
				</xsl:attribute>
				<xsl:element name="unit">
					<xsl:attribute name="units">
						<xsl:value-of select="@units"/>
					</xsl:attribute>
					<xsl:attribute name="prefix">
						<xsl:value-of select="@prefix"/>
					</xsl:attribute>
					<xsl:attribute name="exponent">
						<xsl:value-of select="@exponent"/>
					</xsl:attribute>
					<xsl:attribute name="multiplier">
						<xsl:value-of select="@multiplier"/>
					</xsl:attribute>
					<xsl:attribute name="offset">
						<xsl:value-of select="@offset"/>
					</xsl:attribute>
				</xsl:element>
			</xsl:element>
		</xsl:if>
	</xsl:for-each>
<!--second built unit for item-->
	<xsl:element name="units">
		<xsl:attribute name="name">
<xsl:value-of select="'item'"/>
</xsl:attribute>
		<xsl:element name="unit">
			<xsl:attribute name="units">
<xsl:value-of select="'mole'"/>
</xsl:attribute>
			<xsl:attribute name="prefix">
<xsl:value-of select="'0'"/>
</xsl:attribute>
			<xsl:attribute name="exponent">
<xsl:value-of select="'-1'"/>
</xsl:attribute>
			<xsl:attribute name="multiplier">
<xsl:value-of select="'6.023e23'"/>
</xsl:attribute>
			<xsl:attribute name="offset">
<xsl:value-of select="'0'"/>
</xsl:attribute>
		</xsl:element>
	</xsl:element>
<!--third list out all the self-defined unit-->
	<xsl:for-each select="descendant::sbml:listOfUnitDefinitions/sbml:unitDefinition">
		<xsl:element name="units">
			<xsl:attribute name="name">
<xsl:value-of select="@id"/>
</xsl:attribute>
			<xsl:for-each select="sbml:listOfUnits/sbml:unit">
				<xsl:element name="unit">
					<xsl:attribute name="units">
<xsl:value-of select="@kind"/>
</xsl:attribute>
					<xsl:attribute name="prefix">
						<xsl:if test="@scale">
							<xsl:value-of select="@scale"/>
						</xsl:if>
						<xsl:if test="not(@scale)">
							<xsl:value-of select="'0'"/>
						</xsl:if>
					</xsl:attribute>
					<xsl:attribute name="exponent">
						<xsl:if test="@exponent">
							<xsl:value-of select="@exponent"/>
						</xsl:if>
						<xsl:if test="not(@exponent)">
							<xsl:value-of select="'1'"/>
						</xsl:if>
					</xsl:attribute>
					<xsl:attribute name="multiplier">
						<xsl:if test="@multiplier">
							<xsl:value-of select="@multiplier"/>
						</xsl:if>
						<xsl:if test="not(@multiplier)">
							<xsl:value-of select="'1'"/>
						</xsl:if>
					</xsl:attribute>
					<xsl:attribute name="offset">
						<xsl:if test="@offset">
							<xsl:value-of select="@offset"/>
						</xsl:if>
						<xsl:if test="not(@offset)">
							<xsl:value-of select="'0'"/>
						</xsl:if>
					</xsl:attribute>
			</xsl:element>
			</xsl:for-each>
		</xsl:element>
	</xsl:for-each>
<!--Fourth,all the units for each species -->
      <xsl:for-each select="descendant::sbml:listOfSpecies/sbml:species">
                <xsl:variable name="ID">
<xsl:value-of select="@id"/>
</xsl:variable>
                <xsl:variable name="cID">
<xsl:value-of select="@compartment"/>
</xsl:variable>
                <xsl:variable name="concen">
<!--Boolean variable, if species'unit is concentration, true-->
                        <xsl:choose>
                                <xsl:when test="exsl:node-set($compartments)/lu[@oid=$cID]/@spatialDimensions!='0'">
                                        <xsl:choose>
                                                <xsl:when test="not(@hasOnlySubstanceUnits='true')">
                                                        <xsl:value-of select="'true'"/>
                                                </xsl:when>
                                                <xsl:otherwise>
                                                        <xsl:value-of select="'false'"/>
                                                </xsl:otherwise>
                                        </xsl:choose>
                                </xsl:when>
                                <xsl:otherwise>
                                        <xsl:value-of select="'false'"/>
                                </xsl:otherwise>
                        </xsl:choose>
                </xsl:variable>
                <xsl:variable name="subUnit">
                        <xsl:choose>
                                <xsl:when test="@substanceUnits">
                                        <xsl:value-of select="@substanceUnits"/>
                                </xsl:when>
                                <xsl:otherwise>
                                        <xsl:value-of select="'substance'"/>
                                </xsl:otherwise>
                        </xsl:choose>
                </xsl:variable>
                <xsl:variable name="sizeUnit">
                        <xsl:choose>
                                <xsl:when test="@spatialSizeUnits">
                                        <xsl:value-of select="@spatialSizeUnits"/>
                                </xsl:when>
                                <xsl:otherwise>
                                        <xsl:value-of select="exsl:node-set($compartments)/lu[@oid=$cID]/@units"/>
                                </xsl:otherwise>
                        </xsl:choose>
                </xsl:variable>
		<xsl:element name="units">
			<xsl:attribute name="name">
<xsl:value-of select="concat($ID,'_unit')"/>
</xsl:attribute>
			<xsl:choose>
				<xsl:when test="$concen='true'">
<!--then,units have two element unit for subUnit and sizeUnit-->
					<xsl:element name="unit">
						<xsl:attribute name="units">
							<xsl:value-of select="$subUnit"/>
						</xsl:attribute>
						<xsl:attribute name="prefix">
<xsl:value-of select="'0'"/>
</xsl:attribute>
						<xsl:attribute name="exponent">
<xsl:value-of select="'1'"/>
</xsl:attribute>
						<xsl:attribute name="multiplier">
<xsl:value-of select="'1'"/>
</xsl:attribute>
						<xsl:attribute name="offset">
<xsl:value-of select="'0'"/>
</xsl:attribute>
					</xsl:element>
					
					<xsl:element name="unit">
                                                <xsl:attribute name="units">
                                                        <xsl:value-of select="$sizeUnit"/>
                                                </xsl:attribute>
                                                <xsl:attribute name="prefix">
<xsl:value-of select="'0'"/>
</xsl:attribute>
                                                <xsl:attribute name="exponent">
<xsl:value-of select="'-1'"/>
</xsl:attribute>
                                                <xsl:attribute name="multiplier">
<xsl:value-of select="'1'"/>
</xsl:attribute>
                                                <xsl:attribute name="offset">
<xsl:value-of select="'0'"/>
</xsl:attribute>
                                        </xsl:element>
				</xsl:when>
				<xsl:otherwise>
<!--$concen=false, mean unit should be substance unit-->
					<xsl:element name="unit">
						<xsl:attribute name="units">
                                                        <xsl:value-of select="$subUnit"/>
                                                </xsl:attribute>
                                                <xsl:attribute name="prefix">
<xsl:value-of select="'0'"/>
</xsl:attribute>
                                                <xsl:attribute name="exponent">
<xsl:value-of select="'1'"/>
</xsl:attribute>
                                                <xsl:attribute name="multiplier">
<xsl:value-of select="'1'"/>
</xsl:attribute>
                                                <xsl:attribute name="offset">
<xsl:value-of select="'0'"/>
</xsl:attribute>
					</xsl:element>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:element>
	</xsl:for-each>
<!--five, flux-->
	<xsl:element name="units">
		<xsl:attribute name="name">
<xsl:value-of select="'flux'"/>
</xsl:attribute>
		<xsl:element name="unit">
			<xsl:attribute name="units">
<xsl:value-of select="'substance'"/>
</xsl:attribute>
			<xsl:attribute name="prefix">
<xsl:value-of select="'0'"/>
</xsl:attribute>
			<xsl:attribute name="exponent">
<xsl:value-of select="'1'"/>
</xsl:attribute>
			<xsl:attribute name="multiplier">
<xsl:value-of select="'1'"/>
</xsl:attribute>
			<xsl:attribute name="offset">
<xsl:value-of select="'0'"/>
</xsl:attribute>
		</xsl:element>
		<xsl:element name="unit">
			<xsl:attribute name="units">
<xsl:value-of select="'time'"/>
</xsl:attribute>
			<xsl:attribute name="prefix">
<xsl:value-of select="'0'"/>
</xsl:attribute>
			<xsl:attribute name="exponent">
<xsl:value-of select="'-1'"/>
</xsl:attribute>
			<xsl:attribute name="multiplier">
<xsl:value-of select="'1'"/>
</xsl:attribute>
			<xsl:attribute name="offset">
<xsl:value-of select="'0'"/>
</xsl:attribute>
		</xsl:element>
	</xsl:element>
</xsl:variable>

<!--=============================================-->
<!--            Variable: assignmentRule         -->
<!--=============================================-->
<xsl:variable name="assignmentRule">
        <xsl:for-each select="descendant::sbml:listOfRules/sbml:assignmentRule">
                <xsl:element name="lu">
                        <xsl:attribute name="variable">
                                <xsl:value-of select="@variable"/>
                        </xsl:attribute>
                </xsl:element>
        </xsl:for-each>
</xsl:variable>

<!--=============================================-->
<!--            Variable: rateRule               -->
<!--=============================================-->
<xsl:variable name="rateRule">
        <xsl:for-each select="descendant::sbml:listOfRules/sbml:rateRule">
                <xsl:element name="lu">
                        <xsl:attribute name="variable">
                                <xsl:value-of select="@variable"/>
                        </xsl:attribute>
                </xsl:element>
        </xsl:for-each>
</xsl:variable>

<!--=============================================-->
<!--            Variable: assign_rateRule        -->
<!--=============================================-->
<!--assign all the variables be defined by assignment and rate rule-->
<xsl:variable name="assign_rateRule">
	<xsl:for-each select="descendant::sbml:listOfRules/sbml:assignmentRule|descendant::sbml:listOfRules/sbml:rateRule">
		<xsl:element name="lu">
			<xsl:attribute name="variable">
				<xsl:value-of select="@variable"/>
			</xsl:attribute>
		</xsl:element>
	</xsl:for-each>
</xsl:variable>

<!--=============================================-->
<!--            Variable: algebraicRule          -->
<!--=============================================-->
<!--assign the variable be defined in ci by algebraicRule-->
<!--each algebraicRule will be appear as an element, named lu, with attribute name=algebraic_position()
	lu	name=algebraic_position()
		element lulu
			attribute:  variable= variable appeared in ci
                                                  AND
                                              not in assign_rateRule
				    -->
<!--so first, set up a variable, get together all the ci variables-->
<xsl:variable name="algebraicRule">
	<xsl:for-each select="descendant::sbml:listOfRules/sbml:algebraicRule">
		<xsl:element name="lu">
			<xsl:attribute name="name">
				<xsl:value-of select="concat('algebraic_',position())"/>
			</xsl:attribute>
			<xsl:for-each select="descendant::math:ci">
				<xsl:variable name="CI">
					<xsl:value-of select="."/>
				</xsl:variable>
				<xsl:element name="lulu">
					<xsl:attribute name="variable">
						<xsl:value-of select="$CI"/>
					</xsl:attribute>
				</xsl:element>
			</xsl:for-each>
		</xsl:element>
	</xsl:for-each>
</xsl:variable>


<!--=============================================-->
<!--     Variable:globalSimple                   -->
<!--=============================================-->
<!--all global stuffs will appear as a component, but in different form.
Every component is an object, which comes from:
	compartment  (name=id, variable-itself=out)
	species		(name=id, variable-itself=out)
	global parameters	(name=id, variable-itself=out)

	assignmentRule (name as the variable, mean name is id for c/s/p, variable-self=out)
	rateRule       (name as the variable, from c/s/p, variable-self=out)
		(algebraicRule  is treated as a statement, so all the ci in this rule is 'in')

	reaction (name is reaction id, the delta-variables are out)-->

<!--global stuffs can be divided into:
1 defined by rule(assignmentRule or rateRule)
2 -->
<!--so no matter global stuff, c/s/p, constant or not,just separate them from defined by rule
(assignmentRule, or rateRule) or not
if not so, it is the simple global, whose component form like:
  	component name=id 
		variable name=id
			units= check from allVariables
			public_interface=out
			initial_value= optional (if have)-->



<!--========================================-->
<!--              Variable: gVariables     -->
<!--========================================-->
<!--collect all the global variables-->
<!--this variable composed of:
	1 compartment
	2 global parameters
	3 species-->
<!--take the form as:
	element variable:
		attribute:
			name    id (for global id is unique)
			units	for compartment, fetch from variable compartments
				for parameter, if have use, do not have, use 'dimensionless'
				for species: just write id_unit
					     because all of their units have been defined in allUnits
			initial_value	(optional, if have use.)
			-->
<!--STEPS:
	1 search in variable compartment
	2 search in global parameters (under model !!!)
		use id and 'global' check workid in allName
		units
		initial
	3 search in species
		first, set a variable remember compartment id, then can check everything in compartment var
		name use id and 'global' check in allName
		units, id_unit!
		initial
		-->

<xsl:variable name="gVariables">

<!--first compartment-->
	<xsl:for-each select="exsl:node-set($compartments)/lu">
		<xsl:element name="variable">
			<xsl:attribute name="name">
<xsl:value-of select="@id"/>
</xsl:attribute>
			<xsl:attribute name="units">
<xsl:value-of select="@units"/>
</xsl:attribute>
			<xsl:if test="@size">
				<xsl:attribute name="initial_value">
<xsl:value-of select="@size"/>
</xsl:attribute>
			</xsl:if>
		</xsl:element>
	</xsl:for-each>

<!--second global parameters-->
	<xsl:for-each select="descendant::sbml:model/sbml:listOfParameters/sbml:parameter">
		<xsl:variable name="ID">
<xsl:value-of select="@id"/>
</xsl:variable>
		<xsl:element name="variable">
			<xsl:attribute name="name">
				<xsl:value-of select="@id"/>
			</xsl:attribute>
			<xsl:attribute name="units">
				<xsl:choose>
					<xsl:when test="@units">
						<xsl:value-of select="@units"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="'dimensionless'"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
			<xsl:if test="@value">
				<xsl:attribute name="initial_value">
<xsl:value-of select="@value"/>
</xsl:attribute>			
			</xsl:if>
		</xsl:element>
	</xsl:for-each>
<!--third species-->
	<xsl:for-each select="descendant::sbml:listOfSpecies/sbml:species">
		<xsl:variable name="ID">
<xsl:value-of select="@id"/>
</xsl:variable>

		<xsl:element name="variable">
			<xsl:attribute name="name">
				<xsl:value-of select="@id"/>
			</xsl:attribute>
			<xsl:attribute name="units">
				<xsl:value-of select="concat($ID,'_unit')"/>
			</xsl:attribute>
			<xsl:if test="@initialAmount">
				<xsl:attribute name="initial_value">
<xsl:value-of select="@initialAmount"/>
</xsl:attribute>
			</xsl:if>
			<xsl:if test="@initialConcentration">
				<xsl:attribute name="initial_value">
<xsl:value-of select="@initialConcentration"/>
</xsl:attribute>
			</xsl:if>
		</xsl:element>
	</xsl:for-each>

</xsl:variable>



</xsl:stylesheet>

