以下 Q 的高额赏金:
您好,这是我在 Ubuntu 9.10 上使用 Python 2.6、Amara2 尝试的(顺便说一下,test.xsd 是使用 xml2xsd 工具创建的):
g@spot:~$ cat test.xml; echo =====o=====; cat test.xsd; echo ====
o=====; cat test.py; echo =====o=====; ./test.py; echo =====o=====
<?xml version="1.0" encoding="utf-8"?>==; ./test.py` >
test.txttest.xsd; echo ===
<test>abcde</test>
=====o=====
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="test" type="xs:NCName"/>
</xs:schema>
=====o=====
#!/usr/bin/python2.6
# I wish to validate an xml file against an external XSD schema.
from amara import bindery, parse
source = 'test.xml'
schema = 'test.xsd'
#help(bindery.parse)
#doc = bindery.parse(source, uri=schema, validate=True) # These 2 seem
to fail in the same way.
doc = parse(source, uri=schema, validate=True) # So, what is the
difference anyway?
#
=====o=====
Traceback (most recent call last):
File "./test.py", line 14, in <module>
doc = parse(source, uri=schema, validate=True)
File "/usr/local/lib/python2.6/dist-packages/Amara-2.0a4-py2.6-linux-
x86_64.egg/amara/tree.py", line 50, in parse
return _parse(inputsource(obj, uri), flags,
entity_factory=entity_factory)
amara.ReaderError: In file:///home/g/test.xml, line 2, column 0:
Missing document type declaration
g@spot:~$
=====o=====
那么,为什么我会看到这个错误?不支持此功能吗?如何在灵活指向任何 XSD 文件的同时针对 XSD 验证 XML 文件?谢谢,如果您有任何问题,请告诉我。