使用xml来显示数学公式

[原创] taowen **2004-04-16
** 不要误会,这里并不是用国际标准的数学xml来描述,通过最新的浏览器的支持来实现。我只是尝试用xml+xslt,用简单的html来显示。只是一个初步的想法,拿出来和大家分享。也许之前,已经有很多人做过类似的尝试,没有关系,我只是说一说我的想法。

数学公式的格式是很多样的,比如极限和积分这样的。其中每个部分都能用html来显示,最终用table来组合。我的想法就是用xml来描述数学公式种各部分的关系,然后用xslt来格式化这个xml文件。

举个简单的例子x的平方。用这样的xml数据来描述。注意,这不是国际标准格式。真正实现的时候应该正规一些。

1<power>
2<base/>
3<quote val="X"></quote>
4<exponent>
5<quote val="2"></quote>
6</exponent>
7</power>

那个

 1<quote>就是表示直接复制val属性的值就可,无需格式化。然后这么一个xslt来转化:   
 2  
 3<?xml version="1.0"?>
 4<xsl:stylesheet xmlns:xsl="  http://www.w3.org/TR/WD  -xsl">
 5<xsl:template match="/">
 6<html>
 7<body>
 8<xsl:apply-templates select="Power"></xsl:apply-templates>
 9</body>
10</html>
11</xsl:template>
12<xsl:template match="Power">
13<table cellpadding="0" cellspacing="0">
14<tr>
15<td>
16<table cellpadding="0" cellspacing="0">
17<tr>
18<td>
19</td>
20</tr>
21<tr>
22<td>
23<xsl:apply-templates select="base"></xsl:apply-templates>
24</td>
25</tr>
26</table>
27</td>
28<td valign="top">
29<table cellpadding="0" cellspacing="0">
30<tr>
31<td>
32</td>
33</tr>
34<tr>
35<td valign="top">
36<xsl:apply-templates select="exponent"></xsl:apply-templates>
37</td>
38</tr>
39<tr>
40<td>
41</td>
42</tr>
43</table>
44</td>
45</tr>
46</table>
47</xsl:template>
48<xsl:template match="Base">
49<xsl:apply-templates select="*"></xsl:apply-templates>
50</xsl:template>
51<xsl:template match="Exponent">
52<xsl:apply-templates select="*"></xsl:apply-templates>
53</xsl:template>
54<xsl:template match="Quote">
55<xsl:value-of select="@Val"></xsl:value-of>
56</xsl:template>
57</xsl:stylesheet>   
58  
59最后用这样的办法把两者联在一起   
60  
61<?xml-stylesheet type="text/xsl" href="math.xsl" ?>   
62  
63或者用这样的办法,避免xml与xslt的直接关联。   
64  
65<html>
66<body>
67<script language="javascript">   
68// Load XML   
69var xml = new ActiveXObject("Microsoft.XMLDOM")   
70xml.async = false   
71xml.load("math.xml")   
72// Load the XSL   
73var xsl = new ActiveXObject("Microsoft.XMLDOM")   
74xsl.async = false   
75xsl.load("math.xsl")   
76// Transform   
77document.write(xml.transformNode(xsl))   
78</script>
79</body>
80</html>   
81  
82类似的还可以实现极限,积分等。现在问题还有很多   
83  
841.本人对于table的诸多格式控制并不熟悉,导致一个table占用了多于实际所需的空间,最终在像“分数”这样的场合,空白太多。   
852.部分数学格式难于用table表达,比如开方的符号。   
863.div或许可以派上用场   
874.或许可以改由flash来实现,把xml作为参数。   
885.总体来说,插入图片仍是最佳方案。不过似乎目前没有单纯作数学公式图片输出的软件。只能用截图的办法。   
896.即使用xml实现了。与普通文本的融合也将是个问题。   
90  
91最后,本人是位数学系的大一学生。所知的东西极其有限。以上内容只不过是个人实验,并未广泛查阅网络和已有软件。错误实在很多。如有高手不吝赐教,在下十分感谢</quote>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus