<xsl:stylesheet
  xmlns:math="http://www.w3.org/1998/Math/MathML"
  xmlns:mdf="http://www.mathdox.org/MathDox/Functions"
  xmlns:om="http://www.openmath.org/OpenMath"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0"
  >

  <!--
    Utility functions/templates
  -->

  <!-- check if all the argument nodes are points -->
  <xsl:function name='mdf:all_points_without_conditions' as='xs:boolean'>
    <xsl:param name='points' as='node()*'/>
    <xsl:variable name='all_oma_or_omv' as="xs:boolean"
      select='not($points[not (self::OMA or self::om:OMA or self::OMV or
	self::om:OMV)])' 
    />
    <xsl:variable name='all_oms' as="xs:boolean"
      select='not($points[(self::OMA or self::om:OMA) and 
      *[1][not (self::OMS or self::om:OMS)]])' 
    />
    <xsl:variable name='all_points' as="xs:boolean"
      select='not($points[(self::OMA or self::om:OMA) and 
      *[1][not (@name="point" and @cd="plangeo1")]])' 
    />
    <xsl:variable name='all_points_no_conditions' as="xs:boolean"
      select='not($points[(self::OMA or self::om:OMA) and *[3]])' 
    />
    <xsl:value-of select='$all_oma_or_omv and $all_oms and $all_points and 
      $all_points_no_conditions'
    />
  </xsl:function>

  <!--
    mdf:and n-ary and as fn:min
  -->
  <xsl:function name="mdf:and" as='xs:boolean'>
    <xsl:param name='booleanlist' as='xs:boolean*'/>
    <xsl:copy-of select='min($booleanlist)'/>
  </xsl:function>

  <!-- constructs a list with ',' and 'and' -->
  <xsl:function name='mdf:makelist'>
    <xsl:param name='list' as='node()*'/>
    <xsl:for-each select='$list'>
      <xsl:choose>
        <xsl:when test='position() = 1'/>
        <xsl:when test='position() = last()'>
          <math:mtext>
            <!-- U+00A0 non breakable space -->
            <xsl:text>&#xA0;and&#xA0;</xsl:text>
          </math:mtext>
        </xsl:when>
        <xsl:otherwise>
          <math:mtext>
            <!-- U+00A0 non breakable space -->
            <xsl:text>,&#xA0;</xsl:text>
          </math:mtext>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:apply-templates select='.'/>
    </xsl:for-each>
  </xsl:function>

  <!--
    mdf:om_applied_symbol 
    function that is true if the argument is an <OMA> containing a <OMS> of the
    specified cd and name
  -->

  <xsl:function name='mdf:om_applied_symbol'>
    <xsl:param name='testnode' as='node()'/>
    <xsl:param name='cd' as='xs:string'/>
    <xsl:param name='name' as='xs:string'/>

    <xsl:variable name='is_oma' as='xs:boolean' 
      select='$testnode[self::OMA or self::om:OMA]'
    />
    <xsl:variable name='is_oms' as='xs:boolean' 
      select='$testnode/*[1][self::OMS or self::om:OMS]'
    />
    <xsl:variable name='is_symbol' as='xs:boolean' 
      select='$testnode/*[1][@cd=$cd and $name=$name]'
    />
    <xsl:copy-of select='$is_oma and $is_oms and $is_symbol'/>
  </xsl:function>

  <!--
    mdf:or 
    n-ary or as fn:max
  -->
  <xsl:function name="mdf:or" as='xs:boolean'>
    <xsl:param name='booleanlist' as='xs:boolean*'/>
    <xsl:copy-of select='max($booleanlist)'/>
  </xsl:function>

</xsl:stylesheet> 
