xml 有一个空的 xs:date 元素被 XSD 验证 OK

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7360906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 14:56:31  来源:igfitidea点击:

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

xmlxsd

提问by MoreCoffee

I have this in my XSD:

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

<End_Date>2011-05-31T00:00:00.000</End_Date>应该没问题 <End_Date></End_Date>

How can I modifiy the XSD to make it so?

我如何修改 XSD 以使其如此?

I tried different things:

我尝试了不同的事情:

nillable="true"

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.

检测到错误:'xxxxxxxxxx:End_Date' 元素无效 - 根据其数据类型 'http://www.w3.org/2001/XMLSchema:date',值 '' 无效 - 字符串 '' 不是有效的 XsdDateTime价值。

回答by Michael Kay

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.

也许您对 xs:date 和 xs:dateTime 之间的区别感到困惑。您在架构中使用了 xs:date,但您的示例是 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 ""))

(a) 定义一个类型,它是 (xs:dateTime 和 (xs:string 的限制只允许 ""))

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

(b) 定义一个类型,它是一个 xs:dateTime 列表,其中 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.

(c) 将元素定义为可空。在这种情况下,实例将不得不说 xsi:nil="true",在我看来,这使得该工具非常无用。

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

如果您尝试其中一种方法但它不起作用,请准确告诉我们您做了什么以及它是如何失败的。

回答by Arpit Patel

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

在此处根据 XSD 验证您的 XML

I am using this online XML validation against XSD tool.

回答by user7499251

This worked for me :

这对我有用:

[in xsd :]

[在 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 :]

[有效的 xml :]

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

[Invalid xml :]

[无效的 xml :]

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

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

回答by tom redfern

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

只要您将架构元素定义为 nillable,xsi:nill=true 就肯定是有效的。你用的是什么验证器?

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.

已验证。