2016-04-03 47 views
2

我想對xsd:complexType進行限制,只包含0.520之間的值,包括兩者,步驟爲0.5。這意味着數字:XSD允許一半加倍

0.5 1 1.5 2 2.5 3 3.5 .. up to 20 

我的代碼是在這裏:

<xsd:complexType name="skillType"> 
    <xsd:simpleContent> 
     <xsd:extension base="skillHalfDoubleType"> 
      <xsd:attribute name="special"> 
       <!-- Irrelevant attribute --> 
      </xsd:attribute> 
     </xsd:extension> 
    </xsd:simpleContent> 
</xsd:complexType> 

<xsd:simpleType name="skillHalfDoubleType"> 
    <xsd:restriction base="xsd:double"> 
     <!-- What more? --> 
    </xsd:restriction> 
</xsd:simpleType> 

這是可能的應用上xsd:string正則表達式來做到這一點,但我期待與限制xsd:double的解決方案。

回答

2

XML模式1.1,你可以編寫限制值

<xs:simpleType name="skillHalfDoubleType"> 
    <xs:restriction base="xs:double"> 
     <xs:assertion test="$value = (for $d in 1 to 40 return 0.5 * $d)"></xs:assertion> 
    </xs:restriction> 
</xs:simpleType> 
+2

或者您可以有minInclusive方面,maxInclusive方面和斷言方面round($ value * 2)= $ value * 2 '來測試它是0.5的倍數。 –

3

一個簡單的(雖然詳細)的解決辦法是使用一個枚舉:

<xs:enumerationvalue="0.5"/> 
<xs:enumerationvalue="1"/> 
<xs:enumerationvalue="1.5"/> 
... 

更好的解決方案將是使用一個圖案分鐘並maxInclusive,便會結合:

<xs:minInclusivevalue="0"/> 
<xs:maxInclusivevalue="20"/> 
<xs:pattern value="([1-9]?[0-9])|([1-9]?[0-9].5)"/> 

後者如CM Sperberg-McQueen所指出的,允許更容易的範圍適應,但可能會混淆一些架構意識的建議提供者。

+0

是,相當簡單明瞭的方式斷言。這是可用的,直到我改變主意並要求從'0.5'到'100'。然而,+1解決方案:) –

+0

是的,一種模式也可以工作,雖然架構感知軟件可以生成建議,但它們往往會發現比枚舉更難處理的模式。如果模式的限制性較小(例如'。*(\。[05] 0 *'),則可以避免多說明意圖; OP沒有提及禁止前導符號或前導和尾隨零的任何要求。 –

+0

感謝您的確認,我更新了答案。 –