简体   繁体   English

XML-Schema:maxOccurs,minOccurs

[英]XML-Schema : maxOccurs , minOccurs

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>

<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. <xs:element>可以在顶层声明(在xs:schema下面)但它不能有minOccursmaxOccurs因为没有上下文就没有任何意义。 If it's root it can only have one element, if it's not, that information refers to the context of the parent element. 如果它是root,它只能有一个元素,如果不是,则该信息引用父元素的上下文。 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 . 可以引用组中的顶级xs:element ,例如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. 这里<parent><parameters>发生的上下文,因此您可以说出它在那里允许的次数。 The definition of <parameters> is global and you use the ref attribute to refer to it. <parameters>的定义是全局的,您可以使用ref属性来引用它。

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. 如果您永远不需要重用parameters或者如果您永远不会以root身份获得parameters ,则不需要它在顶层,并且可以将其嵌套在parent定义中。 In this case you can use the name attribute with minOccurs and maxOccurs . 在这种情况下,您可以将name属性与minOccursmaxOccurs

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM