xsdファイルに対して検証されるxmlファイルを読み取るためにpythonとlxmlを使用しています。属性の xs:type を知りたいです。
今までの私のコード: XSD
...
<xs:complexType name="io" >
<xs:attribute name="number" type="xs:decimal" use="optional" default="5.9"/>
</xs:complexType>
...
XML
...
<io number="1.1">
...
次に、Python コード:
with open(self.xmlSchemaFilename) as schema:
xmlschema_doc = objectify.parse(schema)
self.xmlschema = etree.XMLSchema(xmlschema_doc)
parser = etree.XMLParser(schema = self.xmlschema,
attribute_defaults = True, remove_blank_text=True)
with open(self.xmlFilename) as myfile:
tree = objectify.parse(myfile, parser)
#this correctly asserts the type from the xsd
root = tree.getroot()
self.xmlRoot = objectify.fromstring(etree.tostring(root))
self.xmlschema.assertValid(self.xmlRoot)
これで、すべての子ノードのタイプにアクセスできます
type(self.xmlRoot.child)
xsdから正しいタイプを返し、次のようなものを取得します
<type 'lxml.objectify.IntElement'>
しかし、私が経由してアクセスする属性については
self.xmlRoot.child.attrib['number']
xsd の指定に関係なく、型は常に str です。属性の型を取得するにはどうすればよいですか?