简体   繁体   English

XML模式使用属性不能出现在元素中

[英]XML Schema Use Attribute Cannot Appear in Element

So when I put in the use ="required" attribute into my schema for an XML assignment, I get the following message: 因此,当我将use ="required"属性放入我的架构中进行XML分配时,我收到以下消息:

s4s-att-not-allowed: Attribute 'use' cannot appear in element 'element'. s4s-att-not-allowed:属性'use'不能出现在元素'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. 它根本不会影响我的代码,并且此赋值需要use属性。

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代码:

<?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. 代码似乎仍然有效,没有错误。

The use attribute is valid only for xs:attribute elements. use属性仅对xs:attribute元素有效。

If you want to specify that an element is mandatory or optional use the minOccurs attribute. 如果要指定元素是必需的或可选的,请使用minOccurs属性。

Element required: 要素:

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

Element optional: 元素可选:

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

Note that by default minOccurs is 1 , whereas by default use is optional . 请注意,默认情况下, minOccurs1 ,而默认情况下, useoptional

UPDATE UPDATE

The use of restriction requires the definition of a (simple) type. restriction的使用需要(简单)类型的定义。 This: 这个:

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

defines Price to be a decimal with 2 digits for the fraction part. Price定义为小数部分的2位数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM