XSD :integer attribute empty value

2020-07-26 11:04发布

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? This should not be possible ideally. What needs to be added to achieve this?

标签: xml xsd
3条回答
冷血范
2楼-- · 2020-07-26 11:50

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

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

查看更多
Rolldiameter
3楼-- · 2020-07-26 12:00

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:

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

<xs:simpleType name="emptyInt">
    <xs:restriction base="xs:string">
        <xs:pattern value="-?\d*"/>
    </xs:restriction>
</xs:simpleType>
查看更多
在下西门庆
4楼-- · 2020-07-26 12:04

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

<?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>
查看更多
登录 后发表回答