我在XSD中有这个:
<xs:element name="End_Date" type="xs:date" minOccurs="0"/>
Run Code Online (Sandbox Code Playgroud)
如果有日期或者有一个空节点,我想验证通过
<End_Date>2011-05-31T00:00:00.000</End_Date>
应该没问题
<End_Date></End_Date>
我怎样才能修改XSD来实现它呢?
我尝试了不同的东西:
nillable="true"
和
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"
Run Code Online (Sandbox Code Playgroud)
和
<xs:element name="End_Date">
<xs:simpleType>
<xs:union memberTypes="xs:date">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value=""/>
</xs:restriction>
</xs:simpleType>
</xs:union>
</xs:simpleType>
</xs:element>
Run Code Online (Sandbox Code Playgroud)
他们都没有工作.
错误:
检测到错误:'xxxxxxxxxx:End_Date'元素无效 - 根据数据类型'http://www.w3.org/2001/XMLSchema:date',值''无效 - 字符串''不是有效的XsdDateTime值.
也许你对xs:date和xs:dateTime之间的区别感到困惑.您在架构中使用了xs:date,但您的示例是xs:dateTime.
有三种方法可以达到你想要的效果:
(a)定义一个类型,它是(xs:dateTime和(xs:string allow only only"")的联合)
(b)定义一个类型,它是xs的列表:dateTime,minLength = 0,maxLength = 1
(c)将要素定义为可归零.在这种情况下,实例将不得不说xsi:nil ="true",这在我看来使得设施相当无用.
如果您尝试其中之一并且它不起作用,请告诉我们您的确切操作以及确切失败的原因.