Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Outputting MathML via Javascript

Reply
Thread Tools

Outputting MathML via Javascript

 
 
BakedBean
Guest
Posts: n/a
 
      12-14-2005
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

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      12-14-2005


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/
 
Reply With Quote
 
 
 
 
BakedBean
Guest
Posts: n/a
 
      12-14-2005
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

 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      12-14-2005


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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TeX subset into MathML conversion via MathJax Ivan Shmakov XML 0 06-01-2012 03:10 PM
Outputting HTML via XSL hoopsbhoy67@aol.com XML 1 01-28-2006 12:33 AM
MathML in Java Sam Hwang Java 1 09-09-2005 07:02 AM
MathML and LiveConnect applet DKM Java 1 06-10-2005 03:58 PM
MathML =?Utf-8?B?SmVyZW15?= ASP .Net 1 05-09-2004 04:41 PM



Advertisments