2015-04-04 28 views
1

XML有一個父元素parent,它必須包含10個子元素:child1child10,但是隻有一次。如何編寫XML模式以允許/強制元素以任意順序發生一次?

作爲替代方案,所述parent元件可任選地含有child11child20代替child1child10

<parent><child1/><child4/> ... <child2/></parent> OK 
<parent><child15/>...<child20/></parent> OK 
<parent><child1/><child2/></parent> BAD, missing childs 
<parent><child1/>...<child11/></parent> BAD: child11 shall never be with child1 
<parent><child1/><child1/></parent> BAD: childs shall not be repeated 

這是迄今爲止我的架構定義的嘗試:

<xs:complexType name="parent"> 
    <xs:choice> 
     <xs:sequence> 
      <xs:element name="child1" type="xs:integer" minOccurs="1" maxOccurs="1"/> 
      <xs:element name="child2" type="xs:integer" minOccurs="1" maxOccurs="1"/> 
      ... 
     </xs:sequence> 
     <xs:sequence> 
      <xs:element name="child11" type="xs:integer" minOccurs="1" maxOccurs="1"/> 
      <xs:element name="child12" type="xs:integer" minOccurs="1" maxOccurs="1"/> 
      ... 
     </xs:sequence> 
    </xs:choice> 
</xs:complexType> 
+1

爲什麼'child1'不能與'child11'(實施例4)然而'child5'可以用'child20'(例子2)? – kjhughes 2015-04-04 19:11:51

+0

@kjhughes好點我沒有注意到,我認爲這是一個錯字 – softwariness 2015-04-04 19:23:51

+0

是的,很抱歉,它現在解決了 – 2015-04-04 22:27:35

回答

2

XML模式1.0沒有直接解決您的問題。

你想要什麼,是以下(更換xs:sequencexs:all),但這不是有效的XML Schema語言

<xs:complexType name="parent"> 
    <xs:choice> 
     <xs:all> 
      <xs:element name="child1" type="xs:integer" minOccurs="1" maxOccurs="1"/> 
      <xs:element name="child2" type="xs:integer" minOccurs="1" maxOccurs="1"/> 
      <!-- ... --> 
     </xs:all> 
     <xs:all> 
      <xs:element name="child11" type="xs:integer" minOccurs="1" maxOccurs="1"/> 
      <xs:element name="child12" type="xs:integer" minOccurs="1" maxOccurs="1"/> 
      <!-- ... --> 
     </xs:all> 
    </xs:choice> 
</xs:complexType> 

的問題是,xs:choice允許xs:sequence嵌套和xs:choice合成器,但不是xs:all

有五種可能性我看:

  1. 使用模式在你的問題,它採用xs:sequence代替xs:all,但使用XSL轉換驗證之前重新排序按照模式。
  2. 如果XML Schema 1.1是一個選項,您可以定義一個更寬鬆的模式(即不強制執行所有規則但接受所有有效輸入的模式),但可以使用xs:assert進行額外的檢查。
  3. 或者定義一個更寬鬆的模式,但在模式驗證步驟之後,在自己的程序中執行額外的檢查。
  4. 重構模式,以便兩組相關元素嵌套在另一個元素下,以便您可以使用xs:choice
  5. 將抽象類型定義爲parent,爲內容模型的兩種選擇定義派生類型,並使用xsi:type指定在XML文檔中使用哪一種。

後兩種選擇都需要不幸地改變XML格式。


這是你的架構會如何看,如果你可以添加一個額外級別的元素嵌套:

<xs:complexType name="option1"> 
    <xs:all> 
     <xs:element name="child1" type="xs:integer" minOccurs="1" maxOccurs="1"/> 
     <xs:element name="child2" type="xs:integer" minOccurs="1" maxOccurs="1"/> 
     <!-- ... --> 
    </xs:all> 
</xs:complexType> 

<xs:complexType name="option2"> 
    <xs:all> 
     <xs:element name="child11" type="xs:integer" minOccurs="1" maxOccurs="1"/> 
     <xs:element name="child12" type="xs:integer" minOccurs="1" maxOccurs="1"/> 
     <!-- ... --> 
    </xs:all> 
</xs:complexType> 

<xs:complexType name="parent"> 
    <xs:choice> 
     <xs:element name="option1" type="option1"/> 
     <xs:element name="option2" type="option2"/> 
    </xs:choice> 
</xs:complexType> 

這是如果你使用類型替換您的架構可能的樣子:

<xs:complexType name="parent" abstract="true"> 
</xs:complexType> 

<xs:complexType name="parent_option1"> 
    <xs:complexContent> 
     <xs:extension base="parent"> 
      <xs:all> 
       <xs:element name="child1" type="xs:integer" minOccurs="1" maxOccurs="1"/> 
       <xs:element name="child2" type="xs:integer" minOccurs="1" maxOccurs="1"/> 
       <!-- ... --> 
      </xs:all> 
     </xs:extension> 
    </xs:complexContent> 
</xs:complexType> 

<xs:complexType name="parent_option2"> 
    <xs:complexContent> 
     <xs:extension base="parent"> 
      <xs:all> 
       <xs:element name="child11" type="xs:integer" minOccurs="1" maxOccurs="1"/> 
       <xs:element name="child12" type="xs:integer" minOccurs="1" maxOccurs="1"/> 
       <!-- ... --> 
      </xs:all> 
     </xs:extension> 
    </xs:complexContent> 
</xs:complexType> 

<xs:element name="parent" type="parent"/> 

使用此方法的XML實例文檔看起來像下面的第一組el對此語句(請注意使用的xsi:type指定哪些組已經被選中):

<parent 
    xmlns="Your namespace here" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:type="parent_option1"> 

    <child2>2</child2> 
    <child1>1</child1> 
    <!-- ... --> 

</parent> 

而對於第二組:

<parent 
    xmlns="Your namespace here" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:type="parent_option2"> 

    <child11>11</child11> 
    <child12>12</child12> 
    <!-- ... --> 

</parent> 
相關問題