2011-09-09 18 views
5

我在XSD有這種被驗證OK:有一個空的xs:日期元素由XSD

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

我想驗證通過,如果有一個日期,或者如果有一個空節點

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

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

沒有一次成功。

錯誤:

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.

回答

6

也許你對xs:date和xs:dateTime之間的區別感到困惑。您在架構中使用了xs:date,但您的示例是xs:dateTime。

有實現的三種方式你想要什麼:

(一)定義了一個類型,它是一個聯盟(XS:datetime和XS的(限制:字符串只允許「」))

(b)定義一個類型,該類型是使用minLength = 0的xs:dateTime的列表,maxLength = 1

(c)將元素定義爲可填充的。在這種情況下,實例將不得不說xsi:nil =「true」,這在我看來使設施非常無用。

如果您嘗試其中之一併且無法正常工作,請告訴我們您做了什麼以及它究竟是如何失敗的。

+0

(a)做了這個工作,謝謝 – MoreCoffee

0

XSI:nill =真爲你定義的架構元素的nillable絕對應該作爲長期有效。你使用什麼驗證器?

模式:

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

實例:

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

是有效的。

+0

我使用.NET的驗證。實例是什麼意思?我有一個XSD。我嘗試在我的xs:元素中添加nillable =「true」,並得到相同的錯誤消息。 – MoreCoffee

+0

我假設你正試圖驗證一個xml對xsd?我的答案中的「實例」是要驗證的XML。因此,在xsd中,您定義了一個名爲End_Date的nillable類型,其中的nillable屬性= true。然後在xml文檔中創建End_Date元素時,可以使用xsi:nil =「true」屬性。正如我之前所說,這肯定會驗證。 –

+0

仍然沒有去:(在我的XML驗證我把:''在XSD我把:'我得到錯誤:\t'http://cdcixis-cm.com/gbo: End_Date'元素無效 - 值''根據其數據類型'無效'http://www.w3.org/2001/XMLSchema:date' - 字符串''不是有效的XsdDateTime值。' – MoreCoffee

1
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. 
+0

我發現這個策略的唯一問題是,你允許提供一個元素列表,例如... < EffectiveDt /> ... –

相關問題