Nikhil
Nikhil

Reputation: 2308

How to get rid of The attribute 'minOccurs' is not allowed error for SimpleTypes?

I am seeing the above error while validating my schema

xmllint --noout --schema main.xsd main.xml
main.xsd:44: element element: Schemas parser error : Element     '{http://www.w3.org/2001/XMLSchema}element': The attribute 'minOccurs' is not allowed.
main.xsd:53: element element: Schemas parser error : Element     '{http://www.w3.org/2001/XMLSchema}element': The attribute 'minOccurs' is not allowed.
WXS schema main.xsd failed to compile

My schema is as shown. After searching I found this error to be seen in complex types that includes elements without specifying the sequence tag.

What is the reason I am seeing this error?

 <?xml version="1.0" encoding="UTF-8" ?>
 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:simpleType name="ProtocolType"> 
    <xs:restriction base="xs:string"> 
        <xs:enumeration value="HTTP"/> 
        <xs:enumeration value="HTTPS"/> 
        <xs:enumeration value="SSL"/> 
        <xs:enumeration value="TCP"/>
    </xs:restriction> 
</xs:simpleType>

<xs:simpleType name="URL">
    <xs:restriction base="xs:string">
        <xs:pattern value="[hH][tT]{2}[pP]://[wW]{3}.*"/> 
    </xs:restriction>
</xs:simpleType>

<xs:simpleType name="ipv4">
    <xs:annotation>
        <xs:documentation>
            An IP version 4 address.
        </xs:documentation>
    </xs:annotation>
    <xs:restriction base="xs:token">
    <xs:pattern
        value="(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])"/>
        <xs:pattern
        value="[0-9A-Fa-f]{8}"/>
    </xs:restriction>
</xs:simpleType>

<!-- Server: Either ip adddress or url. 
    We might support more than one here -->
<xs:simpleType name="Server">
    <xs:union memberTypes="URL ipv4" />
</xs:simpleType>

<xs:simpleType name="Protocols">
    <xs:list itemType="ProtocolType" />
</xs:simpleType>

<xs:element name="TotalRunTime" minOccurs="0" default="0"> 
    <xs:simpleType>
        <xs:restriction base="xs:positiveInteger"> 
            <xs:minExclusive value="0" />
            <xs:maxInclusive value="3600"/> 
        </xs:restriction> 
    </xs:simpleType> 
</xs:element> 

<xs:element name="Interface" minOccurs="0" type="xs:string" />

</xs:schema>

Upvotes: 3

Views: 6830

Answers (3)

AKaramitsos
AKaramitsos

Reputation: 21

You can only have the minOccurs and maxOccurs attributes at an element that's inside a complexType. The element that has these attributes cannot be global(direct child of the schema element).

Upvotes: 1

Michael Kay
Michael Kay

Reputation: 163262

A global element declaration (appearing as a child of xs:schema) is a reusable description of elements that describe what they can contain, not where they can appear. The constraints on where (and how often) the element can appear comes in a complex type that makes use of the reusable description by means of an declaration. It's this reference that must contain the occurrence limits, not the element declaration itself.

The form used in Matthew Haugen's answer combines the element declaration and reference into a single construct (a local element declaration) which is more concise but less flexible, because you can't reuse the same element declaration if you later find it can appear in more than one place.

Upvotes: 2

Matthew Haugen
Matthew Haugen

Reputation: 13286

Try wrapping those two elements in a root element. minOccurs is only really relevant when you're dealing with a list, which the root of an xml document is not.

<xs:element name="root">
   <xs:complexType>
       <xs:all>
           <xs:element name="TotalRunTime" minOccurs="0" default="0"> 
               <xs:simpleType>
                    <xs:restriction base="xs:positiveInteger"> 
                        <xs:minExclusive value="0" />
                        <xs:maxInclusive value="3600"/> 
                    </xs:restriction> 
                </xs:simpleType> 
            </xs:element>

            <xs:element name="Interface" minOccurs="0" type="xs:string" />
        </xs:sequence>
    </xs:complexType>
</xs:element>

Upvotes: 2

Related Questions