[xsd-users] cxx-parser and optional element or attribute

Boris Kolpackov boris at codesynthesis.com
Mon Aug 29 06:31:35 EDT 2011


Hi Vaughan,

Vaughan Roberts <Vaughan at Roberts.name> writes:

> I have been using cxx-parser to parse an xml document which has a number of
> optional elements and attributes.  My problem seems to be that if the
> optional element is not present I still get the previous value of the
> element returned.  
>
> [...]
>
> <xsd:element name="fielder" type="xsd:string" minOccurs="0" maxOccurs="1"/>
>
> [...] 
>
> It is the optional elements: fielder and assist that I am having trouble
> with at the moment.  If they are provided for a Ball, they only relate to
> that Ball and should be reset afterwards. What I seem to be getting at the
> moment is that there is nothing until the first time they have a value and
> then that value is returned for all subsequent Balls.

In C++/Parser, for optional elements (and attributes) that are not 
present, the corresponding callback function is simply not called.
I tries to reproduce your problem using the library example. This
examples has the died element which is optional. I modified the
sample library.xml file by removing this element from the second
book entry. However, the example still works as expected; I don't
get the date of death for the second book and get the correct
values for the first and third.

Perhaps what happens in your case is that you do not clear the data
member that is used to store the optional element. As a result, when
the element is missing (and the callback is not called), it appears
as if the old value was specified. The place to clear such optional
(and container) values is in the _pre() callback. We do this for the
died element in the library example. Check library-pimpl.cxx, line
60.

Boris



More information about the xsd-users mailing list