[xsd-users] <choice> abuse

Ray Lischner rlischner at proteus-technologies.com
Mon Mar 12 11:26:58 EDT 2007


The implemenentation of <choice> has the potential for abuse. Suppose I have a schema with the following:
<complexType name="X">
  <choice>
    <element name="a" type="int"/>
    <element name="b" type="float"/>
    <element name="c" type="string"/>
  </choice>
</complexType>
 
The generated class X has three optional members: a, b, and c. The problem is that I can set all three members:
 
X x;
x.a(20);
x.b(3.14);
x.c("hello");
 
with the nonsensical result that the "choice" has all three values, not only one. I wish the code were more resilient to programmer error. Calling x.b(2.718) should result in a call to x.a().reset() and x.c().reset().
 
Or the implementation of <choice> could switch to a union, with a generated enumeration to specify which union member is valid.
 
Meanwhile, I'm open to suggestions for the best way to ensure that programmers do not inadvertently create invalid objects.
--
Ray Lischner, Proteus Technologies



More information about the xsd-users mailing list