Outputting MathML via Javascript

Discussion in 'XML' started by BakedBean, Dec 14, 2005.

  1. BakedBean

    BakedBean Guest

    Hi,

    This is probably really simple, but I've only just been asked to look
    at this and I've spent a full day trying to get it to work, so any help
    will be very gratefully received!

    At present I have an HTML page which calls a series of javascript
    functions which output text into a browser. A client then asked if it
    was possible to output mathematical equations instead of just plain
    text! The solution I originally came up with was the MathML was set in
    a javascript variable, displayed via the HTML page and the user needed
    to download MathPlayer to be able to see the equation correctly.
    However, because the clients users may access the pages from computers
    where they can't install plug ins, if possible I need a solution that
    won't require a plug in to work!!!!

    That's when I came across the suggestions on the W3C site and created
    the following simple example following the instructions on the site:

    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="pmathml.xsl"?>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    </head>

    <body>
    <h1>Example</h1>
    ....
    <math xmlns="http://www.w3.org/1998/Math/MathML">
    <mi>x</mi><mo>+</mo><mn>3</mn>
    </math>
    <br/><br/>

    <math xmlns='http://www.w3.org/1998/Math/MathML'>
    <mrow>
    <mn>5</mn>
    <msqrt><mrow><mo>-</mo><mn>1</mn></mrow></msqrt>
    </mrow>
    </math>

    <script type="text/javascript" language="javascript">
    document.write("<br/><br/>simple text<br/><br/>");
    document.write("<math
    xmlns='http://www.w3.org/1998/Math/MathML'><mrow><mn>5</mn><msqrt><mrow><mo>-</mo><mn>1</mn></mrow></msqrt></mrow></math>");
    </script>
    </body>
    </html>

    as an XHTML page. In the above example the first 2 bits of MathML are
    displayed correctly, but the MathML displayed using the document.write
    is not! Therefore, have I missed something? Is this possible? Does it
    need to be an XHTML page or can I 'embed' this into my existing HTML
    page?

    Cheers

    Matt
     
    BakedBean, Dec 14, 2005
    #1
    1. Advertising

  2. BakedBean wrote:


    > <script type="text/javascript" language="javascript">
    > document.write("<br/><br/>simple text<br/><br/>");
    > document.write("<math
    > xmlns='http://www.w3.org/1998/Math/MathML'><mrow><mn>5</mn><msqrt><mrow><mo>-</mo><mn>1</mn></mrow></msqrt></mrow></math>");
    > </script>



    > as an XHTML page. In the above example the first 2 bits of MathML are
    > displayed correctly, but the MathML displayed using the document.write
    > is not!


    Which browser is that? Mozilla does support mixed namespace XML document
    with XHTML and MathML but it does not support document.write in such
    documents. Check the JavaScript console, I think it shows a script error
    for document.write calls in XHTML documents.

    What should be possible in Mozilla is using the W3C Core DOM to create
    elements in the MathML namespace e.g.


    var mnElement = document.createElementNS(mathMLNamespace, 'mn');
    mnElement.appendChild(document.createTextNode('5'));
    mrowElement.appendChild(mnElement);
    var msqrtElement = document.createElementNS(mathMLNamespace, 'msqrt');
    var mrowElement2 = document.createElementNS(mathMLNamespace, 'mrow');
    var moElement = document.createElementNS(mathMLNamespace, 'mo');
    moElement.appendChild(document.createTextNode('-'));
    mrowElement2.appendChild(moElement);
    var mnElement2 = document.createElementNS(mathMLNamespace, 'mn');
    mnElement2.appendChild(document.createTextNode('1'));
    mrowElement2.appendChild(mnElement2);
    msqrtElement.appendChild(mrowElement2);
    mrowElement.appendChild(msqrtElement);
    mathElement.appendChild(mrowElement);

    document.body.appendChild(mathElement);

    Or you could use DOMParser to parse a string with MathML markup and then
    use importNode.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Dec 14, 2005
    #2
    1. Advertising

  3. BakedBean

    BakedBean Guest

    Martin,

    Thanks for the reply. Would this work in IE? I hear IE 7 is on it's
    way, does anyone know if MathML is better supported?

    I think I need to have a look at the W3C Core DOM and try to understand
    that now!!! Are there any other reference sites/books you could
    suggest?

    Cheers

    Matt
     
    BakedBean, Dec 14, 2005
    #3
  4. BakedBean wrote:


    > Would this work in IE? I hear IE 7 is on it's
    > way, does anyone know if MathML is better supported?


    As far as I understand IE does not support MathML natively (like Mozilla
    does) but rather with the help of a plugin. I don't know whether that
    plugin allows scripting the MathML content, I doubt that.
    The script I wrote is meant for W3C DOM Level 2 compatible
    implementations which support document.createElementNS. IE 6 does not do
    that, nor will IE 7 I think as "real XHTML" (meaning XHTML served as
    application/xhtml+xml or at least application/xml) is not an aim for IE 7.

    > I think I need to have a look at the W3C Core DOM and try to understand
    > that now!!! Are there any other reference sites/books you could
    > suggest?


    Reference site is certainly <http://www.w3.org/TR/DOM-Level-2-Core/>.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Dec 14, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmVyZW15?=

    MathML

    =?Utf-8?B?SmVyZW15?=, May 7, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    747
    =?Utf-8?B?RG91ZyBIb2xsYW5k?=
    May 9, 2004
  2. DKM
    Replies:
    1
    Views:
    532
  3. Sam Hwang

    MathML in Java

    Sam Hwang, Sep 9, 2005, in forum: Java
    Replies:
    1
    Views:
    1,376
    Andrew Thompson
    Sep 9, 2005
  4. Replies:
    1
    Views:
    470
    Peter Flynn
    Jan 28, 2006
  5. Ivan Shmakov
    Replies:
    0
    Views:
    843
    Ivan Shmakov
    Jun 1, 2012
Loading...

Share This Page