Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSD.exe doesn't enforce minOccurs

I have a simple schema where I'm declaring both minOccurs and maxOccurs to 1. When I run the XSD.exe to generate a C# class and consume the class in code; the field is not enforced as mandatory. Is there some additional step missing? or does the classes generated using XSD.exe don't mandatory fields?

any suggestions or insight will be helpful.

like image 996
G33kKahuna Avatar asked May 03 '10 14:05

G33kKahuna


People also ask

What is minOccurs in XSD?

The minOccurs attribute specifies the minimum number of times that the element can occur. It can have a value of 0 or any positive integer. The maxOccurs attribute specifies the maximum number of times that the element can occur.

What is the default minOccurs in XSD?

The default value for both the minOccurs and the maxOccurs attributes is 1. Thus, when an element such as comment is declared without a maxOccurs attribute, the element may not occur more than once.

Why we use Xsd file in Java?

It is primarily used to define the elements, attributes and data types the document can contain. The information in the XSD is used to verify if each element, attribute or data type in the document matches its description.

What is Xsd file in Java?

xsd is the XML schema you will use as input to the JAXB binding compiler, and from which schema-derived JAXB Java classes will be generated. For the Customize Inline and Datatype Converter examples, this file contains inline binding customizations.


2 Answers

Like the Xml / infer schema tool in visual studio, whenever I've used XSD.exe I've ended up fixing some of the generated code. XSD.exe does a good approximation but it doesn't work entirely.

The minOccurs / maxOccurs 'bug' is documented on MSDN.

When generating source code from an XML Schema document, Xsd.exe ignores the minOccurs attribute applied to the < choice >, < sequence >, < group >, < all >, and < any > elements.

For the element, Xsd.exe ignores the minOccurs attribute if the value of the maxOccurs attribute is greater than 1, or unbounded. In this case, the tool produces an array of the type corresponding to the XSD data type. Xsd.exe uses the value of the maxOccurs attribute to determine whether to produce a single instance or an array.

For the element, Xsd.exe also ignores the minOccurs attribute if it is applied to a schema data type that converts to a .NET Framework reference type.

Only when all the following conditions are true does Xsd.exe utilize the value of the minOccurs attribute:

The element is involved.

The maxOccurs attribute dictates a single instance.

The data type converts to a value type.

like image 183
amelvin Avatar answered Oct 31 '22 05:10

amelvin


How would you enforce an arbitrary minoccurs in code? Some sort of assertions? I guess you can only map to "Non-nullable single instance" (1:1) non-nullable array (1:many) and their nullable equivalents (0;1), and (0:many)

like image 26
Doobi Avatar answered Oct 31 '22 06:10

Doobi