2014-02-13 95 views
0

我試圖創建此元素的XML架構...XML架構複雜類型屬性

<shoesize country="yes">35</shoesize> 

這是解決方案....

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
<xs:element name="shoesize"> 
    <xs:complexType> 
    <xs:simpleContent> 
     <xs:extension base="xs:integer"> 
     <xs:attribute name="country" type="xs:string" /> 
    </xs:extension> 
    </xs:simpleContent> 
    </xs:complexType> 
</xs:element> 
</xs:schema> 

我我試圖限制的是,該屬性只能是「是」或「否」,內容只能是小於50的整數。任何人都可以給我一些指示,請問如何做到這一點。


行,所以我做了它在單獨的文件工作,但是當我把這個代碼放到我的大架構中

<xsd:sequence> 
     <xsd:element name="something" type="xsd:string"/> 
     <xsd:element name="something else" type="xsd:string"/> 
     ...... 
     ...... 
     code above 
     .... 
     ... 
</xsd:sequence> 

我得到錯誤

s4s-elt-must-match.1: The content of 'sequence' must match (annotation?, (element | group | choice | sequence | any)*). 

回答

1

你必須這樣做在兩個階段中,首先定義一個名爲頂級simpleType來限制內容(將所有現有的xs:element聲明,直接在xs:schema之外)

<xs:simpleType name="lessThanFifty"> 
    <xs:restriction base="xs:integer"> 
    <xs:maxExclusive value="50" /> 
    </xs:restriction> 
</xs:simpleType> 

然後讓你的complexType擴展,以添加屬性

<xs:element name="shoesize"> 
<xs:complexType> 
    <xs:simpleContent> 
    <xs:extension base="lessThanFifty"> 
    <xs:attribute name="country"> 
    <!-- you might want to pull this out into a top-level type if you 
      have other yes/no attributes elsewhere in the schema --> 
    <xs:simpleType> 
     <xs:restriction base="xs:string"> 
     <xs:enumeration value="yes" /> 
     <xs:enumeration value="no" /> 
     </xs:restriction> 
    </xs:simpleType> 
    </xs:attribute> 
    </xs:extension> 
    </xs:simpleContent> 
</xs:complexType> 
</xs:element> 

這將允許任何整數值直到幷包括49,所以-500是一個有效的值。開始限制從xs:nonNegativeInteger而不是xs:integer開始可能更合適。

+0

當我嘗試驗證它時,我得到2個字符... s4s-att-must-appear:屬性'name'必須出現在元素'complexType'中。 和cvc-elt.1.a:找不到元素'shoesize'的聲明。 – Stribor

+0

實際上我解決了它..謝謝 – Stribor

+0

@Stribor編輯,使其更清晰,命名的'simpleType'必須在模式的頂層,而不是在您現有的'序列'裏面。 –

相關問題