这是我想要根据下面的 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"
为某些其他规则定义了一个异常(exception),该规则禁止空元素。该异常是通过在实例上指定 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>
关于xml - 设置 nillable ="false"时未进行 XSD 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63362545/