Xsd Reference in XML
? XSD References in XML Schema
In XSD, a reference allows you to reuse an existing element definition instead of redefining it. This promotes modularity and avoids duplication in your schema.
? What is an XSD Reference?
Instead of defining an element inline, you can reference a globally defined element by its name.
The
refattribute is used inside an<xs:element>to point to another element.
? Example
1. Define a global element:
<xs:element name="address" type="xs:string"/>2. Use reference inside a complex type:
<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element ref="address"/> </xs:sequence> </xs:complexType></xs:element>? Full Example Schema
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <!-- Global element --> <xs:element name="address" type="xs:string"/> <xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <!-- Reference to global 'address' element --> <xs:element ref="address"/> </xs:sequence> </xs:complexType> </xs:element></xs:schema>? Corresponding Valid XML
<person> <name>John Doe</name> <address>123 Main St, Springfield</address></person>Why Use References?
Avoid repeating element definitions.
Changes in the global element propagate everywhere it's referenced.
Supports better schema organization and modularity.
Would you like an example showing referencing elements across multiple schema files with imports or includes?