2009-06-17 52 views
3

我們通過MS XML 4完成了很多序列化操作。當我們序列化C++枚舉時,我們使用一個表將每個可能的值轉換爲一個字符串,並將該字符串存儲爲一個屬性值。當我們反序列化時,我們讀取該屬性值,將其與表中的所有項進行比較並檢索相應的枚舉值。如果我們沒有發現我們提出錯誤。xs:選擇相當於C++枚舉嗎?

爲了便於通過外部程序創建XML,我們爲所有感興趣的數據類型發佈了XML模式。枚舉的屬性定義如下:

<xs:complexType> 
    //other fields here 
    <xs:attribute name="Color" type="xs:string"></xs:attribute> 
</xs:complexType> 

它的工作原理,但不包含什麼是可能的字符串值的定義。我怎樣才能爲這個定義添加可能的值?我是否使用xs:這個選擇?

回答

0
<xs:complexType> 
    //other fields here 
    <xs:attribute name="Color"> 
    <xs:simpleType> 
     <xs:restriction base="xs:string"> 
     <xs:enumeration value="RED"/> 
     <xs:enumeration value="BLUE"/> 
     <xs:enumeration value="GREEN"/> 
     </xs:restriction> 
    </xs:simpleType> 
    </xs:attribute> 
</xs:complexType> 

您還可以創建作爲外部類型:

<xs:complexType> 
    //other fields here 
    <xs:attribute name="Color" type="Color"/> 
</xs:complexType> 
<xs:simpleType name="Color"> 
    <xs:restriction base="xs:string"> 
    <xs:enumeration value="RED"/> 
    <xs:enumeration value="BLUE"/> 
    <xs:enumeration value="GREEN"/> 
    </xs:restriction> 
</xs:simpleType> 

<xs:choice>意味着完全是另一回事。 XML模式中的名字不直觀,有些誤導。選擇意味着其中一個包含的元素。

+0

在實踐中雖然(雖然我同意你的看法)他們不是基本上完成同樣的事情嗎? – 2009-10-10 18:35:24

3

不,xs:choice提供的架構信息說:「在這一點上,你可以有這個或這個或這個,但不是一個組合」;你可以找到更多關於xs:choice here

要創建枚舉,您需要將它們定義爲基於xs:stringrestricted type的一部分。

例如:

<xs:simpleType name="ColorType"> 
    <xs:restriction base="xs:string"> 
    <xs:enumeration value="white"/> 
    <xs:enumeration value="black"/> 
    <xs:enumeration value="blue"/> 
    </xs:restriction> 
</xs:simpleType> 

然後,您可以使用此類型的像任何其他:

<xs:complexType> 
    <xs:attribute name="Color" type="ColorType" /> 
</xs:complexType> 

欲瞭解更多有關using xs:restriction和其他XSD元素和屬性,看看www.w3schools.com。他們有許多關於Web的許多主題的參考指南和教程,例如XHTML,XSLT,XPath和XSD(以及JavaScript和AJAX)。