上面的"product"元素没有任何内容。">

XSD 空元素

创建于 2024-12-03 / 35
字体: [默认] [大] [更大]

空的复杂元素不能有内容,只能有属性。


复杂的空元素

空XML元素:

<product prodid="1345" />

上面的 "product"元素没有任何内容。要定义一个没有内容的类型,我们必须定义一个允许其内容中包含元素的类型,但实际上我们不声明任何元素,如下所示:

<xs:element name="product">
  <xs:complexType>
    <xs:complexContent>
      <xs:restriction base="xs:integer">
        <xs:attribute name="prodid" type="xs:positiveInteger"/>
      </xs:restriction>
    </xs:complexContent>
  </xs:complexType>
</xs:element>

在上面的例子中,我们定义了一个具有复杂内容的复杂类型。complexContent元素表示我们打算限制或扩展复杂类型的内容模型,integer的限制声明一个属性,但不引入任何元素内容。

但是,可以更简洁地声明"product"元素,如下所示:

<xs:element name="product">
  <xs:complexType>
    <xs:attribute name="prodid" type="xs:positiveInteger"/>
  </xs:complexType>
</xs:element>

或者,您可以为complexType元素指定一个名称,并让"product"元素具有引用complexType名称的type属性(如果使用此方法,则多个元素可以引用相同的复杂类型):

<xs:element name="product" type="prodtype"/>

<xs:complexType name="prodtype">
  <xs:attribute name="prodid" type="xs:positiveInteger"/>
</xs:complexType>

0 人点赞过