Problem to generate XML for complexTypeDef

Help
Umesh
2012-07-11
2013-03-06
  • 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?

    <core:Ext>
    <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"/>
    </core:Ext>

    # 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:annotation>
    <xsd:documentation>This type may contain elements or attributes from any namespace and is provided for future extensibility.</xsd:documentation>
    </xsd:annotation>
    <xsd:sequence>
    <xsd:any namespace="##any" processContents="lax" minOccurs="0" maxOccurs="unbounded"/>
    </xsd:sequence>
    <xsd:anyAttribute namespace="##any" processContents="lax"/>
    </xsd:complexType>

    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);
    exType.getAny().add(app_data);

     
  • Peter A. Bigot

    Peter A. Bigot - 2012-07-11

    Given schema:

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

    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')
    ext.append(ad)
    print ext.toxml()
    print ext.wildcardElements()
    

    you should see

    llc[20]$ python testany.py 
    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 {http://www.cablelabs.com/namespaces/metadata/xsd/core/1}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:annotation>
    <xsd:documentation xml:lang="en">An abstract base type from which all other MD3.0 assets are derived.</xsd:documentation>
    </xsd:annotation>
    <xsd:sequence>
    <xsd:element name="Description" type="DescriptionType" minOccurs="0">
    <xsd:annotation>
    <xsd:documentation>DEPRECATED. A description of the asset.</xsd:documentation>
    </xsd:annotation>
    </xsd:element>
    <xsd:element name="Ext" type="ExtType" minOccurs="0">
    <xsd:annotation>
    <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>
    </xsd:annotation>
    </xsd:element>
    </xsd:sequence>

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

     
  • 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 http://www.cablelabs.com/projects/metadata/specifications/xml_schemas/MD-SP-CORE-I01.xsd \
      -m vod30 -u http://www.cablelabs.com/projects/metadata/specifications/xml_schemas/MD-SP-VODContainer-I01.xsd \
      -m offer -u http://www.cablelabs.com/projects/metadata/specifications/xml_schemas/MD-SP-OFFER-I01.xsd \
      -m terms -u http://www.cablelabs.com/projects/metadata/specifications/xml_schemas/MD-SP-TERMS-I01.xsd \
      -m title -u http://www.cablelabs.com/projects/metadata/specifications/xml_schemas/MD-SP-TITLE-I01.xsd \
      -m content -u http://www.cablelabs.com/projects/metadata/specifications/xml_schemas/MD-SP-CONTENT-I01.xsd
    

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

    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
       xmlns="urn:customVOD"
       xmlns:core="http://www.cablelabs.com/namespaces/metadata/xsd/core/1"
       targetNamespace="urn:customVOD"
       elementFormDefault="qualified" attributeFormDefault="unqualified" version="1">
      <xsd:import namespace="http://www.cablelabs.com/namespaces/metadata/xsd/core/1" schemaLocation="MD-SP-CORE-I01.xsd"/>
      <xsd:complexType name="VenueType">
        <xsd:annotation>
          <xsd:documentation>Where to see the movie</xsd:documentation>
        </xsd:annotation>
        <xsd:complexContent>
          <xsd:extension base="core:ExtType">
            <xsd:sequence>
              <xsd:element name="Venue" type="xsd:string"/>
        </xsd:sequence>
          </xsd:extension>
        </xsd:complexContent>
      </xsd:complexType>
      <xsd:element name="Venue" type="VenueType"/>
    </xsd:schema>
    

    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.RequireValidWhenGenerating(False)
    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')
    cgt.PosterRef.append(core.AssetRefType(uriId='urn:aPoster'))
    # 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
    adi3.ContentGroup.append(cgt)
    print adi3.toDOM().toprettyxml()
    

    which produces:

    llc[82]$ python testcl.py 
    <?xml version="1.0" ?>
    <vod30:ADI3 xmlns:core="http://www.cablelabs.com/namespaces/metadata/xsd/core/1" xmlns:custom="urn:customVOD" xmlns:offer="http://www.cablelabs.com/namespaces/metadata/xsd/offer/1" xmlns:vod30="http://www.cablelabs.com/namespaces/metadata/xsd/vod30/1">
            <vod30:ContentGroup uriId="urn:one">
                    <core:Ext>
                            <custom:Venue>some multiplex</custom:Venue>
                    </core:Ext>
                    <offer:TitleRef uriId="urn:aMovie"/>
                    <offer:PosterRef uriId="urn:aPoster"/>
            </vod30:ContentGroup>
    </vod30:ADI3>
    

    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

    <Ext><Venue>a123</Venue></Ext>
    

    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\"> 
                    <core:Ext> 
                            <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\"/> 
                    </core:Ext> 
                    <offer:TitleRef uriId=\"urn:aMovie\"/> 
                    <offer:PosterRef uriId=\"urn:aPoster\"/> 
            </vod30:ContentGroup>
    

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

    Schema:

    <?xml version=\"1.0\" encoding=\"UTF-8\"?> 
    <xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"urn:customVOD\" xmlns:core=\"http://www.cablelabs.com/namespaces/metadata/xsd/core/1\" targetNamespace=\"urn:customVOD\" elementFormDefault=\"qualified\" attributeFormDefault=\"unqualified\" version=\"1\"> 
    <xsd:import namespace=\"http://www.cablelabs.com/namespaces/metadata/xsd/core/1\" schemaLocation=\"MD-SP-CORE-I01.xsd\"/> 
    <xsd:complexType name=\"VenueType\"> 
            <xsd:complexContent> 
                    <xsd:extension base=\"core:ExtType\"> 
                            <xsd:sequence> 
                                    <xsd:element name=\"Venue\" type=\"AttrDataType\" minOccurs=\"0\" maxOccurs=\"unbounded\"/> 
                            </xsd:sequence> 
                    </xsd:extension> 
            </xsd:complexContent> 
    </xsd:complexType> 
    <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:complexType> 
    <xsd:element name=\"dataelement\" type=\"VenueType\"/> 
    </xsd:schema>
    

    Code:

    import venue_data 
    attrdata = vvenue_data.AttrDataType() 
    attrdata.app= \"sdlfsj\" 
    attrdata.name =\"sidfhlf\" 
    attrdata.vale=\"afysydfya\" 
        
    attrdata1 = venue_data.AttrDataType() 
    attrdata1.app= \"12312\" 
    attrdata1.name =\"3453453\" 
    attrdata1.vale=\"w54535\" 
    appDataType = venue_data.AppDataType() 
    appDataType.append(attrdata) 
    cust=venue_data.dataelement(Venue=appDataType) 
    offer.Ext=cust
    

    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