实际上<xs:element name="person" maxOccurs="1">
应该是不必要的,<xs:element name="person">
应该是足够的。
<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 中同时包含人员和地址元素,您应该将选择更改为如下所示的顺序
<?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 个“人”元素,并且它必须是所有元素的第一个,因为序列强制执行此顺序。
如果您需要 address 元素可以在 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>