60

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?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Fiona - myaccessible.website
  • 14,481
  • 16
  • 82
  • 117

2 Answers2

82

Basically it references another element that is declared 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 element 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>
Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
grkvlt
  • 2,577
  • 1
  • 21
  • 38
  • 5
    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: – rjray Sep 20 '09 at 17:51
  • 12
    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 Sep 20 '09 at 20:42
  • 6
    When would one choose ref over type? – pikachu0 Feb 09 '18 at 06:28
  • 1
    I think that pikachu0 might be asking why not just refer directly to the type? What is the advantage of declaring an element and then referring to an element of a type when I can simply create an element and refer to the type? I'm not sure if that warrants another question, but this answer still left me confused about the benefits of a ref to a element as opposed to just making an element with the type equal to the complex type name. – shawn1874 Mar 09 '22 at 02:53
  • I think that the use of refs vs the type is just a preference in how you want to organize the building blocks of the schema. – shawn1874 Mar 09 '22 at 03:09
5

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
  • 10,562
  • 12
  • 44
  • 55