2016-03-03 22 views
2

我有驗證XML文件對一個XSD架構與xmllint問題空標籤:xmllint孔潘與有效性的錯誤,像<foobar/>標籤預計不會盡管foobar定義在XSD模式是這樣的:XML模式驗證xmllint:抱怨像<foobar/>

<xs:element name="foobar" minOccurs="0"> 
    <xs:simpleType> 
     <xs:restriction base="xs:positiveInteger"> 
      <xs:minInclusive value="1"/> 
      <xs:maxInclusive value="9999"/> 
     </xs:restriction> 
    </xs:simpleType> 
</xs:element> 

對於比較:

<foobar>123</foobar>根據xmllint是有效的。 xmllint也不會抱怨,如果我從XML文件中完全剝離標籤foobar

問:

那麼,什麼是否認<foobar/>點?

謝謝!

P.S .:實際的錯誤消息:

myfile.xml:135298: element foobar: Schemas validity error : Element '{http://www.foobaz.com/namespace}foobar': '' is not a valid value of the local atomic type. 

P.P.S .: xmllint版本20901倍

回答

3

你已經在你的架構表示,該元素的值必須是1到9999的整數,但該元素的實際值是空的內容。我不明白爲什麼有任何問題你的模式不允許這個值。

如果你想允許無論是在這個範圍內或空內容的整數,也有做這件事的兩種可能的方式:

(一)定義一個聯合類型,其成員類型是(i)的一個整數範圍爲1〜9999,以及(ii)與小面length=0,或

(b)中的字符串定義一個列表類型,其項類型是在範圍爲1〜9999的整數,與具有minLength=0maxLength=1列表類型。

您也可以使用nillable="true"但後來<foobar/>是無效的內容,它必須是<foobar xsi:nil="true"/>哪(在我看來)完全擊敗了目的。

2

中的數字或正常值可以是正整數或空字符串。

低於可能的解決方案,否則您應該使用nillable="true"

<xs:simpleType name="positive-integer-or-empty"> 
    <xs:annotation> 
     <xs:documentation>The number-or-normal values can be either a positive integer or an empty string. This is used for the content of the ensemble element.</xs:documentation> 
    </xs:annotation> 
    <xs:union memberTypes="positive-integer-restricted"> 
     <xs:simpleType> 
      <xs:restriction base="xs:string"> 
       <xs:enumeration value=""/> 
      </xs:restriction> 
     </xs:simpleType> 
    </xs:union> 
</xs:simpleType> 
<xs:simpleType name="positive-integer-restricted"> 
    <xs:restriction base="xs:positiveInteger"> 
     <xs:minInclusive value="1"/> 
     <xs:maxInclusive value="9999"/> 
    </xs:restriction> 
</xs:simpleType>