Andrii,

Attached to a separate email is a new version.  This version
generates code that:

1. does not export the child element if the element is optional
   (minOccurs="0") and the value of the element is equal to the
   default value;

2. exports the child element if the element is required
   (minOccurs >= 1) even if the value of the element is equal
   to the default value.

One point to consider: if the child element does *not* have a
default value and is mandatory (minOccurs >= 1) and it has a value of
None (that is, it has not been given a value the overrides the value
given by the constructor of the class), then it will not be
exported.  Actually, we can't export it, since we do not have a
value to export.

We may have to discuss this last point.

If the element has been created by parsing a file that contains it,
then it will have a value different from None and we're OK.  And,
since it is mandatory, any correct file (i.e. file that validates
against the schema) should contain that element, and will therefore
be exported.

This makes my head hurt.  Got to quit for today.

Please give me your comments and tell me how your testing goes with
this new version.  Thanks in advance.

Dave


On Thu, Nov 30, 2017 at 05:07:12PM +0000, Andrii Iudin wrote:
> Dear Dave,
> 
> Thank you for the promptly implemented patch. I have tested it and the
> result with the switch "--export-when-default" does not fail xmllint
> validation on framesPerImage anymore.
> 
> I have discussed this with my colleagues and your suggested alternative
> looks reasonable. If the element has more than zero minOccurs, than it has
> to be exported with the default value, otherwise if the minOccurs is zero,
> then it can be omitted.
> 
> Best regards,
> Andrii
> 
> On 29/11/2017 23:59, Dave Kuhlman wrote:
> > Andrii,
> > 
> > I've made a fix.  It's attached to a separate email.  I've done some
> > light testing and this change seems to work as I expect.  But, maybe
> > what I expect is wrong.
> > 
> > However, before using that new version, please read my comments
> > below.  I'm wondering whether I've made the correct fix.  See
> > below.
> > 
> > Following your hint, I have added a new command line option to
> > generateDS.py: --export-when-default
> > 
> > When you use this option, generateDS.py will now generate code that
> > exports children, even when a child's value is equal to the default
> > value specified in the XML schema.
> > 
> > Am I correct that we should apply this new behavior to child
> > elements, but *not* to attributes?  I did a little reading on this
> > and it appears that an attribute with a default value is optional
> > but that a child element with a default value and minOccurs greater
> > than zero is not.
> > 
> > If the above is correct, perhaps instead of a new command line
> > option, this new behavior (not omitting the child element) should
> > always be done, or at least it should be the default and the new
> > flag should generate code that has the old behavior (exports the
> > child element when the value equals the default).
> > 
> > So, here is an alternative:
> > 
> > For child elements defined with a default value, generate code that
> > (a) always exports the child element if it's minOccurs is greater
> > than zero and (b) if its minOccurs is zero (it is optional) exports
> > it only if its value is different from the default.
> > 
> > If we do this, then we can get rid of the --export-when-default
> > option in the patched version I'm sending.
> > 
> > What do you think?
> > 
> > Dave
> > 
> > On Tue, Nov 28, 2017 at 11:28:39AM +0000, Andrii Iudin wrote:
> > > Dear Dave,
> > > 
> > > We are having a problem with exporting an element with a set default 
> > > value.
> > > The element in question is
> > > <xs:element name="framesPerImage" type="xs:integer" default="1">
> > >      <xs:annotation>
> > >          <xs:documentation>Normally = 1.
> > > Relevant for multi-frame direct electron detectors.</xs:documentation>
> > >      </xs:annotation>
> > > </xs:element>
> > > 
> > > It seems to be exported only when its value is not equal to the default 
> > > one
> > > if self.framesPerImage != 1:
> > >      showIndent(outfile, level, pretty_print)
> > > outfile.write('<%sframesPerImage>%s</%sframesPerImage>%s' % (namespace_,
> > > self.gds_format_integer(self.framesPerImage, input_name='framesPerImage'),
> > > namespace_, eol_))
> > > 
> > > As a result, xmllint fails to validate the exported XML since it is 
> > > missing
> > > framesPerImage element.
> > > 
> > > Please could you tell if there is a switch that could force exports of
> > > elements if they have a default value? The relevant code seems to be
> > > starting at line 2080
> > >          if default is None:
> > >              wrt('%s        if self.%s is not None:\n' % (fill, 
> > > mappedName,
> > > ))
> > >          else:
> > >              wrt('%s        if self.%s != %s:\n' % (
> > >                  fill, mappedName, default, ))
> > > 
> > > Many thanks and best regards,
> > > Andrii
> > > 
> > > ------------------------------------------------------------------------------
> > > 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
> 

-- 

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

Reply via email to