XSD 设置 nillable="false" 时未进行验证

XSD validation is not happening while setting nillable="false"

这是我要根据以下 XML 架构验证的 xml 文件。

<?xml version="1.0" encoding="UTF-8"?>
<Students>
<Name></Name>
<Phone>0123987654</Phone>
<Address>
In front of PNB
</Address>
<Dob>2002-09-24</Dob>
</Students>

XML 架构

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Students">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string" nillable="false"  />
<xs:element name="Phone" type="xs:integer"/>
<xs:element name="Address" type="xs:string"/>
<xs:element name="Dob" type="xs:date" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

在验证过程中,尽管名称字段为空并且在架构中设置了 nillable="false",但我没有注意到任何错误。名称字段不应为空,如果错误时未提供任何值,则应抛出验证错误。 任何人都可以建议解决方案如何通过 MinLength 实现与简单类型相同的方法,这对我来说是可能的,但复杂类型不确定如何进一步进行。

XSD nillable 属性是 允许 空内容的方法,而不是 禁止 的方法它。

以下是 the W3C spec for XSD 1.1 的摘录:

If {nillable} is true, then an element with no text or element content can be ·valid· despite a {type definition} which would otherwise require content, if it carries the attribute xsi:nil with the value true

所以 nillable="true" 定义了一个 例外 一些其他规则 禁止 一个空元素。通过在实例上指定 xsi:nil="true" 专门触发异常。

设置 nillable="false" 只是断言您不能向元素添加 xsi:nil 属性,它对元素是否可以为空没有影响。除非被架构的其他部分修改,否则这是所有元素的默认设置,如规范下方所示:

{nillable} The ·actual value· of the nillable [attribute], if present, otherwise false.

要指定元素不能为空,您需要一些其他规则,例如声明一个简单类型,它指定字符串的基本类型和最小长度 1:

<xs:simpleType name="nonEmptyString">
    <xs:restriction base="xs:string">
        <xs:minLength value="1" />
    </xs:restriction>
</xs:simpleType>