如何使用可变元素列表制作XSD(参见说明)

时间:2015-02-20 09:50:14

标签: xml xsd

我需要创建一个xsd来验证XML,该XML具有包含私有和企业关联的列表。 我的XSD看起来像这样:

<xsd:element name="PartnerAssociationList" type="PartnerAssociationList_t"/>

        <xsd:complexType name="PartnerAssociationList_t">
            <xsd:sequence>
                <xsd:choice>
                    <xsd:element name="PartnerPrivateAssociation" type="PartnerPrivateAssociation_t" maxOccurs="unbounded"/>
                    <xsd:element name="PartnerRetailAssociation" type="PartnerRetailAssociation_t" maxOccurs="unbounded"/>
                </xsd:choice>
            </xsd:sequence>
        </xsd:complexType>

显然,这段代码只允许在XML中出现一种类型的关联,而我需要两者。我知道我可以做这样的事情

<xsd:complexType name="PartnerAssociationList_t">
                <xsd:sequence>
                    <xsd:sequence>
                        <xsd:element name="Association" type="PartnerAssociation_t" maxOccurs="unbounded"/>
                    </xsd:sequence`enter code here`>
                </xsd:sequence>
            </xsd:complexType>

<xsd:complexType name="PartnerAssociation_t">
            <xsd:sequence>
                <xsd:choice>
                    <xsd:element name="PartnerPrivateAssociation" type="PartnerPrivateAssociation_t"/>
                    <xsd:element name="PartnerRetailAssociation" type="PartnerRetailAssociation_t"/>
                </xsd:choice>
            </xsd:sequence>
        </xsd:complexType>

但是我不能在XML中使用那个额外的“Association”元素。

1 个答案:

答案 0 :(得分:1)

如果您想支持私人和零售协会的任何组合,那么您只需删除sequence并使choice无限制:

<xsd:complexType name="PartnerAssociationList_t">
  <xsd:choice maxOccurs="unbounded">
    <xsd:element name="PartnerPrivateAssociation" type="PartnerPrivateAssociation_t"/>
    <xsd:element name="PartnerRetailAssociation" type="PartnerRetailAssociation_t"/>
  </xsd:choice>
</xsd:complexType>

您的版本允许在无限数量的私人协会或无限数量的零售协会之间进行单一选择,而我的版本允许无限数量的“商品”,其中每件商品都是私人或零售。