2015-04-07 98 views
2

假設我有一個XML元素,food,可以採取兩種形式之一:XSD允許屬性只依賴於其他屬性值

<food kind="juice" /> 
<food kind="fruit" sort="apple" /> 

在我的XSD我想說明的是,sort屬性可以只存在於<food>元素iff kind="fruit"。其他kind值的屬性sort不可接受。它看起來像一個simple attribute implication可能工作,但我無法找到更多細節。

我該如何指定這樣的依賴關係?

回答

1

你可以做到這一點使用XSD 1.1的Conditional Type Assignment:但是

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
      xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" 
      elementFormDefault="qualified" 
      vc:minVersion="1.1"> 
    <xs:element name="food"> 
    <xs:alternative test="@kind = 'juice'" type="JuiceType"/> 
    <xs:alternative test="@kind = 'fruit'" type="FruitType"/> 
    </xs:element> 
    <xs:complexType name="JuiceType"> 
    <xs:sequence> 
     <!-- ... --> 
    </xs:sequence> 
    </xs:complexType> 
    <xs:complexType name="FruitType"> 
    <xs:sequence> 
     <!-- ... --> 
    </xs:sequence> 
    <xs:attribute name="sort"/> 
    </xs:complexType> 
</xs:schema> 

再做考慮,另一種設計是在兩個XSD 1.1表達和1.0這是一般可取:

<juice/> 
<fruit sort="apple"/> 

也就是說,不是具有kind屬性,而是使用元素名稱來傳達類型。