2009-04-23 48 views
17

我試圖創建一個<property>元素的架構必須具有<key>子元素,以及<val>一個,<shell><perl>和可選<os><condition>,和子元素的順序並不重要。XML架構:爲什麼<xs:all>沒有<choice>孩子?以及如何繞過?

下面是有效<property>元素的一些示例:

<property> 
    <key>A</key> 
    <val>b</val> 
</property> 

<property> 
    <key>A</key> 
    <val>b</val> 
    <os>Windows</os> 
</property> 

<property> 
    <condition>a == 1</condition> 
    <key>A</key> 
    <perl>1+1</perl> 
    <os>unix</os> 
</property> 

理想情況下,我想用<xs:all>這個的:

<xs:element name="property"> 
    <xs:complexType> 
    <xs:all> 
     <xs:element name="key" type="xs:string" /> 
     <xs:choice> 
     <xs:element name="val" type="xs:string" /> 
     <xs:element name="perl" type="xs:string" /> 
     <xs:element name="shell" type="xs:string" /> 
     </xs:choice> 
     <xs:element name="os" type="xs:string" minOccurs="0" /> 
     <xs:element name="condition" type="xs:string" minOccurs="0" /> 
    </xs:all> 
    </xs:complexType> 
</xs:element> 

但我發現<xs:all>只能包含<xs:element>而不是<xs:choice> 。有人可以解釋它爲什麼嗎?

更重要的是,有人可以提供一種方法來驗證這樣一個<property>元素?

我可以把三個要素 - <val><perl><shell> - 作爲可選的元素在<xs:all>,但我希望的模式來驗證一個且只有三個中的一個元素存在。這可以做到嗎?

+1

@splintor:在寫問題時,你是否真的看過預覽窗口?我的意思是,它的一半是隱形的... – Tomalak 2009-04-23 09:23:21

+0

你的意思是說,OS和條件可以發生在同一個屬性元素中,或者一個或另一個或兩者都不可能發生? – 2009-04-23 09:45:21

+0

@Tomalak:不 - 我錯過了預覽窗口。非常感謝編輯它意味着我想要的。 – splintor 2009-04-23 10:11:43

回答

22

我認爲這是一個好一點,作爲「選擇,」現在它自己的元素(typeFacet)但不能直接使用,因爲它是抽象的。

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="property"> 
    <xs:complexType> 
     <xs:all> 
     <xs:element name="key" type="xs:string" /> 
     <xs:element ref="typeFacet" /> 
     <xs:element name="os" type="xs:string" minOccurs="0" /> 
     <xs:element name="condition" type="xs:string" minOccurs="0" /> 
     </xs:all> 
    </xs:complexType> 
    </xs:element> 

    <xs:element name="typeFacet" abstract="true" /> 
    <xs:element name="val" type="xs:string" substitutionGroup="typeFacet" /> 
    <xs:element name="perl" type="xs:string" substitutionGroup="typeFacet" /> 
    <xs:element name="shell" type="xs:string" substitutionGroup="typeFacet" /> 
</xs:schema> 
6

基於對使用的取代基的選擇,蠑螈的評論(用xmllint測試):

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="property"> 
    <xs:complexType> 
     <xs:all> 
     <xs:element name="key" type="xs:string" /> 
     <xs:element ref="val"/> 
     <xs:element name="os" type="xs:string" minOccurs="0" /> 
     <xs:element name="condition" type="xs:string" minOccurs="0" /> 
     </xs:all> 
    </xs:complexType> 
    </xs:element> 

    <xs:element name="val" type="xs:string"/> 
    <xs:element name="perl" type="xs:string" substitutionGroup="val" /> 
    <xs:element name="shell" type="xs:string" substitutionGroup="val" /> 
</xs:schema> 
相關問題