【发布时间】:2013-05-30 12:25:07
【问题描述】:
我很难理解在 XSD 中定义类型扩展和限制的格式的一些细微差别。根据W3Schools reference:
-
simpleContent定义“对纯文本复杂类型或作为内容且不包含任何元素的简单类型的扩展或限制” -
complexContent定义“仅包含混合内容或元素的复杂类型的扩展或限制
我不清楚为什么 XSD 要求扩展和限制包含在这些容器之一中,此外,为什么仅扩展和限制需要它。如果必须在容器中定义所有“内容”,这对我来说会更有意义,但情况并非如此 - 对于基本类型,内容(sequences 等)被定义为complexType 容器。
以this example 为例,对我来说这似乎过于冗长:
<xs:complexType name="fullpersoninfo">
<xs:complexContent>
<xs:extension base="personinfo">
<xs:sequence>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
为什么不能这样写呢?
<xs:complexType name="fullpersoninfo">
<xs:extension base="personinfo">
<xs:sequence>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexType>
甚至像这样?
<xs:complexType name="fullpersoninfo" extends="personinfo">
<xs:sequence>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
我假设它的定义方式一定有某种原因,但我找不到任何线索说明原因。
【问题讨论】:
-
如果你转到w3schools,
restriction元素都不包含在complexContent和simpleContent中。这是一个错字还是因为它们都包含在simpleType元素中? -
我知道这个问题很老,但你的第三个例子在技术上是有效的。如 2.5.3 Empty Content (w3.org/TR/xmlschema-0/#emptyContent) 中所述,“定义为没有任何 simpleContent 或 complexContent 的复杂类型被解释为限制 anyType 的复杂内容的简写。”
-
@claytond 我使用 XSD 已经有一段时间了,但我认为您引用的规则不适用于我的示例,因为该规则仅适用于限制
anytype,而我的示例是从personinfo类型扩展而来的(或者至少这是意图) -
@KevinK 你是对的。我忽略了“extends”参数,没有意识到它是用来表示基础的。
标签: xsd