2014-09-29 23 views
0

XSD1.1允許元素的類型取決於其某個屬性。例如,如何爲XSD中的不同整數類型使用條件類型分配

<integer kind='s'>100</integer> 

將導致'元素'的類型爲xs:short。以下是我有:

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
      xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" 
      elementFormDefault="qualified" 
      attributeFormDefault="unqualified" 
      vc:minVersion="1.1"> 
    <xs:complexType name="GenericInt"> 
     <xs:simpleContent> 
     <xs:extension base="xs:integer"> 
      <xs:attribute name="kind" type="xs:string"/> 
     </xs:extension> 
     </xs:simpleContent> 
    </xs:complexType> 
    <xs:element name="integer" type="GenericInt"> 
     <xs:alternative test="@kind='b'" type="xs:byte"/> 
     <xs:alternative test="@kind='s'" type="xs:short"/> 
     <xs:alternative test="@kind='i'" type="xs:int"/> 
     <xs:alternative test="@kind='l'" type="xs:long"/> 
     <xs:alternative     type="GenericInt"/> 
    </xs:element> 
</xs:schema> 

當我試圖把文件保存在Altova的XMLSpy的,發生了錯誤: COS-ST-派生ok.2:簡單類型定義「XS:字節」不是有效地從'GenericInt'派生。

那麼我該如何糾正XSD代碼呢?

回答

0

好吧,我得到它的工作是這樣的:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
      xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" 
      elementFormDefault="qualified" 
      attributeFormDefault="unqualified" 
      vc:minVersion="1.1"> 
    <xs:complexType name="bInt"> 
     <xs:simpleContent> 
     <xs:extension base="xs:byte"> 
      <xs:attribute name="kind" type="xs:string"/> 
     </xs:extension> 
     </xs:simpleContent> 
    </xs:complexType> 
    <xs:complexType name="sInt"> 
     <xs:simpleContent> 
     <xs:extension base="xs:short"> 
      <xs:attribute name="kind" type="xs:string"/> 
     </xs:extension> 
     </xs:simpleContent> 
    </xs:complexType> 
    <xs:complexType name="iInt"> 
     <xs:simpleContent> 
     <xs:extension base="xs:int"> 
      <xs:attribute name="kind" type="xs:string"/> 
     </xs:extension> 
     </xs:simpleContent> 
    </xs:complexType> 
    <xs:complexType name="lInt"> 
     <xs:simpleContent> 
     <xs:extension base="xs:long"> 
      <xs:attribute name="kind" type="xs:string"/> 
     </xs:extension> 
     </xs:simpleContent> 
    </xs:complexType> 
    <xs:complexType name="gInt"> 
     <xs:simpleContent> 
     <xs:extension base="xs:integer"> 
      <xs:attribute name="kind" type="xs:string"/> 
     </xs:extension> 
     </xs:simpleContent> 
    </xs:complexType> 
    <xs:element name="integer"> 
     <xs:alternative test="@kind='b'" type="bInt"/> 
     <xs:alternative test="@kind='s'" type="sInt"/> 
     <xs:alternative test="@kind='i'" type="iInt"/> 
     <xs:alternative test="@kind='l'" type="lInt"/> 
     <xs:alternative     type="gInt"/> 
    </xs:element> 
</xs:schema> 

任何更好的解決方案?

+0

我的直覺是將各種複雜類型sInt等定義爲複雜類型GenericInt的限制,但它仍然會非常冗長,因爲始終限制複雜類型。 – 2014-09-29 11:29:09

2

一種替代的辦法是像

<xs:complexType name="GenericInt"> 
     <xs:simpleContent> 
     <xs:extension base="xs:integer"> 
      <xs:attribute name="kind" type="xs:string"/> 
     </xs:extension> 
     </xs:simpleContent> 
     <xs:assert test="(@kind='b' and $value = (-128 to 127)) 
         or (@kind='s' and $value = (-32768 to 32767)) 
         or (....)"/> 
    </xs:complexType> 

雖然你不會得到精確的PSVI類型註釋的方式。

相關問題