Specifying notation

(Excerpt from "The MathML Handbook" by Pavi Sandhu)

An important issue in displaying mathematics is that different notations may be used for the same concept, depending on the context. XSLT transformations provide a good way to convert one notational form into another.

The tangent function

For example, the trigonometric function tan(x), is written as tg(x) in some countries. An author in such a country can create an XSLT stylesheet that will detect the occurrence of the <mi>tan<mi> element in a MathML document and convert it into a <mi>tg</mi> element instead. Abrowser will then render the element as tg(x). Hence, any MathML document that contains presentation markup involving the tangent function, such as the one in the following example, can be modified to use the notational form that the author prefers.

Example: A simple MathML document with presentation markup for the tan function.

<math>
  <mrow>
    <mi>tan</mi>
    <mrow><mo>(</mo><mi>x</mi><mo>)</mo></mrow>
  </mrow>
</math>

The following example shows an XSLT stylesheet that transforms a presentation MathML document by replacing all occurrences of tan with tg.

Example: An XSLT stylesheet that changes the name of the tangent function.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="math">
    <math><xsl:apply-templates/></math>
  </xsl:template>
  <xsl:template match="mrow">
    <mrow><xsl:apply-templates/></mrow>
  </xsl:template>
  <xsl:template match="mo">
    <mo><xsl:apply-templates/></mo>
  </xsl:template>
  <xsl:template match="mi">
    <xsl:choose>
      <xsl:when test="text()='tan'">
        <mi>tg</mi>
      </xsl:when>
      <xsl:otherwise>
        <mi><xsl:value-of select="."/></mi>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

This stylesheet contains templates for the math, mrow, and mo elements that simply implement an identity transformation; that is, they copy the content of any element they match into the output, without making any changes. Each of these templates also adds to the output explicit tags to replace the tags in the input document that are stripped away by the XSLT processor.

The key template in this stylesheet is the one that matches the mi element. This contains an xsl:choose element, which is the XSLT equivalent of a switch or case statement in programming. This element can contain any number of xsl:when elements and a single xsl:otherwise element. Each xsl:when element has a match attribute, which specifies a test condition. If the condition is true, the contents of that xsl:when element are processed; otherwise, the condition in the next xsl:when element is evaluated. If none of the xsl:when elements contains a match attribute that evaluates to true, then the statements in the xsl:otherwise element are processed.

In the above stylesheet, the first xsl:when element checks to see if the text contained in the mi element being processed is "tan". If so, the processor writes out the text "tg" to the output file. Otherwise, the xsl:otherwise element is processed. This element encloses the content of the mi element with a pair of mi tags and copies it to the output document. The net result of this template is therefore to replace any mi element that contains the text tan with an equivalent element that contains the text tg. All other mi elements are copied to the output document unchanged.

The result of applying the stylesheet to the above MathML document is given below (the XML declaration at the start of the output is added automatically by the XSLT processor):

<?xml version="1.0" encoding="utf-8"?>
<math>
  <mrow>
    <mi>tg</mi>
    <mrow>
      <mo>(</mo>
      <mi>x</mi>
      <mo>)</mo>
    </mrow>
  </mrow>
</math>

Binomial coefficients

Another common mathematical concept for which there are several different notations is the binomial coefficient, Binomial coefficient. This is also represented as Binomial coefficients in France in France and Binomial coefficient in Germany in Germany. An author who prefers one of these notations can use an XSLT transformation to detect the presence of any alternate notation for this concept, in a MathML document, and convert it into the desired form. The next example shows a simple MathML document to illustrate this behavior.

Example: A MathML document that contains the presentation markup for a binomial coefficient.

<math>
  <mrow>
    <mo>(</mo>
    <mfrac linethickness="0">
      <mi>n</mi>
      <mi>m</mi>
    </mfrac>
    <mo<)</mo>
  </mrow>
</math>

The stylesheet shown above will convert all instances of the markup for Binomial coefficient in a presentation MathML document into the markup for Binomial coefficient in France.

Example: An XSLT stylesheet that changes the notation for a binomial coefficient.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="math">
    <math><xsl:apply-templates/></math>
  </xsl:template>
  <xsl:template match="mrow">
    <mrow><xsl:apply-templates select="mfrac"/></mrow>
  </xsl:template>
  <xsl:template match="mo">
    <mo><xsl:apply-templates/></mo>
  </xsl:template>
  <xsl:template match="mi">
    <mi><xsl:apply-templates/></mi>
  </xsl:template>
  <xsl:template match="mn">
    <mn><xsl:apply-templates/></mn>
  </xsl:template>
  <xsl:template match="mfrac">
    <xsl:choose>
      <xsl:when test="@linethickness='0'">
        <msubsup>
          <mi>C</mi>
          <xsl:apply-templates select="*[2]"/>
          <xsl:apply-templates select="*[1]"/>
        </msubsup>
      </xsl:when>
      <xsl:otherwise>
        <mfrac><xsl:apply-templates/></mfrac>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

The stylesheet contains templates for the math, mrow, mn, mi, and mo elements. These templates simply copy the content of the matching element to the output document (after enclosing them in the appropriate tags). The key template in the stylesheet is the one that matches the mfrac element. This template uses the xsl:choose element to check if the currently selected mfrac element has the attribute linethickness set to 0. This condition is implemented by the <xsl:when test="@linethickness='0'"> element. Recall that @linethickness='0' is the XPath syntax for specifying an attribute called linethickness, whose value is 0.

If the mfrac element for which the template is being evaluated matches this condition, the statements inside the xsl:when element are evaluated. These statements replace the mfrac element with an msubsup element. The three arguments of the msubsup element are constructed using three separate statements. First, the literal text <mi>C</mi> is copied to the output document to serve as the first argument.

Next, the <xsl:apply-templates select="*[1]"/> element is called. This element applies all templates in the current context that match the value specified by the select attribute. This attribute has the value *[1], which is the XPath syntax for the first child element of the current node. Hence, the net effect of this statement is to copy the first argument of the mfrac element into the output document to serve as the third argument of the msubsup element.

Finally, the <xsl:apply-templates select="*[2]"/> element is called. This element takes the second argument of the mfrac element in the input document and copies it as the second argument of the msubsup element in the output document.

Applying the stylesheet to the above MathML document yields the following output:

<?xml version="1.0" encoding="utf-8"?>
<math>
  <mrow>
    <msubsup>
      <mi>C</mi>
      <mi>m</mi>
      <mi>n</mi>
    </msubsup>
  </mrow>
</math>

Hence, the net effect of the stylesheet is to convert the markup for Binomial coefficient into the markup for Binomial coefficient in France. You can easily modify the same stylesheet to produce the markup for the alternative notational form, Binomial coefficient in Germany, if desired. To do so, you would just reverse the order of two statements in the template for the mfrac element, as shown below:

<xsl:apply-templates select="*[1]"/>
<xsl:apply-templates select="*[2]"/>

This causes the first and second arguments of the mfrac element in the input document to become the second and third arguments of the msubsup element in the output, respectively.

Note that if the input document does not contain an <mfrac linethickness="0"> attribute, then the condition in the xsl:when element of the stylesheet evaluates to false, and the statements in the xsl:otherwise element are applied instead. This has the effect of simply copying the mfrac element unchanged into the output, which is what you want to happen.

   

<< back next >>

 

 

 

Tipp der data2type-Redaktion:
Zum Thema MathML bieten wir auch folgende Schulungen zur Vertiefung und professionellen Fortbildung an:

 

Copyright © CHARLES RIVER MEDIA, INC., Massachusetts (USA) 2003
Printing of the online version is permitted exclusively for private use. Otherwise this chapter from the book "The MathML Handbook" is subject to the same provisions as those applicable for the hardcover edition: The work including all its components is protected by copyright. All rights reserved, including reproduction, translation, microfilming as well as storage and processing in electronic systems.


CHARLES RIVER MEDIA, INC., 20 Downer Avenue, Suite 3, Hingham, Massachusetts 02043, United States of America