2013-04-24 38 views
2

所以,當我把在使用=「必需的」屬性到我的架構爲XML分配,我得到以下信息:XML Schema的使用屬性不能元素出現

S4S-ATT-不被允許:屬性'use'不能出現在元素'element'中。

這是什麼意思?它似乎並沒有成爲影響我的代碼在所有,爲此需要分配使用屬性。

架構代碼:

<?xml version="1.0" encoding="UTF-8"?> 

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
<xs:element name="Inventory"> 
<xs:complexType> 
    <xs:sequence> 
    <xs:element name="Items" use="required"> 
     <xs:complexType> 
     <xs:sequence> 
      <xs:element name="PartID" type="xs:ID" use="required"/> 
      <xs:element name="Part_Desc" type="xs:string" use="required"/> 
      <xs:element name="Price" type="xs:decimal" use="required"/> 
         <xs:restriction base="xs:decimal"> 
       <xs:fractionDigits value="2"/> 
       </xs:restriction> 
     </xs:sequence> 
     <xs:attribute name="vendor_id" type="xs:int" use="required"/> 
     </xs:complexType> 
     </xs:element> 
     </xs:sequence> 
    </xs:complexType> 
    </xs:element> 
</xs:schema> 

這裏是我的XML代碼:

<?xml version="1.0" encoding="UTF-8"?> 
    <Inventory xsi:noNamespaceSchemaLocation="exam.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 

<Items vendor_id="2">   
<PartID>a101</PartID>  
<Part_Desc>Keyboard</Part_Desc>   
<Price>79.99</Price>  
</Items>  

<Items vendor_id="5">   
<PartID>a404</PartID>   
<Part_Desc>Wireless Mouse</Part_Desc>   
<Price>59.99</Price>  
</Items>  

<Items vendor_id="3">   
<PartID>a120</PartID>  
<Part_Desc>2 GB USB Drive</Part_Desc>   
<Price>18.99</Price> 
</Items>  

<Items vendor_id="8">   
<PartID>c506</PartID> 
<Part_Desc>24" Monitor</Part_Desc>   
<Price>459.99</Price> 
</Items> 

</Inventory> 

的代碼似乎還在努力,沒有什麼是示數出來。謝謝大家,愛這個地方。

回答

2

use屬性僅對xs:attribute元素是有效的。

如果要指定一個元素是強制性的還是可選的使用minOccurs屬性。

元素所需:

<xs:element name="PartID" type="xs:ID" minOccurs="1"/> 

元可選:

<xs:element name="PartID" type="xs:ID" minOccurs="0"/> 

注意,默認情況minOccurs1,而默認情況下useoptional

UPDATE

採用restriction需要的(簡單)類型的定義。此:

<xs:element name="Price" minOccurs="1"> 
    <xs:simpleType> 
    <xs:restriction base="xs:decimal"> 
     <xs:fractionDigits value="2"/> 
    </xs:restriction> 
    </xs:simpleType> 
</xs:element> 

限定Price爲與2位數字的小數部分小數。

+0

謝謝,但現在當我在元素中取出use屬性,並且將其留空或放入minOccurs時,我收到一條消息,指出不允許使用xs:restriction。但是,再次,似乎沒有任何錯誤。 – user2297971 2013-04-24 15:23:50

+0

我加了關於如何使用'restriction'的解釋。我不知道什麼是您使用的驗證,但只要架構本身是無效的(因爲它是如此),驗證程序應該只是拒絕它,不要做任何XML驗證。再次感謝 – MiMo 2013-04-24 16:37:39

+0

。我正在使用一個名爲Editix的程序。免費下載XML編輯器。 – user2297971 2013-04-24 21:51:00

相關問題