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.
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:
<timespanstart="-INF"end="+INF">
Any ideas?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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:
importpyxbimportgenpynewdoc=genpy.MetadataDocument()newdoc.template='anewtemplate'newdoc.timespan.append(pyxb.BIND(# Single timespanpyxb.BIND(# First field instance'title',pyxb.BIND('foo',lang='ENG')),start='-INF',end='+INF'))bds=pyxb.utils.domutils.BindingDOMSupport()bds.setDefaultNamespace(genpy.Namespace)printnewdoc.toxml(bds)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2010-01-21
Is there a way to stop the <?xml version="1.0" ?> from being included in the generated XML as well?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
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:
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:
Any ideas?
I managed to do it:
But the next problem is this generates XML with:
I want to remove all ns1 or other namespace prefix. How is this possible?
Thanks.
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:
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.
Is there a way to stop the <?xml version="1.0" ?> from being included in the generated XML as well?
Add root_only=True to the toxml parameters:
This will make PyXB extract the documentRoot element out of the DOM document created by bds before converting it to XML.