I am validating some XML against this XSD:
<xs:element name="Composite">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="HeldCurrency" type="mstns:HeldCurrencyType" nillable="true"/>
<xs:element name="Component" type="mstns:ComponentType" nillable="true" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="Type" type="mstns:compositeType" use="required"/>
</xs:complexType>x
</xs:element>
<xs:complexType name="HeldCurrencyType">
<xs:attribute name="CashPerCreationUnit" type="xs:double" />
<xs:attribute name="CashCurrency" type="xs:string" />
<xs:attribute name="ClosingSpotFx" type="xs:double" />
</xs:complexType>
<xs:complexType name="ComponentType">
<xs:choice>
<xs:element name="Weight" type="xs:double"/>
<xs:element name="Units" type="xs:int"/>
</xs:choice>
<xs:attribute name="Symbol" type="xs:string" />
</xs:complexType>
<xs:simpleType name="compositeType">
<xs:restriction base="xs:string">
<xs:enumeration value="Index" />
<xs:enumeration value="Etf" />
</xs:restriction>
</xs:simpleType>
When I run the following XML against it:
<?xml version="1.0"?>
<Composite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Type="Etf" xmlns="urn:xxx-au:index-types">
<HeldCurrency />
<Component />
</Composite>
I run this in C#/.Net4.0 with the following code:
using (FileStream fileStream = File.OpenRead("./Xml/Components.xsd"))
{
using (var schemaReader = new XmlTextReader(fileStream))
{
settings.Schemas.Add(null, schemaReader);
settings.ValidationType = ValidationType.Schema;
settings.ValidationEventHandler += OnValidationEventHandler;
using (FileStream infile = File.OpenRead(filename))
{
using(var xmlReader = XmlReader.Create(infile,settings))
{
var serialiser = new XmlSerializer(typeof (Composite));
_composite = (Composite) serialiser.Deserialize(xmlReader);
}
}
}
}
I get the following error:
The element ‘Component’ in namespace ‘urn:xxx-au:index-types’ has incomplete content. List of possible elements expected: ‘Weight, Units’ in namespace.
However, I have minOccurs=”0″ so why would this be an issue? It is legal for the sequence to contain no ComponentType elements after all, right?
Thanks in advance.
The
minOccurshere is for specifically the “Component” element — not its contents. ComponentType has content ofxs:choice, which does not have aminOccursof0. So you can omit “Component” entirely, but not its contents.