What does the ref attribute on an element in an XSD do?

前端 未结 2 565
旧巷少年郎
旧巷少年郎 2020-12-25 10:01

Documentation seems to say that it references another element in the schema, but how could it be used - I\'ve never seen it in schemas that I have worked with.

Anyon

相关标签:
2条回答
  • 2020-12-25 10:09

    For example if you want to declare element types that can appear deeply nested, but also as top level elements in an instance document.

    The XML Schema Primer has examples for this: http://www.w3.org/TR/xmlschema-0/

    0 讨论(0)
  • 2020-12-25 10:19

    Basically it references another element that is delared elsewhere, which may or may not be the same schema document. For instance, it could come from an externally referenced schema in a different namespace. Suppose you use the item element a lot in several different schemas, you can declare it (and any other common types and attributes) in a common schema, and then reuse those in all your other schemas. If you reference your common schema with the namespace c, you can declare an instance of the item elemnt on its own or as part of a type as follows:

    <xs:element ref="c:item" /><!-- reference here -->
    <xs:complexType name="something">
        <xs:sequence>
            <xs:element ref="c:item" /><!-- and here -->
        </xs:sequence>
        <xs:element name="other" type="xs:Name" />
    </xs:complexType>
    

    The definition in the data schema would look like this:

    <xs:element name="item" type="itemType" /><!-- referenced element -->
    <xs:complexType name="itemType">
        <xs:sequence>
            <xs:element name="code" type="xs:Name" minOccurs="0" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:attribute name="description" type="xs:normalizedString" use="required" />
    </xs:complexType>
    
    0 讨论(0)
提交回复
热议问题