1

sympy と svgmath を使用して、代数を SVG としてレンダリングしようとしています。ルート ディレクトリに「svgmath.xml」というファイルがあります。残念ながら、「ImportError: libxml2 という名前のモジュールはありません」というメッセージが表示されます。これを整理する最善の方法がわかりません。ヘルプ/アドバイスをいただければ幸いです。私のコードは次のとおりです。

from xml import sax
from xml.sax.saxutils import XMLGenerator

from svgmath.tools.saxtools import XMLGenerator, ContentFilter
from svgmath.mathhandler import MathHandler, MathNS
from svgmath.tools.saxtools import XMLGenerator

from sympy.printing.mathml import mathml
from sympy.abc import x

from sympy.utilities.mathml import c2p

# Open all files
output = open("test.svg", "w") 
config = open("svgmath.xml", "r")    

# Create the converter as a content handler. 
saxoutput = XMLGenerator(output, 'utf-8')
handler = MathHandler(saxoutput, config)

# Parse input file with a namespace-aware SAX parser
parser = sax.make_parser()
parser.setFeature(sax.handler.feature_namespaces, 1)
parser.setContentHandler(handler)
parser.parse(c2p(mathml(3*x), simple=False))

スタック トレースは次のようになりました。

Traceback (most recent call last):
  File "D:\Geddes\my python\SVGmath\test1.py", line 29, in <module>
    parser.parse(c2p(mathml(3*x), simple=True))
  File "C:\Python27\lib\xml\sax\expatreader.py", line 102, in parse
    source = saxutils.prepare_input_source(source)
  File "C:\Python27\lib\xml\sax\saxutils.py", line 298, in prepare_input_source
    f = urllib.urlopen(source.getSystemId())
  File "C:\Python27\lib\urllib.py", line 84, in urlopen
    return opener.open(url)
  File "C:\Python27\lib\urllib.py", line 202, in open
    return self.open_unknown(fullurl, data)
  File "C:\Python27\lib\urllib.py", line 214, in open_unknown
    raise IOError, ('url error', 'unknown url type', type)
IOError: [Errno url error] unknown url type: '?xml%20version=%221.0%22?%3e%0a%3cmath%20xmlns=%22http'

私の変更されたコード:

from xml import sax
from xml.sax.saxutils import XMLGenerator

from svgmath.tools.saxtools import XMLGenerator, ContentFilter
from svgmath.mathhandler import MathHandler, MathNS
from svgmath.tools.saxtools import XMLGenerator

from sympy.printing.mathml import mathml
from sympy.abc import x

from sympy.utilities.mathml import c2p

import libxml2

import StringIO

# Open all files
output = open("test.svg", "w") 
config = open("svgmath.xml", "r")    

# Create the converter as a content handler. 
saxoutput = XMLGenerator(output, 'utf-8')
handler = MathHandler(saxoutput, config)

# Parse input file with a namespace-aware SAX parser
parser = sax.make_parser()
parser.setFeature(sax.handler.feature_namespaces, 1)
parser.setContentHandler(handler)
parser.parse(StringIO.StringIO(mathml(3*x)))

これは有効な SVG ファイルの生成に失敗し、次のようになります。

[WARNING] line 1, column 0: Root element in MathML document must be 'math'
[WARNING] line 1, column 7: MathML element 'times' is unsupported
[WARNING] line 1, column 15: MathML element 'cn' is unsupported
[WARNING] line 1, column 25: MathML element 'ci' is unsupported
[WARNING] line 1, column 0: MathML element 'apply' is unsupported
4

1 に答える 1

3

ライブラリをインストールしlibxml2ます。たとえば、ubuntuではこれがpython-libxml2パッケージです。私もsympy->mathml->svg routeを一度試しましたが、パッケージもインストールする必要がありました(c2p()XSLTを適用してコンテンツをプレゼンテーションMathMLに変換するために必要なiirc)。

于 2011-03-09T20:56:31.860 に答える