XSD:日期元素为空或满足限制

Kri*_*n B 5 xml schema xsd

我有一个 XSD,并且我需要一个日期元素为空或在某个日期(2015 年 10 月 1 日)之后。

因此,应该允许以下情况:

<DDate></DDate>
<DDate>2015-10-10</DDate>
Run Code Online (Sandbox Code Playgroud)

我的 XSD 定义为:

<xs:element name = "DDate" nillable="true" >
    <xs:simpleType>
        <xs:restriction base="xs:date">
            <xs:minInclusive value="2015-10-01"/>
        </xs:restriction>
    </xs:simpleType>
</xs:element>
Run Code Online (Sandbox Code Playgroud)

这强制日期正确,但不允许日期为空。任何见解或建议将不胜感激。

Abe*_*bel 4

您的定义已经允许空DDate,除了一项限制外,您还必须指定xsi:nil="true",如下所示:

<!-- ns decl. should go to the root element -->
<DDate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="xsd-date-empty.xsd" 
    xsi:nil="true" />
Run Code Online (Sandbox Code Playgroud)

但是,如果使用“允许空”,则意味着您希望允许空白和/或空节点而不使用xsi:nil,那么有很多方法可以做到这一点。我可能会加入工会,如下所示:

<xs:element name = "DDate" nillable="true" >
    <xs:simpleType>
        <xs:union>
            <xs:simpleType>
                <xs:restriction base="xs:date">
                    <xs:minInclusive value="2015-10-01"/>
                </xs:restriction>        
            </xs:simpleType>            
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:whiteSpace value="collapse" />
                    <xs:length value="0" />
                </xs:restriction>        
            </xs:simpleType>            
        </xs:union>
    </xs:simpleType>
</xs:element>
Run Code Online (Sandbox Code Playgroud)

这意味着无非是允许:

  • 具有 minInclusive 限制的日期
  • 空格折叠后为空的字符串