使用xml來顯示數學公式

使用xml來顯示數學公式

taowen


不要誤會,這裏並不是用國際標準的數學xml來描述,通過最新的瀏覽器的支持來實現。我只是嘗試用xml+xslt,用簡單的html來顯示。只是一個初步的想法,拿出來和大家分享。也許之前,已經有很多人做過類似的嘗試,沒有關係,我只是說一說我的想法。

數學公式的格式是很多樣的,比如極限和積分這樣的。其中每個部分都能用html來顯示,最終用table來組合。我的想法就是用xml來描述數學公式種各部分的關係,然後用xslt來格式化這個xml文件。

舉個簡單的例子x的平方。用這樣的xml數據來描述。注意,這不是國際標準格式。真正實現的時候應該正規一些。

<Power>
 <Base>
  <Quote Val="X"/>
 </Base>
 <Exponent>
  <Quote Val="2"/>
 </Exponent>
</Power>

那個<Quote>就是表示直接複製val屬性的值就可,無需格式化。然後這麼一個xslt來轉化:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="Power"/>
</body>
</html>
</xsl:template>
<xsl:template match="Power">
<table cellpadding="0" cellspacing="0">
<tr>
<td>
<table cellpadding="0" cellspacing="0">
<tr>
<td>
</td>
</tr>
<tr>
<td><xsl:apply-templates select="base"/>
</td>
</tr>
</table>
</td>
<td valign="top">
<table cellpadding="0" cellspacing="0">
<tr>
<td>
</td>
</tr>
<tr>
<td valign="top"><xsl:apply-templates select="exponent"/>
</td>
</tr>
<tr>
<td> 
</td>
</tr>
</table>
</td>
</tr>
</table>
</xsl:template>
<xsl:template match="Base">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="Exponent">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="Quote">
<xsl:value-of select="@Val"/>
</xsl:template>
</xsl:stylesheet>

最後用這樣的辦法把兩者聯在一起

<?xml-stylesheet type="text/xsl" href="math.xsl" ?>

或者用這樣的辦法,避免xml與xslt的直接關聯。

<html>
<body>
<script language="javascript">
// Load XML 
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.load("math.xml")
// Load the XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("math.xsl")
// Transform
document.write(xml.transformNode(xsl))
</script>
</body>
</html>

類似的還可以實現極限,積分等。現在問題還有很多

  • 1.本人對於table的諸多格式控制並不熟悉,導致一個table佔用了多於實際所需的空間,最終在像“分數”這樣的場合,空白太多。
  • 2.部分數學格式難於用table表達,比如開方的符號。
  • 3.div或許可以派上用場
  • 4.或許可以改由flash來實現,把xml作爲參數。
  • 5.總體來說,插入圖片仍是最佳方案。不過似乎目前沒有單純作數學公式圖片輸出的軟件。只能用截圖的辦法。
  • 6.即使用xml實現了。與普通文本的融合也將是個問題。

最後,本人是位數學系的大一學生。所知的東西極其有限。以上內容只不過是個人實驗,並未廣泛查閱網絡和已有軟件。錯誤實在很多。如有高手不吝賜教,在下十分感謝

發佈了61 篇原創文章 · 獲贊 1 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章