2013-04-30 72 views
2

我正在編寫一個XSD模式以驗證存儲(除其他外)若干個result元素的文檔。此元素的兩個屬性clickIndextimeViewed是可選的,並使用內置數據類型nonNegativeIntegerdecimal。而不是刪除這些屬性時,它們的值未設置的,我想將它們設置爲空字符串,像這樣:驗證屬性中的空字符串

<Page resultCount="10" visited="true"> 
    <Result index="1" clickIndex="1" timeViewed="45.21" pid="56118" title="alpha"/> 
    <Result index="2" clickIndex="" timeViewed="" pid="75841" title="beta"/> 
    <Result index="3" clickIndex="2" timeViewed="21.45" pid="4563" title="gamma"/> 
    <Result index="4" clickIndex="" timeViewed="" pid="44546" title="delta"/> 
    <Result index="5" clickIndex="" timeViewed="" pid="1651" title="epsilo"/> 
    <Result index="6" clickIndex="" timeViewed="" pid="551651" title="zeta"/> 
    <Result index="7" clickIndex="" timeViewed="" pid="20358" title="eta"/> 
    <Result index="8" clickIndex="" timeViewed="" pid="61621" title="theta"/> 
    <Result index="9" clickIndex="" timeViewed="" pid="135154" title="iota"/> 
    <Result index="10" clickIndex="" timeViewed="" pid="95821" title="kappa"/> 
</Page> 

不幸的是這不會驗證對以下XSD:

<xs:element name="Result"> 
    <xs:complexType> 
     <xs:attribute name="index" type="xs:nonNegativeInteger" use="required"/> 
     <xs:attribute name="clickIndex" type="xs:nonNegativeInteger" use="optional"/> 
     <xs:attribute name="timeViewed" type="xs:decimal" use="optional"/> 
     <xs:attribute name="pid" type="xs:positiveInteger" use="required"/> 
     <xs:attribute name="title" type="xs:string" use="required"/> 
    </xs:complexType> 
</xs:element> 

是否無法驗證具有已定義類型的屬性上的空字符串?有沒有辦法做到這一點,而不需要定義自定義類型?我是否僅僅忘記了XSD中的一個選項?

我將處理這些XML文件以生成統計信息,其中缺少的屬性可能會引入不必要的複雜性。如果沒有解決方法,我只需用零替換空值。我寧願保留空字符串,因爲它看起來更乾淨,但零也可能更有效。

回答

2

如果所討論的屬性是整數類型,則不能分配空字符串。引號內的值應該可以解析爲整數。你必須或者像你說的那樣用零值更新你的xml,或者刪除保存空字符串值的屬性。如果您不喜歡這些解決方案中的任何一種,那麼您必須定義自己的自定義類型。您可以參考此瞭解更多詳情

XSD make attribute nullable

+0

簡潔,重點突出,我應該意識到解析問題。 – Lilienthal 2013-05-02 06:51:34