2014-03-19 99 views
0

我想要一個文檔只包含蘋果或桔子。我正在創建一個XML架構,如下所示:xml choice:單個元素應該重複

<element name="fruit" type="myns:fruitType"></element> 

<complexType name="fruitType"> 
    <choice minOccurs="1" maxOccurs="1"> 
     <sequence> 
     <element name="apple" type="string" minOccurs="0" maxOccurs="unbounded"></element> 
      <element name="orange" type="string" minOccurs="0" maxOccurs="0"></element> 
     </sequence> 
     <sequence > 
     <element name="orange" type="string" minOccurs="0" maxOccurs="unbounded"></element> 
      <element name="apple" type="string" minOccurs="0" maxOccurs="0"></element> 
     </sequence> 
    </choice> 
</complexType> 

但是,它接受以下內容作爲有效元素。

<fruit> 
    <apple> apple1 </apple> 
    <orange> orange1 </orange> 
    <orange> orange2 </orange> 
    <apple> apple2 </apple> 
</fruit> 

我想只有以下是有效的:

<fruit> 
    <apple> apple1 </apple> 
    <apple> apple2 </apple> 
    . 
    . 
    <apple> appleN </apple> 
</fruit> 

OR

<fruit> 
    <orange> orange1 </orange> 
    <orange> orange2 </orange> 
    . 
    . 
    <orange> orangeN </orange> 
</fruit> 

任何想法如何做到這一點?

回答

0

正確的模式應該是:

<complexType name="fruitType"> 
    <choice minOccurs="1" maxOccurs="1"> 
     <sequence> 
     <element name="apple" type="string" **minOccurs="1"** maxOccurs="unbounded"> 
     </element> 
     </sequence> 
     <sequence > 
     <element name="orange" type="string" **minOccurs="1"** maxOccurs="unbounded"> 
     </element> 
     </sequence> 
    </choice> 
</complexType> 
0
<complexType name="fruitType"> 
    <choice minOccurs="1" maxOccurs="1"> 
     <sequence> 
     <element name="apple" type="string" minOccurs="0" maxOccurs="unbounded"></element> 
       </sequence> 
     <sequence > 
     <element name="orange" type="string" minOccurs="0" maxOccurs="unbounded"></element> 
        </sequence> 
    </choice> 
</complexType> 
+0

@ user..unmarked的答案嗎? –

+0

是的,因爲您建議的模式驗證了沒有蘋果和桔子的文檔。 minOccurs約束應該是1。 – user3436238