How to use XSD Substitution Group in XML

In this article I am going to explain about substitution Element in XML.
  • 2963

XSD Substitution Group in XML

Substitution Group is a feature of XML Schema by which can specify element that replace another element in document that generated from schema. The replaceable element is called the head element and must be defined schema in global scope. The element of substitution group must be same type of head element. In other word Substitution allow we build collection of element that can be specified using generic element.

Substitution Group mainly use where user want to choose element from optional element in XML document then we use substitution group.

Define substitution group. //In this example "name" is the head element and "country" substitution from "name".

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

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

In this example we are using Substitution group with XML documenet.

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

<xs:element name="country" substitutionGroup="name"/>

 

<xs:complexType name="EmpDetail">

  <xs:sequence>

    <xs:element ref="name"/>

  </xs:sequence>

</xs:complexType>

 

<xs:element name="EmpName" type="EmpDetail"/>

<xs:element name="Jown" substitutionGroup="EmpName"/>

Blocking element Substitution

If we want to prevent another element from substitution element with a specified element we use Block element.

<xs:element name="name" type="xs:string" block="substitution"/>

<xs:element name="country" substitutionGroup="name"/>

 

<xs:complexType name="EmpDetail">

  <xs:sequence>

    <xs:element ref="name"/>

  </xs:sequence>

</xs:complexType>

 

<xs:element name="EmpName" type="EmpDetail" block="substitution"/>

<xs:element name="Jown" substitutionGroup="EmpName"/>

Further Readings

You may also want to read these related articles: here
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

 

© 2019 DotNetHeaven. All rights reserved.