Skip to content

Latest commit

 

History

History
103 lines (87 loc) · 6.48 KB

including-or-importing-xml-schemas.md

File metadata and controls

103 lines (87 loc) · 6.48 KB
description title ms.date dev_langs ms.assetid
Learn more about: Including or Importing XML Schemas
Including or Importing XML Schemas
03/30/2017
csharp
vb
cpp
fe1b4a11-37f4-4e1a-93c9-239f4fe736c0

Including or Importing XML Schemas

An XML schema may contain <xs:import />, <xs:include />, and <xs:redefine /> elements. These schema elements refer to other XML schemas that can be used to supplement the structure of the schema that includes or imports them. The xref:System.Xml.Schema.XmlSchemaImport, xref:System.Xml.Schema.XmlSchemaInclude and xref:System.Xml.Schema.XmlSchemaRedefine classes, map to these elements in the Schema Object Model (SOM) API.

Including or Importing an XML Schema

The following code example supplements the customer schema created in the Building XML Schemas topic with the address schema. Supplementing the customer schema with the address schema makes address types available in the customer schema.

The address schema can be incorporated using either <xs:include /> or <xs:import /> elements to use the components of the address schema as-is, or using an <xs:redefine /> element to modify any of its components to suit the need of the customer schema. Because the address schema has a targetNamespace that is different from that of the customer schema, the <xs:import /> element and therefore import semantics is used.

The code example includes the address schema in the following steps.

  1. Adds the customer schema and the address schema to a new xref:System.Xml.Schema.XmlSchemaSet object and then compiles them. Any schema validation warnings and errors encountered reading or compiling the schemas are handled by the xref:System.Xml.Schema.ValidationEventHandler delegate.

  2. Retrieves the compiled xref:System.Xml.Schema.XmlSchema objects for both the customer and address schemas from the xref:System.Xml.Schema.XmlSchemaSet by iterating over the xref:System.Xml.Schema.XmlSchemaSet.Schemas%2A property. Because the schemas are compiled, Post-Schema-Compilation-Infoset (PSCI) properties are accessible.

  3. Creates an xref:System.Xml.Schema.XmlSchemaImport object, sets the xref:System.Xml.Schema.XmlSchemaImport.Namespace%2A property of the import to the namespace of the address schema, sets the xref:System.Xml.Schema.XmlSchemaExternal.Schema%2A property of the import to the xref:System.Xml.Schema.XmlSchema object of the address schema, and adds the import to the xref:System.Xml.Schema.XmlSchema.Includes%2A property of the customer schema.

  4. Reprocesses and compiles the modified xref:System.Xml.Schema.XmlSchema object of the customer schema using the xref:System.Xml.Schema.XmlSchemaSet.Reprocess%2A and xref:System.Xml.Schema.XmlSchemaSet.Compile%2A methods of the xref:System.Xml.Schema.XmlSchemaSet class and writes it to the console.

  5. Finally, recursively writes all of the schemas imported into the customer schema to the console using the xref:System.Xml.Schema.XmlSchema.Includes%2A property of the customer schema. The xref:System.Xml.Schema.XmlSchema.Includes%2A property provides access to all the includes, imports, or redefines added to a schema.

The following is the complete code example and the customer and address schemas written to the console.

[!code-cppXmlSchemaImportExample#1] [!code-csharpXmlSchemaImportExample#1] [!code-vbXmlSchemaImportExample#1]

<?xml version="1.0" encoding="utf-8"?>  
<xs:schema xmlns:tns="http://www.tempuri.org" targetNamespace="http://www.tempuri.org" xmlns:xs="http://www.w3.org/2001/XMLSchema">  
  <xs:import namespace="http://www.example.com/IPO" />  
  <xs:element name="Customer">  
    <xs:complexType>  
      <xs:sequence>  
        <xs:element name="FirstName" type="xs:string" />  
        <xs:element name="LastName" type="tns:LastNameType" />  
      </xs:sequence>  
      <xs:attribute name="CustomerId" type="xs:positiveInteger" use="required" />  
    </xs:complexType>  
  </xs:element>  
  <xs:simpleType name="LastNameType">  
    <xs:restriction base="xs:string">  
      <xs:maxLength value="20" />  
    </xs:restriction>  
  </xs:simpleType>  
</xs:schema>  
<schema targetNamespace="http://www.example.com/IPO" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:ipo="http://www.example.com/IPO">  
  <annotation>  
    <documentation xml:lang="en">  
      Addresses for International Purchase order schema  
      Copyright 2000 Example.com. All rights reserved.  
    </documentation>  
  </annotation>  
  <complexType name="Address">  
    <sequence>  
      <element name="name"   type="string"/>  
      <element name="street" type="string"/>  
      <element name="city"   type="string"/>  
    </sequence>  
  </complexType>  
  <complexType name="USAddress">  
    <complexContent>  
      <extension base="ipo:Address">  
        <sequence>  
          <element name="state" type="ipo:USState"/>  
          <element name="zip"   type="positiveInteger"/>  
        </sequence>  
      </extension>  
    </complexContent>  
  </complexType>  
  <!-- other Address derivations for more countries or regions -->  
  <simpleType name="USState">  
    <restriction base="string">  
      <enumeration value="AK"/>  
      <enumeration value="AL"/>  
      <enumeration value="AR"/>  
      <!-- and so on ... -->  
    </restriction>  
  </simpleType>  
</schema>  

For more information about the <xs:import />, <xs:include />, and <xs:redefine /> elements and the xref:System.Xml.Schema.XmlSchemaImport, xref:System.Xml.Schema.XmlSchemaInclude and xref:System.Xml.Schema.XmlSchemaRedefine classes, see the W3C XML Schema and the xref:System.Xml.Schema?displayProperty=nameWithType namespace class reference documentation.

See also