因此,当我将use ="required"属性放入 XML 分配的架构中时,我收到以下消息:
s4s-att-not-allowed:属性“use”不能出现在元素“element”中。
这是什么意思?它似乎根本不会影响我的代码,并且此分配需要 use 属性。
架构代码:
<?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>
Run Code Online (Sandbox Code Playgroud)
这是我的 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>
Run Code Online (Sandbox Code Playgroud)
该代码似乎仍然可以工作,没有错误。
该use属性仅对元素有效xs:attribute。
如果要指定某个元素是强制元素还是可选元素,请使用该minOccurs属性。
所需元素:
<xs:element name="PartID" type="xs:ID" minOccurs="1"/>
Run Code Online (Sandbox Code Playgroud)
可选元素:
<xs:element name="PartID" type="xs:ID" minOccurs="0"/>
Run Code Online (Sandbox Code Playgroud)
请注意,默认情况下minOccurs是1,而默认情况下use是optional。
更新
使用restriction需要定义一个(简单)类型。这:
<xs:element name="Price" minOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:decimal">
<xs:fractionDigits value="2"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Run Code Online (Sandbox Code Playgroud)
定义Price为小数部分有 2 位数字的小数。
| 归档时间: |
|
| 查看次数: |
12429 次 |
| 最近记录: |