Hi Dave, sorry for the late answer and thank you for introducing the option "--create-mandatory-children". Please find my feedback embedded below. Thanks a lot for your time.
Best regards, Vincent -----Original Message----- From: Dave Kuhlman <dkuhl...@davekuhlman.org> Sent: Friday, August 17, 2018 12:37 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, Perhaps I have a fix. I've introduced a new command line option "--create-mandatory-children". You will need to use this in order to generate code that has this new behavior. I feel that since I have not had this request before, it's safer to leave other users with the old behavior. I hope this is not too much of an inconvenience for you. The new code, with this fix, is at Bitbucket -- https://bitbucket.org/dkuhlman/generateds Please give it a try. Thank you for your help and guidance with this. Please let me know whether it works for you. I'll take a look at your idea about xs:sequence tomorrow. By the way, do you agree that we should not do this for a child defined with xs:simpleContent? The rest of this message is notes I wrote while trying to understand this. Please don't feel obligated to read it all. But, if you do and disagree with something, please let me know. Trying to think this through ... mumbling to myself ... We need to answer two questions: 1. When do we generate this code to create an instance of a child? For which child elements? 2. What (new) code do we generate? With respect to question 1 -- We should generate this new code when all of the following conditions are true: 1. The child is defined as an xs:complexType. [VH]: since the generated code already passes the default value to the __init__() function, there is no need for generating new code. 2. The child is mandatory, for example, it is defined with one of the following: <xs:complexType name="_CellConfigType"> <xs:all> <xs:element name="mandatory_child1" type="SomeComplexType" minOccurs="1" maxOccurs="1"/> <xs:element name="mandatory_child2" type="SomeComplexType"/> </xs:all> </xs:complexType> Note that `mandatory_child1` is mandatory because the default values for both `minOccurs` (and `maxOccurs`) is 1. In other words: (a) the child is not optional (minOccurs == 1) and the child does not allow multiple instances (maxOccurs > 1). [VH] corrected mandatory_child1 typo 3. The child is *not* defined as having xs:simpleContent. If it is xs:simpleContent, then it should be treated as a simple type. It does not seem to make sense to me to try to generate the element when we do not know what it's text content should be. [VH] agreed 4. All the sub-elements have default values. (?) Or, is that at least one sub-element (child) has a default value? Or, is this condition not needed? Whatever. I am currently not checking for and enforcing this, anyway. [VH] The Altova editor is not checking that all sub-elements have defaults values when creating the mandatory present parent element. Only when saving the xml it throws an error if the user does not specify a value for the child element where no default value is defined in the xsd. With respect to question 2 -- We should generate the following (new) code: 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 That is to say, whenever a value is not explicitly passed to the constructor for the mandatory complex child, construct an instance of the class that implements it. Are we on track? Any suggestions or corrections? Does the above describe the behavior that we want? [VH]: it sounds good to me. Dave On Thu, Aug 16, 2018 at 12:41:10PM +0000, Vincent Helfre wrote: > Hi Dave, > > yes this would exactly match my requirements for the initialization of > the data, based on xsd default values (and also what I observed in the > Altova editor). > > I have one more query regarding the sequence (here the line in the > xsd: " <xs:element name="Cell" type="_Cell" minOccurs="1" > maxOccurs="8"/>") > The xsd requires at least one Cell to be present in the sequence. > Is it also possible to introduce this into sequence initialization, so > that one Cell element is present if minOccurs=1 is ? > > class Root(GeneratedsSuper): > subclass = None > superclass = None > def __init__(self, Cell=None): > self.original_tagname_ = None > if Cell is None: > self.Cell = [] > self.Cell.append(globals()['_Cell']()) > else: > self.Cell = Cell > > About the minimum number of occurrence in a sequence, this syntax is > maybe easier to handle (both this one and the one from my previous > email seem correct) > > <xs:complexType> > <xs:sequence minOccurs="1" maxOccurs="8"> > <xs:element name="Cell" type="_Cell"/> > </xs:sequence> > </xs:complexType> > > After editing the generated ServiceClient.py to add the initialization > you described for _CellConfigType, _Cell (and also for the Root for > the Cell sequence which should have a minimum of one Cell), I could > see that the complexType is populated with default values using the > test code below: > " > import ServiceClient > import sys > > if __name__ == "__main__": > tc = ServiceClient.Root() > tc.export(sys.stdout, 0) > print ("complexOptionConfig option1: " + > tc.Cell[0].CellConfig.complexOptionConfig.Option1) > " > => an object populated with all default values defined in the xsd is > created. :) > > Best regards, > > Vincent > > -----Original Message----- > From: Dave Kuhlman <dkuhl...@davekuhlman.org> > Sent: Wednesday, August 15, 2018 2:26 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, > > 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 -- Dave Kuhlman http://www.davekuhlman.org Content provided within this e-mail including any attachments, is for the use of the intended recipients and may contain Rohde & Schwarz company restricted information. Any unauthorized use, disclosure, or distribution of this communication in whole or in part is strictly prohibited. If you are not the intended recipient, please notify the sender by reply email or by telephone and delete the communication in its entirety. ------------------------------------------------------------------------------ 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