Menu

Using Binding Classes

Help
Anonymous
2010-01-21
2013-03-06
  • Anonymous

    Anonymous - 2010-01-21

    Hi,

    We have a XML schema which we are using with PyXB to generate python classes, and it seems to just fine. Our main use at the moment is to build XML using the Python generated but I am stuck on the usage of certain aspects such as Binding classes to produce our XML.

    <xs:element name="MetadataDocument" xmlns:tns="validnamespaceprovider" type="tns:MetadataType" />
        <xs:complexType name="MetadataType">
            <xs:sequence maxOccurs="1" minOccurs="1">
                <xs:element name="template" type="xs:string"/>
                <xs:element name="timespan" maxOccurs="unbounded" minOccurs="0">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="field" maxOccurs="unbounded" minOccurs="0">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="name" type="xs:string"></xs:element>
                                        <xs:element name="value" minOccurs="0" maxOccurs="unbounded">
                                            <xs:complexType>
                                                <xs:simpleContent>
                                                    <xs:extension base="xs:string">
                                                        <xs:attribute name="lang" type="xs:language"/>
                                                        <xs:attribute name="user" type="xs:string"/>
                                                        <xs:attribute name="timestamp" type="xs:dateTime"/>
                                                    </xs:extension>
                                                </xs:simpleContent>
                                            </xs:complexType>
                                        </xs:element>
                                    </xs:sequence>
                                    <xs:attribute name="track" type="xs:string" />
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                        <xs:attribute name="start" type="xs:string" />
                        <xs:attribute name="end" type="xs:string" />
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    

    The NS provider does exist, I just have to remove it from public sites for now. So running this and the rest of the xsd through PyXB results in a file and I can do:

    newdoc  = genpy.MetadataDocument()
    field = pyxb.BIND('testfield')
    newdoc.template="anewtemplate"
    newdoc.timespan.append(pyxb.BIND(field))
    print newdoc.toxml()
    

    But when I come to add a timespan is where I come to problems. In your example it was using items that correspond to a particular item type but I don't have that for timespan. When I check the type of "newdoc.timespan" it is a list.

    At the moment I don't know how to create attributes on timespan to create the following:

    <timespan start="-INF" end="+INF">
    

    Any ideas?

     
  • Anonymous

    Anonymous - 2010-01-21

    I managed to do it:

    name = 'title'
    value = pyxb.BIND('foo', lang='ENG')
    field = pyxb.BIND(name, value)
    newdoc  = genpy.MetadataDocument()
    newdoc.timespan.append(pyxb.BIND(field, start='-INF', end='+INF'))
    newdoc.template = 'templatename'
    print newdoc.toxml()
    

    But the next problem is this generates XML with:

    <ns1:template>Parham</ns1:template>
    

    I want to remove all ns1 or other namespace prefix. How is this possible?

    Thanks.

     
  • Peter A. Bigot

    Peter A. Bigot - 2010-01-21

    First, I strongly recommend refactoring the schema to get rid of all those inner anonymous complex types.  It makes you spend way too much mental energy trying to figure out what you're building where, and forces you to operate on top-level objects since the bindings can't be assigned until you reach the top level.

    However, the following example seems to do what you're asking for:

    import pyxb
    import genpy
    newdoc = genpy.MetadataDocument()
    newdoc.template = 'anewtemplate'
    newdoc.timespan.append(pyxb.BIND( # Single timespan
            pyxb.BIND( # First field instance
                'title',
                pyxb.BIND('foo', lang='ENG')
                ),
            start='-INF', end='+INF'))
    bds = pyxb.utils.domutils.BindingDOMSupport()
    bds.setDefaultNamespace(genpy.Namespace)
    print newdoc.toxml(bds)
    
     
  • Anonymous

    Anonymous - 2010-01-21

    Thanks for your help, that is perfect.

    I agree that I would like to refactor the schema, but at the moment we have no control on this external system.

    Many thanks again.

     
  • Anonymous

    Anonymous - 2010-01-21

    Is there a way to stop the  <?xml version="1.0" ?> from being included in the generated XML as well?

     
  • Peter A. Bigot

    Peter A. Bigot - 2010-01-21

    Add root_only=True to the toxml parameters:

    print newdoc.toxml(bds, root_only=True)
    

    This will make PyXB extract the documentRoot element out of the DOM document created by bds before converting it to XML.

     

Log in to post a comment.

MongoDB Logo MongoDB