我正在尝试以这种方式使用 lxml 验证 MathML XML 字符串:
import lxml.etree
mathml = """
<!DOCTYPE math PUBLIC "-//W3C//DTD MathML 3.0//EN" "http://www.w3.org/Math/DTD/mathml3/mathml3.dtd">
<math xmlns="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink">
<mi>a</mi>
<mo>+</mo>
<mi>b</mi>
</math>
"""
lxml_parser = lxml.etree.XMLParser(
dtd_validation=True,
no_network=False,
load_dtd=True,
ns_clean=True,
remove_blank_text=True,
)
validated = lxml.etree.fromstring(xml, lxml_parser)
通过这种方式,它检查 mathml 字符串中指定的 DTD 并通过网络验证字符串。
问题
当没有可用的 nwtwork 时,如何根据本地 DTD 验证 mathml 字符串?
我试过的
我从 https://www.w3.org/Math/DTD/mathml3/mathml3.dtd 下载了 MathML DTD 3,从https://www.w3.org/Math/DTD/mathml1/下载了MathML DTD 1 mathml.dtd并将它们保存在当前工作目录中,我已将 DOCTYPE 声明更改为指向本地 DTD,例如<!DOCTYPE math SYSTEM "path/to/mathml_dtd.dtd">
,我终于创建了 lxml.etree.XMLParser no_network=True
,但是当我运行以下代码时
import lxml.etree
mathml = """
<!DOCTYPE math SYSTEM "path/to/dtd/mathml3.dtd">
<math xmlns="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink">
<mrow>
<mo>⌊</mo>
<mrow>
<mi>a</mi>
</mrow>
<mo>⌋</mo>
</mrow>
</math>
"""
lxml_parser = lxml.etree.XMLParser(
dtd_validation=True,
no_network=True,
load_dtd=True,
ns_clean=True,
remove_blank_text=True,
)
validated = lxml.etree.fromstring(xml, lxml_parser)
我有这个错误:
File "/projects/py_asciimath/py_asciimath/parser/parser.py", line 117, in __dtd_validation
return lxml.etree.fromstring(xml, lxml_parser)
File "src/lxml/etree.pyx", line 3235, in lxml.etree.fromstring
File "src/lxml/parser.pxi", line 1876, in lxml.etree._parseMemoryDocument
File "src/lxml/parser.pxi", line 1757, in lxml.etree._parseDoc
File "src/lxml/parser.pxi", line 1068, in lxml.etree._BaseParser._parseUnicodeDoc
File "src/lxml/parser.pxi", line 601, in lxml.etree._ParserContext._handleParseResultDoc
File "src/lxml/parser.pxi", line 711, in lxml.etree._handleParseResult
File "src/lxml/parser.pxi", line 640, in lxml.etree._raiseParseError
File "/projects/py_asciimath/py_asciimath/translation/dtd/mathml3.dtd", line 36
lxml.etree.XMLSyntaxError: conditional section INCLUDE or IGNORE keyword expected, line 36, column 17
使用 MathML DTD 1 的本地副本,我得到了这个:
File "/projects/py_asciimath/py_asciimath/parser/parser.py", line 117, in __dtd_validation
return lxml.etree.fromstring(xml, lxml_parser)
File "src/lxml/etree.pyx", line 3235, in lxml.etree.fromstring
File "src/lxml/parser.pxi", line 1876, in lxml.etree._parseMemoryDocument
File "src/lxml/parser.pxi", line 1757, in lxml.etree._parseDoc
File "src/lxml/parser.pxi", line 1068, in lxml.etree._BaseParser._parseUnicodeDoc
File "src/lxml/parser.pxi", line 601, in lxml.etree._ParserContext._handleParseResultDoc
File "src/lxml/parser.pxi", line 711, in lxml.etree._handleParseResult
File "src/lxml/parser.pxi", line 640, in lxml.etree._raiseParseError
File "<string>", line 1
lxml.etree.XMLSyntaxError: Entity 'lfloor' not defined, line 1, column 135