简体   繁体   English

XSD:integer属性为空值

[英]XSD :integer attribute empty value

In the xsd , when we have an attribute with type xs:ID or xs:Integer as use:required, can we pass empty string to it? 在xsd中,当我们有一个类型为xs:ID或xs:Integer的属性,如use:required时,可以将空字符串传递给它吗? This should not be possible ideally. 理想情况下,这是不可能的。 What needs to be added to achieve this? 要实现此目的需要添加什么?

If you need an attribute that is allowed to contain int or empty string you can define custom type and use it as a type for your attribute: 如果需要允许包含int或空字符串的属性,则可以定义自定义类型并将其用作属性的类型:

<xs:simpleType name="emptyInt">
    <xs:union>
        <xs:simpleType>
            <xs:restriction base='xs:string'>
                <xs:length value="0"/>
            </xs:restriction>
        </xs:simpleType>
        <xs:simpleType>
            <xs:restriction base='xs:float'>
            </xs:restriction>
        </xs:simpleType>
    </xs:union>
</xs:simpleType>

Or using regExp: 或使用regExp:

<xs:simpleType name="emptyInt">
    <xs:restriction base="xs:string">
        <xs:pattern value="-?\d*"/>
    </xs:restriction>
</xs:simpleType>

If you declare an attribute to be of type xs:ID or xs:integer , then it will not be valid for the attribute to have a value of an empty string. 如果您声明一个属性为xs:IDxs:integer ,则该属性的值为空字符串将是无效的。 This is true regardless of whether the attribute is required or optional. 无论属性是必需属性还是可选属性,都是如此。

To be concrete, both <a x1=""/> and <a x2=""/> would be invalid for this XSD: <a x1=""/><a x1=""/><a x2=""/>对于此XSD都是无效的:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           version="1.0">
  <xs:element name="a">
    <xs:complexType>
      <xs:attribute name="x1" type="xs:ID"/>
      <xs:attribute name="x2" type="xs:integer"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

Two possible ways to define a type that accepts either an integer or an empty string are: 定义接受整数或空字符串的类型的两种可能方法是:

(a) define a list type with itemType=integer and maxLength=1 (a)使用itemType = integer和maxLength = 1定义列表类型

(b) define a union type with member types xs:integer and my:emptyString where my:EmptyString is defined by restriction from xs:string with length=0. (b)用成员类型xs:integer和my:emptyString定义联合类型,其中my:EmptyString是通过限制长度为0的xs:string定义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM