Search code examples
xmlxsltmathml

How can I implement a Presentation MathML to Content MathML translator using XSLT?


I want to implement a P2C translator using XSLT. E.g.:

<mrow>
  <mi>x</mi>
  <mo>+</mo>
  <msup>
    <mi>y</mi>
    <mn>3</mn>
  </msup>
</mrow>

into the C-MathML term.

<apply>
  <csymbol>mrow</csymbol>
  <ci>x</ci>
  <csymbol>+</csymbol>
  <apply>
    <csymbol>msup</csymbol>
    <ci>y</ci>
    <cn>3</cn>
  </apply>
</apply>

The rules are as follows:

1) <mi> goes to <ci>

2) <mo> goes to <csymbol>

3) all other elements <xxx> go to <apply><csymbol>xxx</csymbol> ... </apply>

Below is what I've written so far, but I have difficulties implementing rule number 3, as I don't have much experience with XSLT. Can anyone help with this?

<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:mml="http://www.w3.org/1998/Math/MathML" >

  <xsl:output method="xml" />

  <xsl:template mode="p2c" match="*">
    <xsl:copy>
      <xsl:copy-of select="@" />
      <xsl:apply-templates mode="p2c"/>
    </xsl:copy>
  </xsl:template>

  <!-- mn -->

  <xsl:template mode="p2c" match="mml:mn">
    <mml:cn><xsl:apply-templates mode="p2c"/></mml:cn>
  </xsl:template>

  <!-- mo -->
  <xsl:template mode="p2c" match="mml:mo">
    <mml:csymbol><xsl:apply-templates mode="p2c"/></mml:csymbol>
  </xsl:template>

  <!-- mi -->
  <xsl:template mode="p2c" match="mml:mi/text()">
    <mml:ci><xsl:value-of select="."/></mml:ci>
  </xsl:template>



</xsl:stylesheet>

Solution

  • all other elements <xxx> go to <apply><csymbol>xxx</csymbol> ... </apply>

    that's simply

    <xsl:template match="*">
      <apply>
       <csymbol>
         <xsl:value-of select="name()"/>
       </csymbol>
     </apply>
    </xsl:template>