2013-04-03 66 views
1

我需要創建具有特殊特徵的xsd和xml文件。例如'Plane''Model' ... 'Ammo'。如果我將'Ammo'設置爲true我需要設置Missiles(0-10)的編號。如果'Ammo'設置爲false我應該無法選擇Missiles的金額。我該如何製作這款切換臺? SXD文件的使用可選元素創建.xsd和.xml(if - then)

部分:

<xsd:complexType name="Plane"> 
    <xsd:sequence> 
     <xsd:element name="Model" type="tns:Model" /> 
     <xsd:element name="Ammunition" type="xsd:boolean" /> 
     <!-- If ammo is true add Missiles --> 
    </xsd:sequence> 
</xsd:complexType> 

<xsd:complexType name="Model"> 
    <xsd:sequence> 
     <xsd:element name="ModelType" type="xsd:string" /> 
    </xsd:sequence> 
</xsd:complexType> 

<xsd:simpleType name="Missile"> 
    <xsd:restriction base="xsd:byte"> 
     <xsd:minExclusive value="0" /> 
     <xsd:maxInclusive value="10" /> 
    </xsd:restriction> 
</xsd:simpleType> 

而且xml文件的一部分:

<Plane> 
    <Model> 
     <ModelType>MiG-29</ModelType> 
    </Model> 

    <Ammunition>true</Ammunition> 

    <!-- Set amount of missiles --> 

</Plane> 

回答

1

XSD 1.0不能單獨表達交叉領域的驗證。您需要使用Schematron擴展它,或切換到XSD 1.1,後者仍然不受歡迎。

要保持使用Ammunition標記,我會將其轉換爲複雜類型並使其成爲可選;其內容將包含您的支持彈藥,實際上會達到相同的語義。理想情況下,我會一併刪除彈藥標籤,但不會增加信息,只是開銷。

+0

這很簡單。謝謝! – rand0m86