问题
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>
回答1:
<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>
来源:https://stackoverflow.com/questions/23115318/xml-schema-maxoccurs-minoccurs