XML - XML Schema Substitution Groups and Polymorphic Elements

 
 

1. Introduction

XML Schema Definition (XSD) is used to define the structure, elements, attributes, and data types of an XML document. It ensures that an XML document follows a predefined set of rules and maintains consistency in its data.

In ordinary XML Schema validation, an element must generally appear in the location and form specified by the schema. However, some applications require flexibility. For example, a system may need to accept different types of elements in the same position while maintaining a common structural definition.

XML Schema provides a feature called Substitution Groups to achieve this flexibility. Substitution groups allow one XML element to replace another element during document validation, provided that the elements follow the required schema rules.

This concept is closely related to polymorphism in object-oriented programming, where different types of objects can be used through a common interface or parent type.

2. What Are XML Schema Substitution Groups?

A substitution group is a collection of XML elements in which one element acts as the main or head element, while other elements are declared as its members.

The head element represents a general element, and its member elements represent more specific alternatives. When an XML document is validated, a member element can appear wherever the head element is permitted.

For example, consider an educational management system that stores information about different types of examinations. Instead of defining separate structures for every examination type in the same location, a common element called examination can be created.

Other elements, such as internalExam, semesterExam, and practicalExam, can become members of the substitution group headed by examination.

This allows the XML document to use different examination elements while following a common schema definition.

3. How Substitution Groups Work

Substitution groups work through a relationship between a head element and its member elements.

The head element is declared first in the XML Schema. Member elements are then associated with it using the substitutionGroup attribute.

When an XML document is validated, the XML processor checks whether the element used in the document belongs to the substitution group of the element expected at that position.

If the relationship is valid and all other schema restrictions are satisfied, the document passes validation.

The main components involved are:

  • Head Element: The primary element that represents a general category.

  • Member Element: An element that can replace the head element.

  • SubstitutionGroup Attribute: An XSD attribute that establishes the relationship between a member element and its head element.

  • Type Compatibility: The member element must use a type that is compatible with the declared type of the head element.

4. Example of XML Schema Substitution Groups

Consider an XML application that manages different types of examinations in a college.

The following XML Schema demonstrates how substitution groups can be created.

XML Schema (XSD):

<?xml version="1.0"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="examination" type="xs:string"/>

    <xs:element name="internalExam"
                type="xs:string"
                substitutionGroup="examination"/>

    <xs:element name="semesterExam"
                type="xs:string"
                substitutionGroup="examination"/>

    <xs:element name="practicalExam"
                type="xs:string"
                substitutionGroup="examination"/>

    <xs:element name="college">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="examination"
                            minOccurs="0"
                            maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>

In this example:

  1. The examination element is declared as the head element.

  2. The internalExam element is registered as a member of the examination substitution group.

  3. The semesterExam element is also registered as a member.

  4. The practicalExam element becomes another member of the same substitution group.

  5. The college element allows the examination element to appear multiple times.

The XML document can therefore use any of these member elements in place of the head element.

XML Document:

<?xml version="1.0"?>

<college>
    <internalExam>Computer Science Internal Test</internalExam>
    <semesterExam>First Semester Examination</semesterExam>
    <practicalExam>Programming Laboratory Test</practicalExam>
</college>

The document is valid because all three examination elements belong to the substitution group of the examination element and use compatible data types.

5. What Are Polymorphic Elements in XML?

Polymorphism is a programming concept that allows different types of objects to be handled through a common parent type or interface.

XML Schema supports a similar concept through substitution groups and type derivation.

In XML, polymorphic elements allow different element types or structures to be represented through a common element declaration or type definition.

For example, an online shopping application may contain different payment methods, such as:

  • Credit card payment

  • Debit card payment

  • Bank transfer

  • Digital wallet payment

All these payment methods belong to a common category called payment. However, each payment method may contain different information.

A credit card payment may require a card number and expiry date, whereas a bank transfer may require an account number and bank name.

By using XML Schema polymorphism, an application can support these different structures while maintaining a common data model.

6. Difference Between Substitution Groups and Type Derivation

Substitution groups and type derivation are related XML Schema concepts, but they work differently.

Substitution Groups

Type Derivation

Allows one element to replace another element.

Allows one data type to extend or restrict another data type.

Uses the substitutionGroup attribute.

Uses extension or restriction.

Establishes a relationship between element declarations.

Establishes a relationship between data types.

Member elements can appear in place of the head element.

Derived types can be used where their base type is expected, subject to schema rules.

Useful for providing alternative XML elements.

Useful for creating specialized data structures.

For example, a semesterExam element can replace an examination element through substitution groups. Similarly, a specialized examination type can extend a general examination type through type derivation.

These features can also be used together to create flexible and reusable XML structures.

7. Important Rules of Substitution Groups

XML Schema substitution groups follow certain rules to ensure that XML documents remain valid and consistent.

1. Head Element Declaration

The head element must be declared before it can be referenced by substitution group members.

2. Compatible Data Types

A member element must have a type that is compatible with the head element's type. It may use the same type or a permitted derived type.

3. Element Names Must Be Unique

Elements within the same XML namespace must follow XML Schema naming and declaration rules. Different member elements must have distinguishable qualified names.

4. Abstract Elements

An element can be declared as abstract using the abstract="true" attribute. An abstract head element cannot appear directly in an XML document, but its valid substitution group members can appear instead.

5. Blocking Substitution

The block attribute can restrict which types of substitutions are allowed. This helps prevent unwanted substitutions during validation.

6. Substitution Group Membership

A member element must be explicitly associated with the head element through the substitutionGroup attribute.

8. Advantages of XML Schema Substitution Groups and Polymorphic Elements

Substitution groups and polymorphic elements provide several advantages when designing complex XML applications.

Improved Flexibility: Different XML elements can be used in the same position without changing the overall document structure.

Code Reusability: Developers can define common elements and types once and reuse them across multiple XML documents.

Simplified Schema Management: A common head element can represent several related elements, making complex schemas easier to organize.

Better Extensibility: New member elements can be introduced without completely redesigning the existing XML structure, provided the schema relationships remain compatible.

Consistent Validation: XML Schema ensures that substituted elements follow the required data type and structural rules.

Support for Complex Applications: These features are useful when an application must manage multiple related categories of information through a common structure.

9. Applications of Substitution Groups and Polymorphic Elements

Substitution groups and polymorphic elements are useful in different industries and software applications.

Banking Applications: Different transaction types, such as deposits, withdrawals, and transfers, can be represented through a common transaction structure.

Educational Management Systems: Different examination types, student records, and academic activities can be organized using common XML element declarations.

E-Commerce Applications: Different product categories and payment methods can be represented through reusable XML structures.

Healthcare Systems: Different medical record types, laboratory reports, and clinical documents can be organized under common element definitions.

Enterprise Applications: Large business applications can use substitution groups to manage different types of business documents while maintaining consistent XML validation.

Web Services: XML-based web services can use flexible schemas to exchange different types of related information between applications.

10. Conclusion

XML Schema Substitution Groups and Polymorphic Elements are important features for creating flexible, reusable, and extensible XML document structures. Substitution groups allow one element to replace another through a predefined relationship, while polymorphic behaviour allows different specialized structures to be handled through a common element or data type.

These features reduce duplication, simplify schema design, and support applications that need to process multiple types of related information. By understanding how head elements, member elements, type compatibility, and type derivation work together, developers can create XML schemas that are easier to maintain and extend.

They are particularly useful in enterprise applications, banking systems, educational platforms, healthcare applications, and XML-based web services where different types of information must be represented through a common and consistent structure.