Search code examples
pythonxml-parsingxsdpyxb

XSD parsing documents and elements


I have a xsd for which I used pyxb to generate object classes for. So far everything is working, i'm able to take in documents, error handling, etc. is working fine. My only question is this, I have the following in my xsd:

<xs:element name="users">
  <xs:complexType>
    <xs:sequence>
        <xs:element minOccurs="0" maxOccurs="unbounded" ref="user" />
    </xs:sequence>
  </xs:complexType>
</xs:element>

I have the user complex type defined elsewhere. Sometimes I want to take a user from the main xml document and I want to create python class bindings from it...but i'm not able to. pyxb only allows me to create from an entire document(using the CreateFromDocument function). Is there anyway to get just that one user element?

I read the following:

https://developer.yahoo.com/python/python-xml.html

http://pyxb.sourceforge.net/api/pyxb.binding.basis.element-class.html

http://pyxb.sourceforge.net/userref_pyxbgen.html


Solution

  • CreateFromDocument() will create a binding for any XML fragment that is a top-level element in the schema. So you should have been able to do:

    instance = user_xsd.CreateFromDocument(string)
    

    without going through a dom instance first. There are a lot of examples that do exactly this in the tests directory.