views:

196

answers:

2

As the question says really, 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.

Anyone got any nice use cases or something that could explain its use further?

Thanks :)

+1  A: 

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/

zedoo
+2  A: 

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>
grkvlt
I up-voted this, as it is a good explanation. However, a ref attribute refers to an external entity by its "id", not "name". For your example to work, the declaration of "item" in the second code-block needs to be: <xs:element id="item" name="item" type="itemType" />
rjray
According to all XSD documentation I've seen, the "ref" applies to a "name", as seen here: http://www.w3schools.com/schema/el_element.asp (ref - Optional. Refers to the name of another element. ...)
grkvlt