10

When I run my code it gives me this error

[ s4s-att-not-allowed: Attribute 'maxOccurs' cannot appear in element 'element'.]

Here is my Schema :

<xs:element name="parameters" maxOccurs="1" minOccurs="0">
    <xs:complexType>
        <xs:all>
            <xs:element ref="p ?"/> 
        </xs:all>
    </xs:complexType>
</xs:element>
helderdarocha
  • 23,209
  • 4
  • 50
  • 65
user3445281
  • 107
  • 1
  • 1
  • 4
  • 2
    That's not your full schema. Context is important with `minOccurs` and `maxOccurs`. They are related to a group. In what context is your `xs:element` defined? You can declare `top-level` elements, but they can't have maxOccurs or minOccurs since they are not in a context. Edit your question and paste the full context (from the root of your schema). – helderdarocha Apr 16 '14 at 17:35
  • Also `p ?` is invalid as the value of `ref`. If you mean *one or none* you can use `` (you can omit `maxOccurs="1"` because it's default) – helderdarocha Apr 16 '14 at 17:52
  • The thing to remember here is that every XML document can have "exactly one single root element" so that's why you basically cannot specify max and min values for the root--it's implied max+max of 1, so it doesn't even allow you to specify any. – rogerdpack Mar 11 '15 at 20:42

1 Answers1

8

<xs:element> may be declared at top-level (below xs:schema) but it can't have minOccurs or maxOccurs since that doesn't make any sense without a context. If it's root it can only have one element, if it's not, that information refers to the context of the parent element. This is legal:

<xs:schema ...>
    <xs:element name="parameters">...</xs:element>
    ...
</xs:schema>

but this is not:

<xs:schema ...>
    <xs:element name="parameters" maxOccurs="1" minOccurs="0">...</xs:element>
...
</xs:schema>

You can refer to a top level xs:element within a group such as xs:sequence. Here you can use these attributes because now you have a context (how many are allowed in this group). This is legal:

<xs:schema ...>
    <xs:element name="parent">
        <xs:complexType>
            <xs:sequence>
                 <xs:element ref="parameters" maxOccurs="1" minOccurs="0" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="parameters">
        <xs:complexType>
            <xs:all>
                <xs:element ref="p" minOccurs="0"/> 
            </xs:all>
        </xs:complexType>
    </xs:element>
    ...
</xs:schema>

Here <parent> is the context where <parameters> occurs, so you can say how many times it's allowed in there. The definition of <parameters> is global and you use the ref attribute to refer to it.

If you never need to reuse parameters or if you are never going to have parameters as root, you don't need it at top-level and can nest it inside your parent definition. In this case you can use the name attribute with minOccurs and maxOccurs.

<xs:schema ...>
    <xs:element name="parent">
        <xs:complexType>
            <xs:sequence>
                 <xs:element name="parameters" maxOccurs="1" minOccurs="0" />
                     <xs:complexType>
                          <xs:all>
                               <xs:element ref="p" minOccurs="0"/> 
                          </xs:all>
                     </xs:complexType>
                 </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    ...
</xs:schema>

You can also refer to a top-level type. It's more common to reuse, extend and restrict types, so this is also a valid (and recommended) way to define your element:

<xs:schema ...>
    <xs:element name="parent">
        <xs:complexType>
            <xs:sequence>
                 <xs:element name="parameters" type="ParameterType" maxOccurs="1" minOccurs="0" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="ParameterType">
        <xs:all>
            <xs:element ref="p" minOccurs="0"/> 
        </xs:all>
    </xs:complexType>
    ...
</xs:schema>
helderdarocha
  • 23,209
  • 4
  • 50
  • 65