Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I mix unbounded groups with restricted groups in a complexType?
    text
    copied!<p>I'm trying to create a tool that can generate XSDs for the XAML produced by serializing a type in .NET, but this is not specifically .NET related.</p> <p>Let's say I have a type. This type has properties and is a collection. </p> <pre><code>public class MyType : Collection&lt;CollectedType&gt; { public PType1 PropertyOne {get;set;} public PType2 PropertyTwo {get;set;} } </code></pre> <p>This serializes to the following (omitting the object graph construction):</p> <pre><code>&lt;MyType xmlns="clr-namespace:blahblahblah"&gt; &lt;CollectedType name="First instance in the collection"/&gt; &lt;CollectedType name="Second instance in the collection"/&gt; &lt;MyType.PropertyOne&gt; &lt;PType1 Value = "Serialized object in PropertyOne" /&gt; &lt;/MyType.PropertyOne&gt; &lt;MyType.PropertyTwo&gt; &lt;PType2 Value = "Serialized object in PropertyTwo" /&gt; &lt;/MyType. PropertyTwo &gt; &lt;/MyType&gt; </code></pre> <p>In generating the XSD for this type, I can say the following:</p> <ul> <li>MyType is a complexType</li> <li>MyType will contain a reference to a group called CollectedTypeGroup</li> <li><ul> <li>CollectedTypeGroup contains CollectedType's element and elements for types that extend CollectedType</li> </ul></li> <li>MyType will contain an element called MyType.PropertyOne</li> <li><ul> <li>PType1 is another complexType</li> </ul></li> <li>MyType will contain an element called MyType.PropertyTwo</li> <li><ul> <li>PType2 is another complexType</li> </ul></li> </ul> <p>This is all relatively easy to do. Here's a chunk of the generated xsd:</p> <pre><code> &lt;xs:complexType name="MyType"&gt; &lt;xs:sequence&gt; &lt;xs:element name="MyType.PropertyOne" type="PType1"/&gt; &lt;xs:element name="MyType.PropertyOne" type="PType1"/&gt; &lt;xs:group ref="CollectedTypeGroup"/&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; </code></pre> <p>Now comes the hard part. Because the XML will be mapped back to an object graph, I have a list of restrictions on how elements are added to MyType that <strong>MUST BE</strong> enforced by the schema. If these three requirements <strong>and only these three</strong> are not enforced, I face issues with users attempting to use my schema:</p> <p>1) Elements added to MyType <strong>must not be restricted to a particular order</strong><br> 2) Elements that represent a property of my object can <strong>only appear once or not at all</strong><br> 3) Group elements <strong>must be unbounded</strong>; they can appear anywhere in the parent element and 0...* times </p> <p>This is where I am having a horrendous time. I cannot find a satisfactory combination of choices, sequences, alls etc. to satisfy these three requirements. I have also tried placing the elements in a separate group, complexTypes, etc. Nothing seems to work. </p> <p>How can I combine my elements and my groups into a single complexType and meet my three requirements?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload