2013-07-15 31 views
0

我要爲源代碼(多種語言)編寫生成器。 應將類,即基本數據容器指定爲XML 文件。要自動驗證和解析這些XML文件,我需要定義一個XSD架構。這應該是一個有效的文件:自定義類型在XML模式中作爲限制的定義

<?xml version="1.0"?> 
<class> 
    <customType name="vector3D"> 
     <variable name="x" type="int"/> 
     <variable name="y" type="int"/> 
     <variable name="z" type="int"/> 
    </customType> 
    <variable name="identifier" type="string"/> 
    <variable name="direction" type="vector3D"/> 
</class> 

我定義我的根元素class,和元素customTypevariable爲:

<xsd:complexType name="Class"> 
    <xsd:sequence> 
     <xsd:element name="customType" type="CustomType" 
      minOccurs="0" maxOccurs="unbounded"/> 
     <xsd:element name="variable" type="Variable" 
      minOccurs="0" maxOccurs="unbounded"/> 
    </xsd:sequence> 
<xsd:complexType> 

<xsd:complexType name="CustomType"> 
    <xsd:sequence> 
     <xsd:element name="variable" type="Variable" 
      minOccurs="0" maxOccurs="unbounded"/> 
    </xsd:sequence> 
    <xsd:attribute name="name" type="xsd:string" 
     use="required"/> 
</xsd:complexType> 

<xsd:complexType name="Variable"> 
    <xsd:attribute name="name" type="xsd:string" 
     use="required"/> 
    <xsd:attribute name="type" type="ValidType" 
     use="required"/> 
</xsd:complexType> 

不過,我掙扎着大量試圖讓有限的一組 基本類型名稱中定義的customType標籤。定義 我的基本類型集很容易:

<xsd:simpleType name="ValidType"> 
    <xsd:restriction base="xsd:string"> 
     <xsd:enumeration value="bool"/> 
     <xsd:enumeration value="int"/> 
     <xsd:enumeration value="string"/> 
    </xsd:restriction> 
</xsd:simpleType> 

但有什麼辦法,我也可以允許在name 屬性customType標籤定義的標識符,或者我必須允許任何 xsd:string和檢查有效性在我的發電機內?

編輯

如果我理解正確的3.16.2 of the W3C XML Schema Definition Language (XSD) Recommendation,我想不能用XSD來完成(因爲限制僅限於 minExclusive | minInclusive | maxExclusive | maxInclusive | totalDigits | fractionDigits | length | minLength | maxLength | enumeration | whiteSpace | pattern | assertion | explicitTimezone,不支持這種動態的限制),而我所要做的這在手動驗證XSD架構之後。

任何人都可以確認這是正確的嗎?

回答

3

是的我認爲你是對的。在XSD 1.0,不可能做出那樣:「如果屬性等於XX然後在屬性可以僅等於ZZ」動態限制。在xsd 1.1中有定義斷言的可能性,但我不確定在可用解析器中支持多少(可能撒克遜可能具有此功能)。