XmlSchemaClassGenerator icon indicating copy to clipboard operation
XmlSchemaClassGenerator copied to clipboard

xs:choice duplicated element

Open kubajt opened this issue 2 years ago • 3 comments

"xs:choice" with same names in branches causes error "The XML element '...' from namespace '...' is already present in the current scope."

For example:

<xs:choice minOccurs="0">
  <xs:sequence>
    <xs:element name="MinAge" type="PersonAgeType" minOccurs="1"></xs:element>
    <xs:element name="MaxAge" type="PersonAgeType" minOccurs="0"></xs:element>
  </xs:sequence>
  <xs:sequence>
    <xs:element name="MaxAge" type="PersonAgeType" minOccurs="1"></xs:element>
  </xs:sequence>
</xs:choice>

gives

[System.Xml.Serialization.XmlElementAttribute("MaxAge")]
public uint MaxAgeValue { get; set; }
...
[System.Xml.Serialization.XmlElementAttribute("MaxAge")]
public uint MaxAge1Value { get; set; }

"MaxAge" is duplicated.

kubajt avatar May 05 '23 11:05 kubajt

Support for choices is incomplete, see https://github.com/mganss/XmlSchemaClassGenerator#choice-elements I'd recommend changing the input schema to something like this (just for the purpose of generating classes):

<xs:complexType>
    <xs:sequence>
	    <xs:element name="MinAge" type="PersonAgeType" minOccurs="0">
	    </xs:element>
	    <xs:element name="MaxAge" type="PersonAgeType" minOccurs="0">
	    </xs:element>
    </xs:sequence>
</xs:complexType>

mganss avatar May 05 '23 15:05 mganss

Another example from the standard schema fpml-valuation-4.6.xsd (e.g. one cannot change it). It generates collection property List<DateTime> AdjustedData instead of optional value. Interesting enough, this happens when generating interface for the group. Class that implements this interface actually generates a scalar property.

<xsd:group name="AdjustedAndOrUnadjustedDate.model">
    <xsd:annotation>
      <xsd:documentation xml:lang="en">Contains at least one of an adjusted date and and unadjusted date, using the usual meanings of those terms.</xsd:documentation>
    </xsd:annotation>
    <xsd:choice>
      <xsd:sequence>
        <xsd:element name="unadjustedDate" type="xsd:date" />
        <xsd:element name="adjustedDate" type="xsd:date" minOccurs="0" />
      </xsd:sequence>
      <xsd:element name="adjustedDate" type="xsd:date" />
    </xsd:choice>
  </xsd:group>

michaelplavnik avatar Dec 22 '23 00:12 michaelplavnik

@michaelplavnik Could this be a duplicate of #475?

mganss avatar Jan 12 '24 17:01 mganss