XSD :整数属性空值

use*_*864 2 xml xsd

在 xsd 中,当我们有一个类型为 xs:ID 或 xs:Integer 的属性为 use:required 时,我们可以将空字符串传递给它吗?这在理想情况下是不可能的。需要添加什么来实现这一点?

Vic*_*tor 5

如果您需要一个允许包含 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>
Run Code Online (Sandbox Code Playgroud)

或者使用正则表达式:

<xs:simpleType name="emptyInt">
    <xs:restriction base="xs:string">
        <xs:pattern value="-?\d*"/>
    </xs:restriction>
</xs:simpleType>
Run Code Online (Sandbox Code Playgroud)