XSD 几个序列之间的选择

XSD choice between several sequences

我有一个 XML 文档,其结构如下:

<Values xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="test.xsd">
    <value>
        <styles>
            <style>   
                <one>12</one>
                <two>123</two>
                <three>123</three>
            </style>
            <style>
                <something>123</something>
            </style>
        </styles>
    </value>
</Values>

而且我需要为它写一个XSD模式,所以我这样写:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Values">
    <xs:complexType>
        <xs:all>
            <xs:element name="value">
                <xs:complexType>
                    <xs:all>
                        <xs:element name="styles" type="style_props"/>
                    </xs:all>
                </xs:complexType>
            </xs:element>
        </xs:all>
    </xs:complexType>
</xs:element>

<xs:complexType name="style_props">
    <xs:all>
        <xs:element name="style" type="style_elements"/>
    </xs:all>
</xs:complexType>

<xs:complexType name="style_elements">
    <xs:choice minOccurs="1" maxOccurs="1">
        <xs:group ref="first"/>
        <xs:group ref="second"/>
    </xs:choice>
</xs:complexType>

<xs:group name="first">
    <xs:sequence>
        <xs:element name="one" type="xs:integer"/>
        <xs:element name="two" type="xs:integer"/>
        <xs:element name="three" type="xs:integer"/>
    </xs:sequence>
</xs:group>

<xs:group name="second">
    <xs:sequence>
        <xs:element name="something" type="xs:integer"/>
    </xs:sequence>
</xs:group>
</xs:schema>

但是接下来会出现错误:"Invalid content was found starting with element 'style'. No child element is expected at this point".

我该如何解决?

您误解了 xs:all 的作用。如果希望 styles 元素包含零个或多个 style 元素,请写:

<xs:complexType name="style_props">
    <xs:sequence>
        <xs:element name="style" type="style_elements" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
</xs:complexType>