xml XSD:声明 xs:integer 数据类型时允许空值的选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4315258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:24:06  来源:igfitidea点击:

XSD: Options for allowing null values when declaring xs:integer data types

xmlschemaxsd

提问by johkar

My problem (or question) centers around empty elements which are typed as xs:integer. I need to allow for empty elements so I used a union to allow an empty element or a valid integer as the value as shown in the schema below. However, my schema serves a dual role and also needs to be imported into 3rd party software which expects data types of String, Float, Integer or Date. If I code the schema using the union method for all integers they will not be typed as integers in the software. Is there another way other than the union method of allowing an empty element for integer data types? I'd like to just have the one XSD but can have two if that is what needs to happen.

我的问题(或问题)集中在输入为 xs:integer 的空元素上。我需要允许空元素,所以我使用联合来允许空元素或有效整数作为值,如下面的架构所示。但是,我的架构具有双重作用,还需要导入到需要字符串、浮点数、整数或日期数据类型的 3rd 方软件中。如果我对所有整数使用 union 方法对模式进行编码,它们将不会在软件中输入为整数。除了允许整数数据类型为空元素的联合方法之外,还有其他方法吗?我只想拥有一个 XSD,但如果需要的话可以拥有两个。

Given XML sample of:

给定的 XML 示例:

<?xml version="1.0" encoding="UTF-8"?>
<company>
    <division>
        <department>
            <roles/>
            <employees>7</employees>
        </department>
    </division>
</company>

And schema of:

和架构:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:simpleType name="nullval">
        <xs:union memberTypes="IntegerType empty"/>
    </xs:simpleType>
    <xs:simpleType name="IntegerType">
        <xs:restriction base="xs:integer"/>
    </xs:simpleType>
    <xs:simpleType name="empty">
        <xs:restriction base="xs:string">
            <xs:maxLength value="0"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:element name="company">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="division">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="department" minOccurs="0" maxOccurs="unbounded">
                                <xs:complexType>
                                    <!-- elements may appear in any order -->
                                    <xs:all minOccurs="0" maxOccurs="1">
                                        <xs:element name="roles" type="nullval"/>
                                        <xs:element name="employees" type="xs:integer"/>
                                    </xs:all>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

回答by sho222

Have you tried

你有没有尝试过

<xs:element name="roles" type="xs:integer" nillable="true"/>

回答by Ahmad Hindash

What you have to do is to assign two restrictions on the same element, plus make a union of them such as in the following example:

您需要做的是为同一个元素分配两个限制,然后将它们合并,如下例所示:

<xs:element name='job_code'>
  <xs:simpleType>
    <xs:union>
      <xs:simpleType>
        <xs:restriction base='xs:string'>
          <xs:length value='0'/>
        </xs:restriction>
      </xs:simpleType>
      <xs:simpleType>
        <xs:restriction base='xs:integer'>
        </xs:restriction>
      </xs:simpleType>
    </xs:union>
  </xs:simpleType>
</xs:element>

Using this restriction, you tell the XML validation to allow any integer value and allowing the element if it is empty.

使用此限制,您可以告诉 XML 验证允许任何整数值,如果元素为空则允许该元素。

回答by user2198825

hi the nillable="true" and minOccurs="0" works only when you dont send the tag at all. If however you need to pass an empty value inside the tags i think you have to implement a union.

嗨 nillable="true" 和 minOccurs="0" 仅在您根本不发送标签时才有效。但是,如果您需要在标签内传递一个空值,我认为您必须实现一个联合。

回答by Menol

I had the same requirement today. Following XSD only allows empty, or any value between -1 and 999

我今天也有同样的要求。跟随 XSD 只允许空值,或 -1 和 999 之间的任何值

I am extracting only the required stuff from a very large XSD so some of these might look like overkill.

我只从一个非常大的 XSD 中提取所需的东西,所以其中一些可能看起来有点矫枉过正。

<xs:simpleType name="emptyString">
    <xs:restriction base="xs:string">
    <xs:enumeration value=""/>
    </xs:restriction>
</xs:simpleType>


<xs:simpleType name="int-1999">
 <xs:restriction base="xs:int">
 <xs:minInclusive value="-1"/>
 <xs:maxInclusive value="999"/>
 </xs:restriction>
</xs:simpleType>


<xs:element name="preNotificationPeriod" nillable="true">
  <xs:simpleType>
  <xs:union memberTypes="int-1999 emptyString"/>
  </xs:simpleType>
</xs:element>

Reference - http://www.ilearnttoday.com/xsd-empty-values-and-range-restriction-for-numeric-types

参考 - http://www.ilearnttoday.com/xsd-empty-values-and-range-restriction-for-numeric-types

More details on this article

这篇文章的更多细节