如何在 XSD 中为不同的整数类型使用条件类型分配 [英] How to use Conditional Type Assignment for different integer types in XSD

查看:22
本文介绍了如何在 XSD 中为不同的整数类型使用条件类型分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XSD1.1允许元素的类型依赖于其属性之一.例如,

XSD1.1 allows the type of an element to depend on one of its attributes. For example,

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

将导致元素"的类型为 xs:short.这是我得到的:

will cause the type of 'element' to be xs:short. Here is what I have got:

<?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-derived-ok.2:简单类型定义xs:byte"不是从GenericInt"有效派生的.

When I tried to save the file in Altova XMLSpy, an error occurred: cos-st-derived-ok.2: Simple type definition 'xs:byte' is not validly derived from 'GenericInt'.

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

So how should I correct the XSD code?

推荐答案

另一种方法类似于

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

though you wouldn't get the precise PSVI type annotation that way.

这篇关于如何在 XSD 中为不同的整数类型使用条件类型分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆