XML 架构: maxOccurs , minOccurs [英] XML-Schema : maxOccurs , minOccurs

查看:35
本文介绍了XML 架构: maxOccurs , minOccurs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行我的代码时,它给了我这个错误

When I run my code it gives me this error

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

这是我的架构:

<xs:element name="parameters" maxOccurs="1" minOccurs="0">
    <xs:complexType>
        <xs:all>
            <xs:element ref="p ?"/> 
        </xs:all>
    </xs:complexType>
</xs:element>

推荐答案

可以在顶层声明(低于 xs:schema)但它不能有 minOccursmaxOccurs 因为没有上下文就没有任何意义.如果它是根元素,则它只能有一个元素,如果不是,则该信息指的是父元素的上下文.这是合法的:

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

但这不是:

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

您可以引用到一个组中的顶级xs:element,例如xs:sequence.在这里,您可以使用这些属性,因为现在您有了一个上下文(该组中允许有多少个).这是合法的:

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>

这里的 出现的上下文,所以你可以说它允许在那里出现多少次. 的定义是全局的,您可以使用 ref 属性来引用它.

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.

如果你永远不需要重用 parameters 或者如果你永远不会将 parameters 作为 root,你不需要它在顶层并且可以嵌套它在您的 parent 定义中.在这种情况下,您可以将 name 属性与 minOccursmaxOccurs 一起使用.

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>

这篇关于XML 架构: maxOccurs , minOccurs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆