<?xml version="1.0" encoding="UTF-8"?>

<!--
FILE : cellml_equation_extractor.xsl

CREATED : 23 July 2001

LAST MODIFIED : 25 July 2001

AUTHOR : Warren Hedley (w.hedley@auckland.ac.nz)
         Department of Engineering Science
         The University of Auckland

DESCRIPTION : This stylesheet can be applied to a CellML document to extract
  all equations from it, and construct an HTML file that can be
  rendered by IBM's techexplorer plug-in, with one equation per line.

CHANGES :

TODO : At some point it would be nice if this output implied math generated
  by @delta_variable attributes on <role> elements.
-->

<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">


<xsl:output method="xml" indent="yes" />

<!--
  If this parameter is set to anything other 'no', then the equations defined
  inside any <mathml:math> elements inside extension elements will be
  reproduced in the output, and labelled with EXTENSION.
-->
<xsl:param name="INCLUDE_MATH_IN_EXTENSION_ELEMENTS" select="'no'" />

<xsl:variable name="CELLML_NAMESPACE_URI"
    select="'http://www.cellml.org/cellml/1.0#'" />
<xsl:variable name="MATHML_NAMESPACE_URI"
    select="'http://www.w3.org/1998/Math/MathML'" />

<xsl:template match="/">
  <math xmlns="http://www.w3.org/1998/Math/MathML">
    <mtable columnalign="left">
      <xsl:apply-templates />
    </mtable>
  </math>
</xsl:template>


<xsl:template match="cellml:component">
  <mtr xmlns="http://www.w3.org/1998/Math/MathML">
    <mtd>
      <mtext>
        <xsl:text>Equations for component '</xsl:text>
        <xsl:value-of select="@name" />
        <xsl:text>'</xsl:text>
      </mtext>
    </mtd>
  </mtr>
  <xsl:apply-templates />
</xsl:template>


<xsl:template match="mathml:math">
  <xsl:for-each select="*">
    <xsl:variable name="ancestor_extension_elements"
        select="ancestor::*[namespace-uri() != $CELLML_NAMESPACE_URI and
        namespace-uri() != $MATHML_NAMESPACE_URI]" />
    <xsl:choose>
      <!--
        Is there an ancestor element in an extension namespace.
      -->
      <xsl:when test="$ancestor_extension_elements">
        <!--
          Only reproduce the math in the output if the 
          $INCLUDE_MATH_IN_EXTENSION_ELEMENTS is set. Prefix it with
          'EXTENSION'.
        -->
        <xsl:if test="$INCLUDE_MATH_IN_EXTENSION_ELEMENTS != 'no'">
          <mtr xmlns="http://www.w3.org/1998/Math/MathML">
            <mtd>
              <mtext>
                <xsl:text>EXTENSION:&#160;</xsl:text>
              </mtext>
              <xsl:apply-templates select="." />
            </mtd>
          </mtr>
        </xsl:if>
      </xsl:when>
      <!--
        If the grandparent of the current element is <cellml:component> or
        <cellml:role> then indent this equation by four spaces, so that it
        appears to be *inside* the "Equations for component 'bob'" string.
      -->
      <xsl:when test="(local-name(../..) = 'component' or
          local-name(../..) = 'role') and
          namespace-uri(../..) = $CELLML_NAMESPACE_URI">
        <mtr xmlns="http://www.w3.org/1998/Math/MathML">
          <mtd>
            <mtext>
              <xsl:text>&#160;&#160;&#160;&#160;</xsl:text>
            </mtext>
            <xsl:apply-templates select="." />
          </mtd>
        </mtr>
      </xsl:when>
      <!--
        Otherwise we're at a <mathml:math> element which is in an unusual
        place - output with an 'UNEXPECTED EQUATION' prefix.
      --> 
      <xsl:otherwise>
        <mtr xmlns="http://www.w3.org/1998/Math/MathML">
          <mtd>
            <mtext>
              <xsl:text>UNEXPECTED&#160;EQUATION:&#160;</xsl:text>
            </mtext>
            <xsl:apply-templates select="." />
          </mtd>
        </mtr>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:for-each>
</xsl:template>


<xsl:template match="mathml:*">
  <xsl:element
      name="{local-name()}"
      namespace="http://www.w3.org/1998/Math/MathML">
    <xsl:apply-templates select="@mathml:* | @*[namespace-uri() = '']" />
    <xsl:apply-templates />
  </xsl:element>
</xsl:template>


<xsl:template match="mathml:ci">
  <mi mathcolor="#0000AA" xmlns="http://www.w3.org/1998/Math/MathML">
    <xsl:apply-templates select="@mathml:* | @*[namespace-uri() = '']" />
    <xsl:apply-templates />
  </mi>
</xsl:template>


<xsl:template match="mathml:*/@*">
  <xsl:attribute name="{local-name()}">
    <xsl:value-of select="." />
  </xsl:attribute>
</xsl:template>


<xsl:template match="mathml:*/text()">
  <xsl:value-of select="normalize-space(.)" />
</xsl:template>


<xsl:template match="text()" />


</xsl:stylesheet>
