Problem to generate XML for complexTypeDef

  • Umesh

    Umesh - 2012-07-11

    I am facing problem to generate XML from Pyxb classes.
    Below is the class, i want to fill the data inside something given below like but unable to do so.How to fill the data inside it?

    <App_Data app="MOD" name="Format" value="SD"/>
    <App_Data app="MOD" name="Model" value="EST"/>
    <App_Data app="MOD" name="Provider" value="PAR"/>
    <App_Data app="MOD" name="Description" value="The SD EST Titanic asset package"/>

    # Complex type ExtType with content type ELEMENT_ONLY
    class ExtType (pyxb.binding.basis.complexTypeDefinition):
        _TypeDefinition = None
        _ContentTypeTag = pyxb.binding.basis.complexTypeDefinition._CT_ELEMENT_ONLY
        _Abstract = False
        _ExpandedName = pyxb.namespace.ExpandedName(Namespace, u'ExtType')
        # Base type is pyxb.binding.datatypes.anyType
        _AttributeWildcard = pyxb.binding.content.Wildcard(process_contents=pyxb.binding.content.Wildcard.PC_lax, namespace_constraint=pyxb.binding.content.Wildcard.NC_any)
        _HasWildcardElement = True

        _ElementMap = {
        _AttributeMap = {
    Namespace.addCategoryObject('typeBinding', u'ExtType', ExtType)

    Pls let me know if any other information required.

  • Peter A. Bigot

    Peter A. Bigot - 2012-07-11

    Are you asking how to define ExtType to have App_Data as a subelement?  That's not how PyXB is used: you need an XML schema that defines the structure (including an element named Ext that is of type ExtType).  Then the bindings are generated by pyxbgen from that schema.

    If you mean you have a binding for ExtType and App_Data that came from some schema, and you want Python code to create an instance, it would be something like:

    instance = core.Ext(app=)

  • Peter A. Bigot

    Peter A. Bigot - 2012-07-11

    Sorry, more like:

    instance = core.Ext(App_Data=)

    It would help to see the schema, to know what the structure is.

  • Umesh

    Umesh - 2012-07-11

    Actually ExtType may contain elements or attributes from any namespace and is provided for future extensibility. The class above is given generated by pyxb, below is the schema definition for Exttpe. its possible to fill Exttype by jaxb below given example by jaxb. Pls help how to do in Python same thing.

    <!-                              ->
    <!- Extensibility types ->
    <!-                              ->
    <xsd:complexType name="ExtType">
    <xsd:documentation>This type may contain elements or attributes from any namespace and is provided for future extensibility.</xsd:documentation>
    <xsd:any namespace="##any" processContents="lax" minOccurs="0" maxOccurs="unbounded"/>
    <xsd:anyAttribute namespace="##any" processContents="lax"/>

    java code

    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    Element app_data = doc.createElement("App_Data");
    app_data.setAttribute("app", app);
    app_data.setAttribute("name", name);
    app_data.setAttribute("value", value);

  • Peter A. Bigot

    Peter A. Bigot - 2012-07-11

    Given schema:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="">
      <xs:element name="Ext" type="ExtType"/>
      <xs:complexType name="ExtType">
          <xs:documentation>This type may contain elements or attributes from
    any namespace and is provided for future
          <xs:any namespace="##any" processContents="lax" minOccurs="0" maxOccurs="unbounded"/>
        <xs:anyAttribute namespace="##any" processContents="lax"/>

    built by pyxbgen with:

     pyxbgen -m any -u any.xsd

    and program [

    import any
    import xml.dom.minidom
    ext = any.Ext()
    domimpl = xml.dom.minidom.getDOMImplementation()
    doc  = domimpl.createDocument(None, None, None)
    ad = doc.createElement('App_Data')
    ad.setAttribute('app', 'appvalue')
    ad.setAttribute('name', 'namevalue')
    ad.setAttribute('value', 'valuevalue')
    print ext.toxml()
    print ext.wildcardElements()

    you should see

    llc[20]$ python 
    NOTE: Created unbound wildcard element from value in App_Data
    <?xml version="1.0" ?><Ext><App_Data app="appvalue" name="namevalue" value="valuevalue"/></Ext>
    [<DOM Element: App_Data at 0x1b29d40>]

    The NOTE warning comes because what you're doing here is creating a DOM element instance and inserting that into the model.  Because this is a DOM element, PyXB knows nothing about it, and you can't interact with it like PyXB binding class instances.  If you had a schema that defined the App_Data element, you would create it in the normal PyXB way and then append it.

  • Umesh

    Umesh - 2012-07-12

    Thank you very much yes its generating with your schema, Sorry i didn't send you complete schema. Actually in my case i can't directly create object of Ext that's why i am creating object of Exttype and then i append App_data element in it but i got empty Ext.

    The code is generated is shown below in my schema and for ExtType class code is same as u have.
    # Complex type AssetType with content type ELEMENT_ONLY
    class AssetType (pyxb.binding.basis.complexTypeDefinition):
        _TypeDefinition = None
        _ContentTypeTag = pyxb.binding.basis.complexTypeDefinition._CT_ELEMENT_ONLY
        _Abstract = True
        _ExpandedName = pyxb.namespace.ExpandedName(Namespace, u'AssetType')

        # Element {}Ext uses Python identifier Ext
        __Ext = pyxb.binding.content.ElementUse(pyxb.namespace.ExpandedName(Namespace, u'Ext'), 'Ext', '__httpwww_cablelabs_comnamespacesmetadataxsdcore1_AssetType_httpwww_cablelabs_comnamespacesmetadataxsdcore1Ext', False)

      Ext = property(__Ext.value, __Ext.set, None, u'In addition to the elements and attributes defined, an asset may include additional, custom content within this element as needed by the application.')

    schema where it used
    <xsd:complexType name="AssetType" abstract="true">
    <xsd:documentation xml:lang="en">An abstract base type from which all other MD3.0 assets are derived.</xsd:documentation>
    <xsd:element name="Description" type="DescriptionType" minOccurs="0">
    <xsd:documentation>DEPRECATED. A description of the asset.</xsd:documentation>
    <xsd:element name="Ext" type="ExtType" minOccurs="0">
    <xsd:documentation>In addition to the elements and attributes defined, an asset may include additional, custom content within this element as needed by the application.</xsd:documentation>

    Can you pls tell which object i have to create as i can't create Ext object.

  • Peter A. Bigot

    Peter A. Bigot - 2012-07-12

    Well, that was entertaining.  PyXB can indeed be very difficult to use.  In the example program below you might need to uncomment the line that disables validation: those schema are complex, and PyXB does a really bad job of telling you what it doesn't like.

    The key point, now that I see more of the schema, is that you don't create an ExtType instance in DOM.  You need to have a non-abstract complex type that extends or restricts it, so you can assign an instance of that type to the Ext property of an AssetType instance.

    First, I created bindings for the CableLabs schemas, and generated them in a way that allows extensions to be created with external schema:

    pyxbgen \
      --module-prefix=cablelabs \
      --archive-to-file=cablelabs.wxs \
      -m core -u \
      -m vod30 -u \
      -m offer -u \
      -m terms -u \
      -m title -u \
      -m content -u

    Next, I created a schema that defines an extension type:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema xmlns:xsd=""
       elementFormDefault="qualified" attributeFormDefault="unqualified" version="1">
      <xsd:import namespace="" schemaLocation="MD-SP-CORE-I01.xsd"/>
      <xsd:complexType name="VenueType">
          <xsd:documentation>Where to see the movie</xsd:documentation>
          <xsd:extension base="core:ExtType">
              <xsd:element name="Venue" type="xsd:string"/>
      <xsd:element name="Venue" type="VenueType"/>

    I generated bindings for the extensions with:

    pyxbgen --archive-path=.:+ -u custom.xsd -m custom

    Then I used the following program to build up an instance:

    import pyxb.utils.domutils
    import xml.dom.minidom
    import cablelabs.core as core
    import cablelabs.offer as offer
    import cablelabs.title as title
    import cablelabs.vod30 as vod30
    import custom
    pyxb.utils.domutils.BindingDOMSupport.DeclareNamespace(core.Namespace, 'core')
    pyxb.utils.domutils.BindingDOMSupport.DeclareNamespace(offer.Namespace, 'offer')
    pyxb.utils.domutils.BindingDOMSupport.DeclareNamespace(title.Namespace, 'title')
    pyxb.utils.domutils.BindingDOMSupport.DeclareNamespace(vod30.Namespace, 'vod30')
    pyxb.utils.domutils.BindingDOMSupport.DeclareNamespace(custom.Namespace, 'custom')
    adi3 = vod30.ADI3()
    # Create an instance of ContentGroupType (which extends AssetType). It
    # is not associated with an element.
    cgt = offer.ContentGroupType(uriId='urn:one')
    cgt.TitleRef = core.AssetRefType(uriId='urn:aMovie')
    # Give it an extension value.  The Ext element in AssetType must be an
    # extension or restriction of core:ExtType, so use a custom schema, then
    # just assign the instance to the Ext field
    cust = custom.Venue(Venue='some multiplex')
    cgt.Ext = cust
    # Now you can add the instance to the description and display it
    print adi3.toDOM().toprettyxml()

    which produces:

    llc[82]$ python 
    <?xml version="1.0" ?>
    <vod30:ADI3 xmlns:core="" xmlns:custom="urn:customVOD" xmlns:offer="" xmlns:vod30="">
            <vod30:ContentGroup uriId="urn:one">
                            <custom:Venue>some multiplex</custom:Venue>
                    <offer:TitleRef uriId="urn:aMovie"/>
                    <offer:PosterRef uriId="urn:aPoster"/>

    This took me longer than I can spend on support for specific questions since PyXB is not a revenue-producing product, but I will convert this into an extended example for the next release.  I hope this is enough to get you going.

  • Peter A. Bigot

    Peter A. Bigot - 2012-07-12

    BTW: In the code sections above that have XML, SourceForge inserted semicolons after all the namespaces that look like web links.  The semicolons are not in my original as posted, and should be removed before attempting to reproduce the example.  (Apparently I should have used a different BBCode tag.)

  • Umesh

    Umesh - 2012-07-12

    Thank you very much dear,
    Best of luck.

  • Umesh

    Umesh - 2012-07-26

    Thanks a lot for you reply we got a lot of help from your suggestion and were able to add <Venue> tag under <core:Ext> like


    But we are facing another problem. We need to add something of this sort under <core:Ext> tag i.e we need to add multiple Venue tags along with attributes

    <?xml version=\"1.0\" ?> 
            <vod30:ContentGroup uriId=\"urn:one\"> 
                            <Venue name=\"a123\" time=\"12:00\" date=\"25-7-2012\"/> 
                                                    <Venue name=\"b345\" time=\"12:00\" date=\"25-7-2012\"/> 
                                                    <Venue name=\"c678\" time=\"12:00\" date=\"25-7-2012\"/> 
                    <offer:TitleRef uriId=\"urn:aMovie\"/> 
                    <offer:PosterRef uriId=\"urn:aPoster\"/> 

    Following is the schema and code we tried but unable to get desired output


    <?xml version=\"1.0\" encoding=\"UTF-8\"?> 
    <xsd:schema xmlns:xsd=\"\" xmlns=\"urn:customVOD\" xmlns:core=\"\" targetNamespace=\"urn:customVOD\" elementFormDefault=\"qualified\" attributeFormDefault=\"unqualified\" version=\"1\"> 
    <xsd:import namespace=\"\" schemaLocation=\"MD-SP-CORE-I01.xsd\"/> 
    <xsd:complexType name=\"VenueType\"> 
                    <xsd:extension base=\"core:ExtType\"> 
                                    <xsd:element name=\"Venue\" type=\"AttrDataType\" minOccurs=\"0\" maxOccurs=\"unbounded\"/> 
    <xsd:complexType name=\"AttrDataType\"> 
            <xsd:attribute name=\"name\" type=\"xsd:string\" use=\"required\"/> 
            <xsd:attribute name=\"time\" type=\"xsd:string\" use=\"required\"/> 
            <xsd:attribute name=\"date\" type=\"xsd:string\" use=\"required\"/> 
    <xsd:element name=\"dataelement\" type=\"VenueType\"/> 


    import venue_data 
    attrdata = vvenue_data.AttrDataType() \"sdlfsj\" =\"sidfhlf\" 
    attrdata1 = venue_data.AttrDataType() \"12312\" =\"3453453\" 
    appDataType = venue_data.AppDataType() 

    Please if you could provide us some suggestion on this as well

  • Peter A. Bigot

    Peter A. Bigot - 2012-07-26

    Without spending time digging into the schema again, my recollection is the Ext element allows only a single element as a child.  You'll need to modify your custom schema so that multiple venues are held within a single element.

    Start with the example I provided and change it so the "Venue" element of type String is a Venue element of type VenueType.  Then put attributes on VenueType.  Then replace Venue in the extension with Venues of type VenuesType which supports a sequence of "Venue" elements of type VenueType. 

    What you have may or may not be close; I just don't have any spare time right now to spend on this example.


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks