mganss / XmlSchemaClassGenerator

Generate C# classes from XML Schema files
Apache License 2.0
625 stars 180 forks source link

Wildcard '##any' causes the content model to become ambiguous #482

Open jflevesque-genetec opened 10 months ago

jflevesque-genetec commented 10 months ago

Hi,

First time user of this generator and I'm getting these warnings that seem to prevent the code gen from completing. I am currently trying to understand how I should modify the XSD file to resolve these issues.

xscgen --namespace "|onvif.xsd=My.Custom.Namespace" --nullable --gc --netCore --nullableReferenceAttributes --verbose --output="c:\temp" common.xsd

Generates these logs:

Wildcard '##any' allows element 'http://www.onvif.org/ver10/schema:Extension', and causes the content model to become ambiguous. A content model must be formed such that during validation of an element information item sequence, the particle contained directly, indirectly or implicitly therein with which to attempt to validate each item in the sequence in turn can be uniquely determined without examining the content or attributes of that item, and without any information about the items in the remainder of the sequence.
Wildcard '##any' allows element 'http://www.onvif.org/ver10/schema:Weight', and causes the content model to become ambiguous. A content model must be formed such that during validation of an element information item sequence, the particle contained directly, indirectly or implicitly therein with which to attempt to validate each item in the sequence in turn can be uniquely determined without examining the content or attributes of that item, and without any information about the items in the remainder of the sequence.

Any ideas?

common.xsd can be found here: https://www.onvif.org/ver10/schema/common.xsd

mganss commented 10 months ago

The error message is a validation error that occurs when compiling the XML schema using XmlSchemaSet.Compile().

Though the error message is pretty cryptic, I think what it means is that it's impossible to validate a document against the schema because of this part:

<xs:complexType name="ColorDescriptor">
        <xs:sequence>
            <xs:element name="ColorCluster" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="Color" type="tt:Color"/>
                        <xs:element name="Weight" type="xs:float" minOccurs="0"/>
                        <xs:element name="Covariance" type="tt:ColorCovariance" minOccurs="0"/>
                        <xs:any namespace="##any" processContents="lax" minOccurs="0" maxOccurs="unbounded"/>   <!-- reserved for ONVIF -->
                    </xs:sequence>
                    <xs:anyAttribute processContents="lax"/>
                </xs:complexType>
            </xs:element>
            <xs:element name="Extension" type="xs:anyType" minOccurs="0"/>
            <xs:any namespace="##any" processContents="lax" minOccurs="0" maxOccurs="unbounded"/>   <!-- reserved for ONVIF -->
        </xs:sequence>
        <xs:anyAttribute processContents="lax"/>
</xs:complexType>
  1. If Weight occurs after Color, is it the explicit Weight element or were both Weight and Covariance omitted (minOccurs="0") and it's part of the any?
  2. Similarly, if Extension occurs, is it the explicit element or was that omitted and it's part of the any?

If you remove both of the above lines containing xs:any (lines 224 and 230) it will generate code. However, I'm not sure if this would allow all real life documents to deserialize.

Another way to fix the schema is to set minOccurs="1" for Covariance and Extension. Again, not sure what real life documents will look like for your use case and if they can be deserialized with this modification to the schema.

Third option I can think of is to make the owners of the schema aware of this ambiguity and have them fix it. Not sure if that's feasible of course. The organization that publishes the schema has a GitHub account https://github.com/onvif/ but I haven't investigated further if they accept issue reports in the schemas they publish.