Difference between include and import elements in XML Schema - XSD with Example

As we all know that include and import elements in a xsd is used to reference schema components from another schema,
In this post I will explore the difference between them and when to use what with simple examples and how we can use the same in BizTalk Server.

Include element - include refers to declarations or definitions that are from same namespace or a no target namespace.
That is if the Schema being added also contains elements with same namespace then we can directly include it.
XSD Include means Importing types from another schema within the SAME namespace
When we are referring to the declarations or definitions from the same namespaces then we use Include element.

Import element - import refers to declarations or definitions that are from a different target namespace.
The import element is used to add multiple schema's with different target namespace to a document.
XSD Import means Importing types from another schema/namespace
When we are referring to the declarations or definitions from different namespaces then we use Import element.

Example on Include and Import

Schema 1) ReferenceSchema.xsd – This is the schema that will be referred in MainSchema1.xsd or MainSchema2.xsd (The namespace in the RefernceSchema.xsd is the Production namespace i.e. "http://abc.com/production")

Structure of ReferenceSchema.xsd
<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns="http://abc.com/production"
           targetNamespace="http://abc.com/production"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:simpleType name="OrderNumType">
    <xs:restriction base="xs:string" />
  </xs:simpleType>
</xs:schema>

MainSchemaX.xsd – Can mean MainSchema1.xsd or MainSchema2.xsd (Refer below)
Structure of MainSchemaX.xsd is as - 

MainSchema.xsd


Schema 2) MainSchema1.xsd (Example for include) – In this schema, elements belong to  namespace -  http://abc.com/production i.e. (Production namespace) hence targetNamespace is set to "http://abc.com/production"

In the MainSchema1.xsd the target Namespace is same as ReferenceSchema.xsd hence we will directly include the ReferenceSchema.xsd and use it.
If the targetNamespace attribute is not specified in the ReferenceSchema.xsd then also we can include it.

Structure of MainSchema1.xsd
 <?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns="http://abc.com/production"
           targetNamespace="http://abc.com/production"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:include schemaLocation=".\ReferenceSchema.xsd" />
  <xs:element name="RootOrder">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Order">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="OrderNo">
                <xs:simpleType>
                  <xs:restriction base="OrderNumType" />
                </xs:simpleType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>


Schema 3) MainSchema2.xsd (Example for import) – In this schema the elements belong to the namespace -  http://abc.com/development i.e. (Development namespace) hence targetNamespace is set to "http://abc.com/development"

In the MainSchema2.xsd the target Namespace is different as ReferenceSchema.xsd we have to import the ReferenceSchema.xsd and use it.

Structure of MainSchema2.xsd
 <?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns="http://abc.com/developement
           xmlns:ns0="http://abc.com/production"
           targetNamespace="http://abc.com/developement"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import schemaLocation=".\ReferenceSchema.xsd" namespace="http://abc.com/production" />
  <xs:element name="RootOrder">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Order">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="OrderNo">
                <xs:simpleType>
                  <xs:restriction base="ns0:OrderNumType" />
                </xs:simpleType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>




Click on the root node i.e. Schema of a Schema file -> Right click and clock properties -> In the Properties window -> Click On the "Imports" Properties -> Select XSD Import/XSD Include and click on Add-> Select the Schema -> Done



You may be interested in - 

1)  XML Design Consideration – What Should be used XML Elements or Attributes - BizTalk Schema Design Consideration


No comments: