Vincent, Thank you for your analysis on this. It's been very helpful.
So, it seems that you are saying that when we have a child that is both complex and mandatory, for example: <xs:complexType name="_CellConfigType"> <xs:all> <xs:element name="simple" type="_SimpleType" minOccurs="1" maxOccurs="1" default="300"/> <xs:element name="complexOptionConfig" type="_ComplexType" minOccurs="1" maxOccurs="1"/> </xs:all> </xs:complexType> then we should generate: class _CellConfigType(GeneratedsSuper): subclass = None superclass = None def __init__(self, simple=300, complexOptionConfig=None):" self.simple = simple if complexOptionConfig is None: self.complexOptionConfig = globals()['_ComplexType']() else: self.complexOptionConfig = complexOptionConfig The reason for using "globals['_ComplexType']" instead of accessing the value directly is because we need to avoid a conflict with a local name. Do I understand you correctly. If so, I believe that I agree with you. We should call the constructor for the child element when that child element is mandatory. But, when I tried to make this change, it introduced a bug. I have to figure out how to do this for elements defined with xs:complexType but not those defined with xs:complexType and xs:simpleContent. Not sure. Need to study it a bit more. More tomorrow. Dave On Tue, Aug 14, 2018 at 09:44:49AM +0000, Vincent Helfre wrote: > Hi Dave, > > I also found that default must be used for simple types only and cannot be > used for complex types. > So this syntax cannot be used: <xs:element name="item1" type="complexTypeB" > default="xxx"/> > > But it looks valid to have a complexType mandatory (minOccurs="1") and that > the elements of this complexType have default values defined. > > Attaching ServiceClient.xsd below. > After calling generateDS, we get ServiceClient.py and ServiceClientSub.py. > In ServiceClient.py, in the _CellConfigType class, complexOptionConfig is > initialized to None: > "class _CellConfigType(GeneratedsSuper): > subclass = None > superclass = None > def __init__(self, simple=300, complexOptionConfig=None):" > > > But the complexOptionConfig element is mandatory and every element of > _ComplexType has default values. > => do you have a suggestion to be able to call the _ComplexType() constructor > to initialize complexOptionConfig instead setting it to None? > > It looks like other editor work this way. Attaching a xml generated with > Altova editor. There the editor automatically adds the complexOptionConfig > element. Also it does not set explicitly the values of the elements of > complexOptionConfig since default values are defined for it in the xsd. > > Example ServiceClient.xsd > <?xml version="1.0"?> > <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" > elementFormDefault="qualified" attributeFormDefault="unqualified"> > <xs:simpleType name="_Option1"> > <xs:restriction base="xs:token"> > <xs:enumeration value="Option1_CONFIG_0"/> > <xs:enumeration value="Option1_CONFIG_1"/> > </xs:restriction> > </xs:simpleType> > <xs:simpleType name="_Option2"> > <xs:restriction base="xs:token"> > <xs:enumeration value="Option2_CONFIG_0"/> > <xs:enumeration value="Option2_CONFIG_1"/> > <xs:enumeration value="Option2_CONFIG_2"/> > </xs:restriction> > </xs:simpleType> > <xs:complexType name="_ComplexType"> > <xs:sequence> > <xs:element name="Option1" type="_Option1" > default="Option1_CONFIG_0"/> > <xs:element name="Option2" type="_Option2" > default="Option2_CONFIG_0"/> > </xs:sequence> > </xs:complexType> > <xs:simpleType name="_SimpleType"> > <xs:restriction base="xs:unsignedInt"> > <xs:minInclusive value="0"/> > </xs:restriction> > </xs:simpleType> > <xs:complexType name="_CellConfigType"> > <xs:all> > <xs:element name="simple" type="_SimpleType" > minOccurs="1" maxOccurs="1" default="300"/> > <xs:element name="complexOptionConfig" > type="_ComplexType" minOccurs="1" maxOccurs="1"/> > </xs:all> > </xs:complexType> > <xs:simpleType name="_CellHandle"> > <xs:restriction base="xs:unsignedInt"/> > </xs:simpleType> > <xs:complexType name="_Cell"> > <xs:sequence> > <xs:element name="CellHandle" type="_CellHandle" > default="0" minOccurs="1"/> > <xs:element name="CellConfig" type="_CellConfigType" > minOccurs="1"/> > </xs:sequence> > </xs:complexType> > <xs:element name="Root"> > <xs:complexType> > <xs:sequence> > <!-- maximum 8 cells --> > <xs:element name="Cell" type="_Cell" > minOccurs="1" maxOccurs="8"/> > </xs:sequence> > </xs:complexType> > </xs:element> > </xs:schema> > > XML generated with Altova Editor: > the editor requires one Cell element to be present. Then all elements of Cell > (with minOccurs="1") are present, even ComplexType and the ComplexType > content is populated with default values defined for the elements of > ComplexType > <?xml version="1.0" encoding="UTF-8"?> > <Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > xsi:noNamespaceSchemaLocation="ServiceClient.xsd"> > <Cell> > <CellHandle></CellHandle> > <CellConfig> > <simple></simple> > <complexOptionConfig> > <Option1></Option1> > <Option2></Option2> > </complexOptionConfig> > </CellConfig> > </Cell> > </Root> > > Thanks for your feedback! > > Best regards, > > Vincent > > -----Original Message----- > From: Dave Kuhlman <dkuhl...@davekuhlman.org> > Sent: Tuesday, August 14, 2018 1:16 AM > To: Helfre Vincent 5RSV-AE <vincent.hel...@rohde-schwarz.com> > Cc: generateds-users <generateds-users@lists.sourceforge.net> > Subject: *EXT* Re: setting default values for complextypes > > Vincent, > > > first I would like to thank you a lot for your generateDS script and > > the good documentation. > > > > I have a question regarding the setting of default values. > > You are welcome. I appreciate the comment. > > It seems that you want to be able to specify a default value that is an > xs:complexType. I do not know how you would do that. > > Suppose you had: > > <xs:complexType name="complexTypeA"> > <xs:sequence> > <xs:element name="description" type="xs:string"/> > <xs:element name="item1" type="complexTypeB" default="xxx"/> > </xs:sequence> > </xs:complexType> > > What would you put in the place of xxx? I know how to specify a default > value for primitive and simple types (for example, an xs:string, an > xs:integer, or an xs:simpleType that restricts a primitive type). But, I do > not know how to do that for complex types. > > I did several Web searches, but found no help on how to do that. I believe > that default values must simple types. > > Am I interpreting your question incorrectly? If so, please straighten me out. > > By the way, your task (loading a database from an XML instance > document) seems like a ETL (extract, transform, load) sort of problem. I am > currently working on adding support to generateDS for a similar capability so > that it can load XML data into a Django database. So, someday, maybe, I will > have a little more support for that kind of task. > > Thank you for your question. And, if I can be of more help, please ask. > > Dave > > On Mon, Aug 13, 2018 at 10:32:42AM +0000, Vincent Helfre wrote: > > Dear David, > > first I would like to thank you a lot for your generateDS script and > > the good documentation. > > > > I have a question regarding the setting of default values. > > A customer gives me a xsd where all the default values are set (for > > simple types). Then the structure can be quite deep and have several > > levels that look like for example: > > complexTypeA->ComplexTypeB->SimpleTypeC > > I have to create a database in Python with the structure that holds > > these default values. > > The problem I am facing now is that for a tree like > > complexTypeA->ComplexTypeB->SimpleTypeC, the constructor generated > > via generateDS for the complexTypeA initializes ComplexTypeB with > > None. So my structure does not hold the default values for > > ComplexTypeB. > > What works is that ComplexTypeB initializes SimpleTypeC with the > > default values specified in the xsd. > > Would you have a suggestion to initialize complexTypeA with a default > > value for ComplexTypeB? > > Thanks for your feedback! > > Best regards, > > > > -- > > Dave Kuhlman > http://www.davekuhlman.org -- Dave Kuhlman http://www.davekuhlman.org ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot _______________________________________________ generateds-users mailing list generateds-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/generateds-users