minOccurs和maxOccurs内部选择

时间:2013-10-14 13:37:58

标签: xml xsd choice minoccurs

我试图用minOccurs和maxOccurs限制 choice 元素中的元素,但它看起来不像它正在工作。

我创造了一个选择并试图限制" person"至少发生1次,"地址"到1出现最小值,但是当我尝试验证一个包含2个出现的" person"和#34;地址"出现,我使用的验证器(Xerces)说它有效。

我想做的是正确的吗? 有没有办法强制在选择中出现一个元素?

以下是我的XSD和XML:

mySchema.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="family">
    <xs:complexType>
        <xs:choice maxOccurs="unbounded">
            <xs:element name="person" maxOccurs="1">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="name" type="xs:string"/>
                        <xs:element name="firstname" type="xs:string"/>
                        <xs:element name="age" type="xs:int"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="address" type="xs:string" minOccurs="1"/> 
        </xs:choice>
    </xs:complexType>
</xs:element>

instance.xml

<?xml version="1.0" encoding="UTF-8"?>
<family>
    <person>
        <name> Kurtis </name>
        <firstname> John </firstname>
        <age> 35 </age>
    </person>
    <person>
        <name> Kurtis </name>
        <firstname> Helena </firstname>
        <age> 33 </age>
    </person>
</family>

提前感谢您的回答!

1 个答案:

答案 0 :(得分:3)

实际上<xs:element name="person" maxOccurs="1">应该是不必要的,<xs:element name="person">应该是enougn。

<xs:choice maxOccurs="unbounded">是两个人被评价为好的原因,只需尝试<xs:choice>

<xs:element name="address" type="xs:string" minOccurs="1"/>中,您应该添加maxOccurs属性<xs:element name="address" type="xs:string" minOccurs="1" maxOccurs="unbounded" />

编辑:

你可以做这样的事情:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="family">
        <xs:complexType>
            <!-- Choice between one "person" element and one or more "address" element. They cannot occur simultaneously because of choice -->
            <xs:choice>
                <xs:element name="person">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="name" type="xs:string"/>
                            <xs:element name="firstname" type="xs:string"/>
                            <xs:element name="age" type="xs:int"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="address" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
            </xs:choice>
        </xs:complexType>
    </xs:element>
</xs:schema>

在这种情况下,一个人的xml将验证

<?xml version="1.0" encoding="UTF-8"?>
<family xsi:noNamespaceSchemaLocation="Untitled4.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <person>
        <name>String</name>
        <firstname>String</firstname>
        <age>0</age>
    </person>
</family>

或包含许多地址元素的xml将验证

<?xml version="1.0" encoding="UTF-8"?>
<family xsi:noNamespaceSchemaLocation="Untitled4.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <address>a</address>
    <address>b</address>
    <address>c</address>
</family>

带有两个人的XML不会像一个人和一些地址元素那样验证XML(因为选择结构)。

如果您需要在一个XML中同时包含person和address元素,则应将选项更改为序列,如下所示

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="family">
        <xs:complexType>
            <xs:sequence>
                <!-- Only 0 or 1 "person element might appear -->
                <xs:element name="person" minOccurs="0">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="name" type="xs:string"/>
                            <xs:element name="firstname" type="xs:string"/>
                            <xs:element name="age" type="xs:int"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <!-- followed by many "address" elements -->
                <xs:element name="address" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

所以,例如以下xml将验证

<?xml version="1.0" encoding="UTF-8"?>
<family xsi:noNamespaceSchemaLocation="Untitled4.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <person>
        <name>String</name>
        <firstname>String</firstname>
        <age>0</age>
    </person>
    <address>String</address>
    <address>String</address>
    <address>String</address>
</family>

以及

<?xml version="1.0" encoding="UTF-8"?>
<family xsi:noNamespaceSchemaLocation="Untitled4.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <address>String</address>
    <address>String</address>
    <address>String</address>
</family>

我之前的例子中可能只有0或1个“person”元素,并且它必须是所有元素中的第一个,因为序列强制执行此顺序。

如果您需要地址元素可以在person元素之前,则需要将模型更改为all。但不幸的是,在这个模型中你不能有更多的元素出现,所以你应该将地址元素“包装”到另一个元素中,如

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="family">
        <xs:complexType>
            <xs:all>
                <!-- Only 0 or 1 "person element might appear -->
                <xs:element name="person" minOccurs="0">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="name" type="xs:string"/>
                            <xs:element name="firstname" type="xs:string"/>
                            <xs:element name="age" type="xs:int"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <!-- or many "address" elements packed into "addresses" element-->
                <xs:element name="addresses">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="address" type="xs:string" maxOccurs="unbounded"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:all>
        </xs:complexType>
    </xs:element>
</xs:schema>

在这种情况下,以下XML将验证

示例1

<?xml version="1.0" encoding="UTF-8"?>
<family xsi:noNamespaceSchemaLocation="Untitled4.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <person>
        <name>String</name>
        <firstname>String</firstname>
        <age>0</age>
    </person>
    <addresses>
        <address>String</address>
        <address>String</address>
        <address>String</address>
    </addresses>
</family>

示例2

<?xml version="1.0" encoding="UTF-8"?>
<family xsi:noNamespaceSchemaLocation="Untitled4.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <addresses>
        <address>String</address>
        <address>String</address>
        <address>String</address>
    </addresses>
    <person>
        <name>String</name>
        <firstname>String</firstname>
        <age>0</age>
    </person>
</family>

示例3

<?xml version="1.0" encoding="UTF-8"?>
<family xsi:noNamespaceSchemaLocation="Untitled4.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <addresses>
        <address>String</address>
        <address>String</address>
        <address>String</address>
    </addresses>
</family>