Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml minOccurs和maxOccurs在选项中_Xml_Xsd_Choice_Minoccurs - Fatal编程技术网

Xml minOccurs和maxOccurs在选项中

Xml minOccurs和maxOccurs在选项中,xml,xsd,choice,minoccurs,Xml,Xsd,Choice,Minoccurs,我试图用minOccurs和maxOccurs限制choice元素中的元素,但它看起来不起作用 我创建了一个选项,并尝试将“person”限制为1个最大出现次数,“address”限制为1个最小出现次数,但当我尝试验证包含2个“person”出现次数和0个“address”出现次数的XML文件时,我使用的验证器(Xerces)表示该文件有效 我想做的对吗? 有没有办法强制在选项中出现元素 以下是我的XSD和XML: mySchema.xsd <?xml version="1.0" enco

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

我创建了一个选项,并尝试将“person”限制为1个最大出现次数,“address”限制为1个最小出现次数,但当我尝试验证包含2个“person”出现次数和0个“address”出现次数的XML文件时,我使用的验证器(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>

库尔蒂斯
约翰
35
库尔蒂斯
海伦娜
33
提前感谢您的回答

实际上
应该是不必要的,而
应该是不必要的

是两个人被评估为ok的原因,请尝试

中,应添加maxOccurs属性

编辑:

你可以这样做:

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

一串
一串
0
或包含许多地址元素的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 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 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>

A.
B
C
有两个人的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>
</family>
<?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 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>

一串
一串
0
一串
一串
一串
以及

<?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”元素,并且它必须是所有元素中的第一个,因为sequence强制执行此顺序

如果需要address元素可以位于person元素之前,则需要将模型更改为all。但不幸的是,在这个模型中,一个元素的出现次数不能再多了,所以应该将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: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

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

一串
一串
0
一串
一串
一串
例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>

一串
一串
一串
一串
一串
0
例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>

一串
一串
一串

这实际上只是一个例子来说明我的问题。我想要的是一个最多可以包含1人的选项,以及至少一个地址(或者相反的地址,这样会更有意义)。。。我仍然不确定你到底想做什么,但我在回答中添加了一些提示。看看其中的一些是否对您有用。感谢您提供的详细答案。我的理解是,如果我绝对希望在XML中使用“address”元素,那么我需要使用序列,而不是选择。是吗?是的,假设在该序列中定义的元素的固定顺序没有问题。