Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML-Schema : maxOccurs , minOccurs

Tags:

xml

xsd

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>
like image 363
user3445281 Avatar asked Apr 16 '14 16:04

user3445281


People also ask

What is minOccurs and maxOccurs in XML?

The minOccurs attribute specifies the minimum number of times that the element can occur. It can have a value of 0 or any positive integer. The maxOccurs attribute specifies the maximum number of times that the element can occur.

What is minOccurs 1 in XSD?

When "minOccurs=1" is contained within an XML Schema Definition (XSD), it means that the first occurrence of the element is required. The element does not need to contain any data, it just needs to be present.

What is Nillable true in XSD?

The nillable attribute can be defined on an xsd:element within an XML schema. It specifies that the xsi:nil attribute is valid for the element. If an XML schema has defined the nillable attribute as true, it is mapped as a required attribute and is included in the document, however, its values are nullified.

What is Nillable?

The nillable attribute specifies whether or not an explicit NULL value can be assigned to the element. True enables an instance of the element to have the NULL attribute set to true. The NULL attribute is defined as part of the XML Schema namespace for instances.

What is maxOccurs unbounded?

A maxOccurs attribute value of unbounded indicates that the element can appear an unlimited number of times.


1 Answers

<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>
like image 63
helderdarocha Avatar answered Nov 15 '22 18:11

helderdarocha