We frequently inherit from pyxb generated classes. (See attached file for example). This fails in the latest 1.2.3 with a start tag error. One way this can be fixed is, in the toDOM method in basis.py, insert the following lines:
The former behavior, and that resulting from your patch, is trac#203. That PyXB ever allowed a type to automatically become an element tag was an error.
You need to augment your schema so there is an element named Foo that has type Foo.
Also see more extended discussion here which provides an alternative solution where the desired element is explicitly associated with the binding instance.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
In the context of extending classes, it may be that you're missing the call to _SetSupersedingClass() which is supposed to ensure that bound elements are created using the extended class rather than the base class automatically associated with the element in the generated bindings. See also this page.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Here's an annotated and updated version of your example that does what I think you want:
# -*- coding: utf-8 -*-importloggingif__name__=='__main__':logging.basicConfig()_log=logging.getLogger(__name__)importpyxb.binding.generateimportpyxb.binding.datatypesasxsimportpyxb.binding.basisimportpyxb.utils.domutilsimportos.pathxsd='''<?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:complexType name="Foo"> <xs:sequence> <xs:element name="entry" type="xs:string"/> </xs:sequence> </xs:complexType> <!-- NEW element --> <xs:element name="Foo" type="Foo"/></xs:schema>'''code=pyxb.binding.generate.GeneratePython(schema_text=xsd)rv=compile(code,'test','exec')eval(rv)frompyxb.exceptions_import*importunittest# Note name clash resolved by renaming type, not elementclassFooWrapper(Foo_):# No need to override constructor.# Add functionality to extensiondeflcentry(self):returnself.entry.lower()# Ensure creation of element Foo() instantiates the wrapping classFoo_._SetSupersedingClass(FooWrapper)classTest_simple(unittest.TestCase):deftest(self):x=Foo()# Create through element, not underlying typex.entry="Something"self.assertEqual(x.toxml(),'''<?xml version="1.0" ?><Foo><entry>Something</entry></Foo>''')# Verify that bound element is instance of the extended classself.assertTrue(isinstance(x,FooWrapper))self.assertEqual('something',x.lcentry())if__name__=='__main__':unittest.main()
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
We frequently inherit from pyxb generated classes. (See attached file for example). This fails in the latest 1.2.3 with a start tag error. One way this can be fixed is, in the toDOM method in basis.py, insert the following lines:
these two lines were removed from the 1.2.2 basis.py and replaced with the exception above.
Last edit: Harold Solbrig 2013-11-11
The former behavior, and that resulting from your patch, is trac#203. That PyXB ever allowed a type to automatically become an element tag was an error.
You need to augment your schema so there is an element named Foo that has type Foo.
Also see more extended discussion here which provides an alternative solution where the desired element is explicitly associated with the binding instance.
In the context of extending classes, it may be that you're missing the call to _SetSupersedingClass() which is supposed to ensure that bound elements are created using the extended class rather than the base class automatically associated with the element in the generated bindings. See also this page.
Here's an annotated and updated version of your example that does what I think you want:
Thanks! I hadn't realized how the Superseding Class worked -- this is useful.