1

I would like to validate this XML:

<meta>
    <house>
        <big ... />
        <little ... />
        <big ... />
    </house>

    <flat>
        <red ... />
        <red ... />
        <yellow ... />
    </flat>
</meta>

I wrote that.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="meta">
        <xs:complexType>
            <xs:sequence minOccurs="0" maxOccurs="unbounded">
                <xs:element name="house">
                    <xs:complexType>
                        <xs:sequence minOccurs="0" maxOccurs="unbounded">
                            <xs:element name='big' />
                            <xs:element name='little' />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="flat">
                    <xs:complexType>
                        <xs:sequence minOccurs="0" maxOccurs="unbounded">
                            <xs:element name='red'/>
                            <xs:element name='yellow'/>
                        </xs:sequence>
                     </xs:complexType>
                 </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

But that does not validate my example. Without 'house' or 'flat', and only meta, that worked.

Where could be my problem ?

4

1 回答 1

0

成立 !

解决方案:为每个“xs:sequence”添加一个“xs:choice”,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="meta">
        <xs:complexType>
            <xs:sequence minOccurs="0" maxOccurs="unbounded">
               <xs:choice>
                <xs:element name="house">
                    <xs:complexType>
                        <xs:sequence minOccurs="0" maxOccurs="unbounded">
                            <xs:choice>
                                <xs:element name='big' />
                                <xs:element name='little' />
                            </xs:choice>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="flat">
                    <xs:complexType>
                        <xs:sequence minOccurs="0" maxOccurs="unbounded">
                            <xs:choice>
                                <xs:element name='red'/>
                                <xs:element name='yellow'/>
                            </xs:choice>
                        </xs:sequence>
                     </xs:complexType>
                 </xs:element>
               </xs:choice>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
于 2011-09-27T13:46:52.290 回答