Reputation: 110267
I am trying to allow the following XML pattern:
<Locales>
<Locale Language="FR">
<Name>La Jetée</Name>
</Locale>
<Locale Language="EN">
<Name>The Jetty</Name>
</Locale>
</Locales>
Here is the XSD I currently have, but it is giving an error about the attributes. When I remove the attributes it validates
<xs:element name="Locales" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="Locale" maxOccurs="unbounded" minOccurs="1">
<xs:complexType>
<xs:attribute name="Language" use="optional"/>
<xs:all>
<xs:element name="Name" type="xs:string" minOccurs="0"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
The error I get is
Element 'all' Is Invalid, Misplaced, Or Occurs Too Often.
Upvotes: 1
Views: 1276
Reputation: 111620
Your XSD is fine except that you have to move xs:all
before xs:attribute
; it may not appear after xs:attribute
, thus the error.
Here is your XSD fragment with the above change applied:
<xs:element name="Locales">
<xs:complexType>
<xs:sequence>
<xs:element name="Locale" maxOccurs="unbounded" minOccurs="1">
<xs:complexType>
<xs:all>
<xs:element name="Name" type="xs:string" minOccurs="0"/>
</xs:all>
<xs:attribute name="Language" use="optional"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
It will successfully validate your XML. Note that it also removes minOccurs="0"
because occurrence constraints may not appear on top-level elements.
Upvotes: 1
Reputation: 110267
First of all, you should be able to use a tool such as http://www.freeformatter.com/xsd-generator.html to infer the xsd structure that's required.
For the above case, something like the following should work:
<xs:element name="Locales">
<xs:complexType>
<xs:sequence>
<xs:element name="Locale" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="Name"/>
</xs:sequence>
<xs:attribute type="xs:string" name="Language" use="optional"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
Upvotes: 0