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

发布于 2024-10-05 04:39:28 字数 2301 浏览 0 评论 0原文

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

给定 XML 示例:

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

和架构:

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

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.

Given XML sample of:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

贪了杯 2024-10-12 04:39:29

你尝试过吗

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

Have you tried

<xs:element name="roles" type="xs:integer" nillable="true"/>
八巷 2024-10-12 04:39:29

您所要做的就是对同一元素指定两个限制,并将它们合并起来,如下例所示:

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

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

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.

无声静候 2024-10-12 04:39:29

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

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.

抠脚大汉 2024-10-12 04:39:29

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

我只从一个非常大的 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>

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

本文的更多详细信息

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

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

<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

More details on this article

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文