How to parse xml element with attribute xsi:type use lxml?

I’ve got xsd file like below:

<xsd:element name="Root">
  <xsd:complexType name="TypeDecl">
    <xsd:complexContent>
      <xsd:element name="TypeSpec" type="ppx:AbstractTypeA"/>
    </xsd:complexContent>
  </xsd:complexType>
  <xsd:complexType name="TypeC">
    <xsd:complexContent>
      <xsd:extension base="ppx:AbstractTypeA">
        <xsd:sequence>
          <xsd:element name="Ele1" type="xsd:string"/>
          <xsd:element name="Ele2" type="xsd:string"/>
        </xsd:sequence>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>
  <xsd:complexType name="TypeD">
    <xsd:complexContent>
      <xsd:extension base="ppx:AbstractTypeA">
        <xsd:sequence>
          <xsd:element name="Ele3" type="xsd:string"/>
          <xsd:element name="Ele3" type="xsd:string"/>
        </xsd:sequence>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>
  <xsd:complexType name="AbstractTypeA" abstract="true"/>
</xsd:element>

And xml file like below:

<Root>
  <TypeSpec xsi:type="TypeC">
    <Ele1>some content</Ele1>
    <Ele2>some content</Ele2>
  </TypeSpec>
</Root>

I’m trying to parse this xml to elementTree:

from lxml import etree

xsd_schema = etree.XMLSchema(f=xsd_file)
root = etree.fromstring(xml_string, parser=parser)

And expect element TypeSpec.__class__ is ‘TypeC’, but is ‘AbstractTypeA’.And if I try TypeSpec.getchildren(), it will raise a :

TypeError: result of class lookup must be subclass of <class 'type'>, got <class 'type'>

I think I’ve got misunderstand how ‘xsi:type’ works, wish somebody please explain it to me, thx.

Leave a Comment