开发者

Having an empty xs:date element being validated OK by the XSD

I have this in my XSD:

<xs:element name="End_Date" type="xs:date" minOccurs="0"/>

I would like the validation pass if there's a date or if there's an empty node

<End_Date>2011-05-31T00:00:00.000</End_Date> should be ok as well as <End_Date></End_Date>

How can I modifiy the XSD to make 开发者_如何学运维it so?

I tried different things:

nillable="true"

and

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"

and

                    <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>

None of them worked.

Error:

Error detected : The 'xxxxxxxxxx:End_Date' element is invalid - The value '' is invalid according to its datatype 'http://www.w3.org/2001/XMLSchema:date' - The string '' is not a valid XsdDateTime value.


Perhaps you are confused about the difference between xs:date and xs:dateTime. You have used xs:date in your schema, but your example is an xs:dateTime.

There are three ways of achieving what you want:

(a) define a type that is a union of (xs:dateTime and (restriction of xs:string allowing only ""))

(b) define a type that is a list of xs:dateTime with minLength = 0, maxLength = 1

(c) define the element to be nillable. In this case the instance will have to say xsi:nil="true", which to my mind makes the facility pretty useless.

If you try one of these and it doesn't work, tell us exactly what you did and exactly how it failed.


Set Type="xs:date", derivedBy="list" and minOccurs="0"

which looks like this in your XML Schem Document 

<xs:element name="EffectiveDt" nillable="true" minOccurs="0">
  <xs:simpleType>
   <xs:list itemType="xs:date"/>
  </xs:simpleType>
</xs:element>

This will surely help you, I tried in my Code Works perfect.

Validate Your XML against XSD here

I am using this online XML validation against XSD tool.


This worked for me :

[in xsd :]

<xs:simpleType name="DateOrEmptyDate">
    <xs:union memberTypes="xs:date">
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:enumeration value=""/>
            </xs:restriction>
        </xs:simpleType>
    </xs:union>
</xs:simpleType>    

[Valid xml :]

<EndDate/>
<EndDate>2017-12-31</EndDate>

[Invalid xml :]

<EndDate>2017-31-31</EndDate>


xsi:nill=true should definitely be valid as long as you define the schema element as nillable. What validator are you using?

Schema:

<xs:schema xmlns="http://myNS" targetNamespace="http://myNS" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="End_Date" nillable="true" type="xs:dateTime" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Instance:

<ns0:Root xmlns:ns0="myNS">
  <End_Date xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</ns0:Root>

is valid.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜