我要爲源代碼(多種語言)編寫生成器。 應將類,即基本數據容器指定爲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
,和元素customType
和 variable
爲:
<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架構之後。
任何人都可以確認這是正確的嗎?