tiền thưởng cao cho những điều sau đây Q:Làm cách nào để xác thực tệp xml dựa vào Lược đồ XSD bằng thư viện Amara bằng Python?
Xin chào, Đây là những gì tôi đã cố gắng trên Ubuntu 9.10 sử dụng Python 2.6, Amara2 (bằng cách này, test.xsd được tạo bằng công cụ xml2xsd):
[email protected]:~$ 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
[email protected]:~$
=====o=====
Vì vậy, tại sao tôi thấy lỗi này? Chức năng này có được hỗ trợ không? Làm cách nào để xác thực tệp XML đối với XSD trong khi có tính linh hoạt để trỏ đến bất kỳ tệp XSD nào? Cảm ơn và cho tôi biết nếu bạn có câu hỏi.
bạn có muốn sử dụng Thư viện Amara không? –
@movieyoda, không, tôi có thể chuyển sang thư viện Python xml tốt khác. Tuy nhiên, tôi sẽ cần phải chuyển mã 'amara' hiện có của tôi sang nó sau đó, và tôi hy vọng sẽ tìm ra một giải pháp không đau cho điều đó. –