Xml 如何在XSD中为不同的整数类型使用条件类型赋值

Xml 如何在XSD中为不同的整数类型使用条件类型赋值,xml,xsd,xsd-1.1,Xml,Xsd,Xsd 1.1,允许元素的类型取决于其属性之一。比如说, <integer kind='s'>100</integer> 100 将导致“元素”的类型为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/XM

允许元素的类型取决于其属性之一。比如说,

<integer kind='s'>100</integer>
100
将导致“元素”的类型为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派生正常。2:简单类型定义“xs:byte”不是从“GenericInt”有效派生的

那么我应该如何更正XSD代码呢?

嗯,我是这样工作的:

<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>


有更好的解决方案吗?

另一种方法是

    <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类型注释。

我的直觉是将各种复杂类型sInt等定义为复杂类型泛型的限制,但它仍然非常冗长,因为限制复杂类型总是如此。