4

So when I put in the use ="required" attribute into my schema for an XML assignment, I get the following message:

s4s-att-not-allowed: Attribute 'use' cannot appear in element 'element'.

What does it mean? It doesn't seem to be affecting my code at all, and the use attribute is required for this assignment.

Schema Code:

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

And here is my XML code:

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

The code seems to work still, there are not errors.

4

1 回答 1

4

use属性仅对xs:attribute元素有效。

如果要指定元素是必需的还是可选的,请使用该minOccurs属性。

所需元素:

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

元素可选:

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

请注意,默认情况下minOccurs1,而默认情况下useoptional

更新

的使用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 位的小数。

于 2013-04-24T15:00:01.687 回答