An empty complex element cannot have contents, only attributes.
一个空的复合元素不能含有内容,只能含有属性。
Complex Empty Elements
复合空元素(Complex Empty Elements)
An empty XML element:
一个空的XML元素:
<product prodid="1345" /> |
The "product" element above has no content at all. To define a type with no content, we must define a type that allows only elements in its content, but we do not actually declare any elements, like this:
上述"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> |
In the example above, we define a complex type with a complex content. The complexContent element signals that we intend to restrict or extend the content model of a complex type, and the restriction of integer declares one attribute but does not introduce any element content.
上述例子中,我们定义了一个有复合内容的复合类型。复合内容的元素表示了我们想要约束或扩充的复合类型的内容模式。对整数的约束声明了一个属性,但并没有介绍任何元素内容。
However, it is possible to declare the "product" element more compactly, like this:
但是,可以更加简洁地声明"product"元素,就像这样:
<xs:element name="product"> <xs:complexType> <xs:attribute name="prodid" type="xs:positiveInteger"/> </xs:complexType> </xs:element> |
Or you can give the complexType element a name, and let the "product" element have a type attribute that refers to the name of the complexType (if you use this method, several elements can refer to the same complex type):
或者你可以给complexType元素起个名称,并让"product"元素有个类型属性,而且类型属性引用的是complexType的名称(如果你用这个方法,几个元素可以引用相同的复合类型):
<xs:element name="product" type="prodtype"/> <xs:complexType name="prodtype"> <xs:attribute name="prodid" type="xs:positiveInteger"/> </xs:complexType> |