2012-05-10 5 views
2

ximファイルを読み取るためにpythonとlxmlを使用し、xsdファイルに対して検証されます。私は属性のxs:タイプを見つけたいと思います。今までlxmlを持つxmlファイル内の属性のタイプ

マイコード: 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'> 

のような私はタイプは関係なく、常にXSD仕様をstrさ

self.xmlRoot.child.attrib['number'] 

を経由してアクセスする属性のために何かを取得します。どのように私は属性の型を取得する?

答えて

0

これは可能ではないと思います。間違っているとは思いませんが、属性のタイプに関する情報がlxmlによってどのように提供されるかを知ることは難しいです。

lxml.objectify APIには「型付き」要素クラス(IntElementなど)がありますが、属性を表すクラスはありません。 lxml(およびElementTree; lxmlがベースになっている)では、属性はElementsのプロパティです。属性値を要求すると文字列を取得します。

関連する問題