7

定义 XSD 时,您可以选择将类型定义为嵌套类型或全局类型 (complexType)。

我知道全局类型在态射或元素重用方面更有用。

但是,如果您有一个大数据模型,则必须为每个级别定义一个全局 complexType,然后创建一个引用全局类型的元素。

嵌套

<xs:element name="person">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="firstname"/>
                            <xs:element name="lastname"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="address">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="street"/>
                            <xs:element name="city"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>

全球的

<xs:element name="person">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="name" type="nameType"/>
            <xs:element name="address" type="addressType"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>
<xs:complexType name="nameType">
    <xs:sequence>
        <xs:element name="firstname"/>
        <xs:element name="lastname"/>
    </xs:sequence>
</xs:complexType>
<xs:complexType name="addressType">
    <xs:sequence>
        <xs:element name="street"/>
        <xs:element name="city"/>
    </xs:sequence>
</xs:complexType>

因此,我的问题是:您何时使用嵌套类型而不是使它们成为全局类型?

对于任何感兴趣的人:我的问题在某种程度上与这个问题有关(XML 属性与元素)。

4

1 回答 1

8

In the example given, there's no real difference between the two - and no significant advantages or disadvantages to either.

However, in larger schemas things can get very untidy and difficult to manage when the practice for choosing nested over global isn't clearly defined.

The obvious reasons for using global types (primarily reuse, also nesting) tend to dictate - in general I prefer one mode or the other. I.e. if you're reusing some complexTypes but not others, make them all global. If you're not reusing anything, make them all nested.

The exception to this (and this is something I've come across frequently) is if the definition of the types make up the bulk of the complexity (!) of your schema, and the their containment is relatively simple. In this case, regardless of whether they're reused, I'd recommend making them global as it's far easier to restructure/reorder your document when you don't have to wade through massive complexType definitions. They're also theoretically more portable.

There are also cases where you can't acheive certain document structures with nested types - an example of this is using two complexTypes in a sequence that can contain 0 to unbounded instances of each type, in any mixed order. This isn't possible with nested types, but it is with referenced global types.

于 2008-09-02T15:07:49.040 回答