2

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 です。属性の型を取得するにはどうすればよいですか?

4

1 に答える 1

0

これは不可能だと思います。間違っていることが証明されてもかまいませんが、属性のタイプに関する情報が lxml によってどのように提供されるかを理解するのは困難です。

API には「型指定されたlxml.objectify」要素クラス ( などIntElement) がありますが、属性を表すクラスはありません。lxml (および lxml が基づいている ElementTree) では、属性は要素のプロパティであり、属性値を要求すると文字列が取得されます。

于 2012-05-19T08:29:06.427 に答える