2013-10-22 55 views
1

我有以下XSD:重新使用XSD复杂类型

<element name="OrderElement" type="tns:OrderType"></element> 

<complexType name="OrderType"> 
    <sequence> 
     <element name="Name" type="tns:NameType"></element> 
     <element name="Address" type="tns:AddressType"></element> 
    </sequence> 
</complexType> 

<complexType name="NameType"> 
    <sequence> 
     <element name="FirstName" minOccurs="1" maxOccurs="1" type="string"> 
     </element> 
     <element name="Surname" minOccurs="1" maxOccurs="1" type="string"> 
     </element> 
    </sequence> 
</complexType> 

<complexType name="AddressType"> 
    <sequence> 
     <element name="AddressLine1" minOccurs="1" maxOccurs="1" 
      type="string"> 
     </element> 
     <element name="AddressLine2" minOccurs="1" maxOccurs="1" 
      type="string"> 
     </element> 
     <element name="Country" minOccurs="1" maxOccurs="1" 
      type="tns:CountriesDeliveryType"> 
     </element> 
    </sequence> 
</complexType> 

<complexType name="CountriesDeliveryType"> 
    <choice minOccurs="1"> 
     <element name="USA" type="string" maxOccurs="1" minOccurs="0"></element> 
     <element name="Brazil" type="string" maxOccurs="1" minOccurs="0"></element> 
     <element name="China" type="string" maxOccurs="1" minOccurs="0"></element> 
    </choice> 
</complexType> 

我想要做的是有一个复杂的类型,这样我可以重新使用个国家代码。我遇到的问题是,在XML中它证明,当我删除的国家:

<?xml version="1.0" encoding="UTF-8"?> 
<tns:OrderElement xmlns:tns="http://www.example.org/NewXMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/NewXMLSchema NewXMLSchema.xsd "> 
    <tns:Name> 
    <tns:FirstName>tns:FirstName</tns:FirstName> 
    <tns:Surname>tns:Surname</tns:Surname> 
    </tns:Name> 
    <tns:Address> 
    <tns:AddressLine1>tns:AddressLine1</tns:AddressLine1> 
    <tns:AddressLine2>tns:AddressLine2</tns:AddressLine2> 
    <tns:Country> 
     <tns:USA>tns:USA</tns:USA> 
    </tns:Country> 
    </tns:Address> 
</tns:OrderElement> 

即如果我删除 <tns:USA>tns:USA</tns:USA> 它仍然验证。

我一直在寻找这个多年,但还没有遇到过解决方案。这可能很简单,但我似乎无法解决。有什么建议么?

回答

1

这一切都在您的minOccurs="0"。由于这个原因,所有国家都不能同时设置。修正看起来像

<complexType name="CountriesDeliveryType"> 
    <choice minOccurs="1" maxOccurs="3"> 
     <element name="USA" type="string" maxOccurs="1" minOccurs="1"></element> 
     <element name="Brazil" type="string" maxOccurs="1" minOccurs="1"></element> 
     <element name="China" type="string" maxOccurs="1" minOccurs="1"></element> 
    </choice> 
</complexType> 

虽然,仍然可能有两个<USA>元素。

+0

太好了,就是这样。非常感谢@ polkovnikov.ph。 – Michael

+0

正如你所说重复元素是现在唯一的问题。如果我找到答案,我会在这里发布。 – Michael

+1

如果你没有一个很大的国家名单,你可以列举所有可能的情况。让我们把序列写成逗号,选择bar,'minoccurs =“0”'作为问号。那么你需要......(美国,(巴西?中国?中国?巴西?)) (巴西,(美国?中国?中国?美国?)) (中国,(巴西?美国?|美国?巴西?))' –