lxml/MathML XML Schema - 如何修复“内容模型不是确定性”的错误?
我正在按照lxml 验证文档来创建一个类,这个类的作用是验证给定的 XML 字符串是否符合 Math ML 3.0 的标准。下面是我写的这个类:
class XMLSchema(object):
def __init__(self, path_to_xsd_file):
with open(path_to_xsd_file) as f:
xmlschema_doc = etree.parse(f)
self.xmlschema = etree.XMLSchema(xmlschema_doc)
def validate(self, well_formed_xml_string):
"""Validates a well-formed XML string against an XML schema.
Returns True if xml_string is valid, False if not.
"""
xml = etree.parse(StringIO(well_formed_xml_string))
return self.xmlschema.validate(xml)
创建这个类的实例后,得到了以下结果:
>>> x = XMLSchema('mathml3.xsd')
Traceback (most recent call last):
...
lxml.etree.XMLSchemaParseError: complex type
'annotation-xml.model': The content model is not determinist., line 42
我该如何解决这个问题呢?
1 个回答
6
嗯,我试过的那些xsd验证工具并没有说它是非确定性的(不过我没有用lxml)。相关的代码是
<xs:complexType name="annotation-xml.model">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:group ref="m:MathExpression"/>
<xs:group ref="m:anyElement"/>
</xs:choice>
</xs:complexType>
<xs:group name="anyElement">
<xs:choice>
<xs:any namespace="##other" processContents="skip"/>
<xs:any namespace="##local" processContents="skip"/>
</xs:choice>
</xs:group>
这段代码应该说明annotation-xml可以包含mathml或者其他东西,而“其他东西”指的是在其他命名空间中的东西(##other)或者不在任何命名空间中的东西(##local)。
我看不出这些选择中哪些是非确定性的,但你可以尝试简化一下,比如如果你实际上不需要没有命名空间的注释,可以去掉##local这一条。
如果你能让它工作(或者没能工作),能不能在www-math@w3.org这个邮件列表上联系我,我会修正这个模式,如果需要修正的话(或者至少记录一下lxml需要本地修改)(我不关注这个论坛,只是偶然看到关于mathml的谷歌提醒:-)
更新
作为对MathML3第二版的更新,我已经重写了XSD版本中的内容模型,以便它能被libxml接受。旧的模式并没有错,但这对用户没有帮助,所以我觉得改一下更好。