2009-01-15 96 views
2

我有一個元素爲0的XML表單,它格式正確但無效。 當我嘗試驗證它時,XMLSpy出現以下錯誤: 「隱藏」的空元素內不允許有任何內容。 下面是我的架構:如何解決XSD驗證錯誤?

<xs:element name="hidden"> 
    <xs:complexType> 
     <xs:attribute name="datatype" type="xs:string" use="optional"/> 
     <xs:attribute name="alias" type="xs:string" use="optional"/> 
     <xs:attribute name="source" type="xs:string" use="optional"/> 
     <xs:attribute name="name" type="xs:string" use="required"/> 
     <xs:attribute name="lookup" type="xs:string" use="optional"/> 
    </xs:complexType> 
</xs:element> 

我需要什麼添加到上面的架構來解決這個問題? Thanx ml

+0

你還沒有設法以可見的方式附加你的模式 - 你可以讓它和你的示例XML在服務器上的某個地方可用並鏈接到它們嗎? – 2009-01-15 12:59:52

+0

我修好了 - 它只需要縮進四個空格來顯示。 – 2009-01-15 13:00:39

回答

1

正如welbog所述,您定義了一個複雜的空元素。假設你只想要隱藏的標籤中的文字,你可以沿着論文線寫一個模式:

<xs:element name="hidden"> 
    <xs:complexType> 
    <xs:simpleContent> 
     <xs:extension base="xs:integer"> 
     <xs:attribute name="datatype" type="xs:string" use="optional"/> 
     <xs:attribute name="alias" type="xs:string" use="optional"/> 
     <xs:attribute name="source" type="xs:string" use="optional"/> 
     <xs:attribute name="name"  type="xs:string" use="required"/> 
     <xs:attribute name="lookup" type="xs:string" use="optional"/> 
     </xs:extension> 
    </xs:simpleContent> 
    </xs:complexType> 
</xs:element> 

這樣,您就可以擁有一塊XML的像這樣的:

<hidden datatype="foo" name="bar">0</hidden> 

這是怎麼回事在這裏,我定義的「隱藏」是擴展xs:integer(順便說一句,你可以讓它擴展你想要的任何類型),這意味着「隱藏」元素就像是整數元素,但有了額外的約束,或者在這種情況下具有附加屬性。

2

您的「隱藏」元素被定義爲空的,因爲您在架構中沒有任何明確允許子元素的內容。我假設你正在根據http://www.w3schools.com/Schema/schema_complex_empty.asp你已經隱含定義的「隱藏」是空的希望像

<hidden *[attributes]*> 
    <some_other_element/> 
</hidden> 

不過。您需要定義哪些元素可以在「隱藏」內出現。有很多方法可以做到這一點,我建議通過閱讀http://www.w3schools.com/Schema/schema_complex.asp開始。

+0

Thanx a milion weblog。我感謝您的幫助。 – Fet 2009-01-15 13:08:55