PythonでXSDスキーマを調べたいと思います。現在、私はlxmlを使用しています。これは、スキーマに対してドキュメントを検証するだけでよい場合に非常にうまく機能します。しかし、スキーマの内部を知り、lxml動作の要素にアクセスしたいと思います。
スキーマ:
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:include schemaLocation="worker_remote_base.xsd"/>
<xsd:include schemaLocation="transactions_worker_responses.xsd"/>
<xsd:include schemaLocation="transactions_worker_requests.xsd"/>
</xsd:schema>
スキーマをロードするためのlxmlコードは(単純化):
xsd_file_handle = open( self._xsd_file, 'rb')
xsd_text = xsd_file_handle.read()
schema_document = etree.fromstring(xsd_text, base_url=xmlpath)
xmlschema = etree.XMLSchema(schema_document)
schema_document
これで、( )を使用etree._Element
してスキーマをXMLドキュメントとして処理できるようになります。しかしetree.fromstring
(少なくともそのように思われる)XMLドキュメントを想定しているため、xsd:include
要素は処理されません。
この問題は現在、最初のスキーマドキュメントを解析し、次にインクルード要素をロードしてから、それらを1つずつメインドキュメントに手動で挿入することで解決されています。
BASE_URL = "/xml/"
schema_document = etree.fromstring(xsd_text, base_url=BASE_URL)
tree = schema_document.getroottree()
schemas = []
for schemaChild in schema_document.iterchildren():
if schemaChild.tag.endswith("include"):
try:
h = open (os.path.join(BASE_URL, schemaChild.get("schemaLocation")), "r")
s = etree.fromstring(h.read(), base_url=BASE_URL)
schemas.append(s)
except Exception as ex:
print "failed to load schema: %s" % ex
finally:
h.close()
# remove the <xsd:include ...> element
self._schema_document.remove(schemaChild)
for s in schemas:
# inside <schema>
for sChild in s:
schema_document.append(sChild)
私が求めているのは、より一般的な方法を使用して問題を解決する方法のアイデアです。私はすでにPythonで他のスキーマパーサーを検索しましたが、今のところ、その場合に適合するものはありませんでした。
ご挨拶、