返回介绍

包含或导入 XML 架构

发布于 2025-02-23 23:16:09 字数 13078 浏览 0 评论 0 收藏 0

XML 架构可以包含 <xs:import /><xs:include /><xs:redefine /> 元素。 这些架构元素引用其他 XML 架构,可以用于补充包括或导入这些架构的架构的结构。 XmlSchemaImport 、 XmlSchemaInclude 和 XmlSchemaRedefine 类映射到架构对象模型 (SOM) API 中的这些元素。

包括或导入 XML 架构

下面的代码示例中创建的客户架构补充 生成 XML 架构 地址架构的主题。 为客户架构补充地址架构后,可以在客户架构中使用地址类型。

地址架构可以使用 <xs:include /><xs:import /> 元素加入,以原样使用地址架构的组件,也可以使用 <xs:redefine /> 元素修改其任意组件,以适合客户架构的需要。 因为地址架构的 targetNamespace 与客户架构的不同,所以,将使用 <xs:import /> 元素以及导入语义。

代码示例通过下列步骤包括客户架构。

  1. 将客户架构和地址架构添加到新的 XmlSchemaSet 对象并进行编译。 _在读取或编译架构时遇到的任何架构验证警告和错误由 ValidationEventHandler 委托进行处理。
  2. 通过循环访问 XmlSchema 属性,从 XmlSchemaSet 中为客户架构和地址架构检索已编译的 Schemas 对象。 因为架构已编译,所以,可以访问后架构编译信息集 (PSCI) 属性。
  3. 创建一个 XmlSchemaImport 对象,将导入的 Namespace 属性设置为地址架构的命名空间,将导入的 Schema 属性设置为地址架构的 XmlSchema 对象,并将导入添加到客户架构的 Includes 属性中。
  4. 使用 XmlSchema 类的 Reprocess 和 Compile 方法重新处理并编译已修改的客户架构的 XmlSchemaSet 对象并将其写入控制台。
  5. 最后,使用客户架构的 Includes 方法递归式将导入到客户架构的所有架构写入控制台。 Includes 属性提供对所有添加到架构中的包括、导入或重新定义的访问。

以下是完整的代码示例以及写入控制台的客户架构和地址架构。

#using <System.Xml.dll>

using namespace System;
using namespace System::Xml;
using namespace System::Xml::Schema;

ref class XmlSchemaImportExample
{
public:

  static void Main()
  {
    // Add the customer and address schemas to a new XmlSchemaSet and compile them.
    // Any schema validation warnings and errors encountered reading or 
    // compiling the schemas are handled by the ValidationEventHandler delegate.
    XmlSchemaSet^ schemaSet = gcnew XmlSchemaSet();
    schemaSet->ValidationEventHandler += gcnew ValidationEventHandler(ValidationCallback);
    schemaSet->Add("http://www.tempuri.org", "customer.xsd");
    schemaSet->Add("http://www.example.com/IPO", "address.xsd");
    schemaSet->Compile();

    // Retrieve the compiled XmlSchema objects for the customer and
    // address schema from the XmlSchemaSet by iterating over 
    // the Schemas property.
    XmlSchema^ customerSchema = nullptr;
    XmlSchema^ addressSchema = nullptr;
    for each (XmlSchema^ schema in schemaSet->Schemas())
    {
      if (schema->TargetNamespace == "http://www.tempuri.org")
        customerSchema = schema;
      else if (schema->TargetNamespace == "http://www.example.com/IPO")
        addressSchema = schema;
    }

    // Create an XmlSchemaImport object, set the Namespace property
    // to the namespace of the address schema, the Schema property 
    // to the address schema, and add it to the Includes property
    // of the customer schema.
    XmlSchemaImport^ import = gcnew XmlSchemaImport();
    import->Namespace = "http://www.example.com/IPO";
    import->Schema = addressSchema;
    customerSchema->Includes->Add(import);

    // Reprocess and compile the modified XmlSchema object 
    // of the customer schema and write it to the console.  
    schemaSet->Reprocess(customerSchema);
    schemaSet->Compile();
    customerSchema->Write(Console::Out);

    // Recursively write all of the schemas imported into the
    // customer schema to the console using the Includes 
    // property of the customer schema.
    RecurseExternals(customerSchema);     
  }

  static void RecurseExternals(XmlSchema^ schema)
  {
    for each (XmlSchemaExternal^ external in schema->Includes)
    {
      if (external->SchemaLocation != nullptr)
      {
        Console::WriteLine("External SchemaLocation: {0}", external->SchemaLocation);
      }

			if (external::typeid == XmlSchemaImport::typeid)
      {
        XmlSchemaImport^ import = dynamic_cast<XmlSchemaImport^>(external);
        Console::WriteLine("Imported namespace: {0}", import->Namespace);
      }

      if (external->Schema != nullptr)
      {
        external->Schema->Write(Console::Out);
        RecurseExternals(external->Schema);
      }
    }
  }

  static void ValidationCallback(Object^ sender, ValidationEventArgs^ args)
  {
		if (args->Severity == XmlSeverityType::Warning)
      Console::Write("WARNING: ");
    else if (args->Severity == XmlSeverityType::Error)
      Console::Write("ERROR: ");

    Console::WriteLine(args->Message);
  }
};

int main()
{
	XmlSchemaImportExample::Main();
  return 0;
}
using System;
using System.Xml;
using System.Xml.Schema;

class XmlSchemaImportExample
{
  static void Main(string[] args)
  {
    // Add the customer and address schemas to a new XmlSchemaSet and compile them.
    // Any schema validation warnings and errors encountered reading or 
    // compiling the schemas are handled by the ValidationEventHandler delegate.
    XmlSchemaSet schemaSet = new XmlSchemaSet();
    schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);
    schemaSet.Add("http://www.tempuri.org", "customer.xsd");
    schemaSet.Add("http://www.example.com/IPO", "address.xsd");
    schemaSet.Compile();

    // Retrieve the compiled XmlSchema objects for the customer and
    // address schema from the XmlSchemaSet by iterating over 
    // the Schemas property.
    XmlSchema customerSchema = null;
    XmlSchema addressSchema = null;
    foreach (XmlSchema schema in schemaSet.Schemas())
    {
      if (schema.TargetNamespace == "http://www.tempuri.org")
        customerSchema = schema;
      else if (schema.TargetNamespace == "http://www.example.com/IPO")
        addressSchema = schema;
    }

    // Create an XmlSchemaImport object, set the Namespace property
    // to the namespace of the address schema, the Schema property 
    // to the address schema, and add it to the Includes property
    // of the customer schema.
    XmlSchemaImport import = new XmlSchemaImport();
    import.Namespace = "http://www.example.com/IPO";
    import.Schema = addressSchema;
    customerSchema.Includes.Add(import);

    // Reprocess and compile the modified XmlSchema object 
    // of the customer schema and write it to the console.  
    schemaSet.Reprocess(customerSchema);
    schemaSet.Compile();
    customerSchema.Write(Console.Out);

    // Recursively write all of the schemas imported into the
    // customer schema to the console using the Includes 
    // property of the customer schema.
    RecurseExternals(customerSchema);     
  }

  static void RecurseExternals(XmlSchema schema)
  {
    foreach (XmlSchemaExternal external in schema.Includes)
    {
      if (external.SchemaLocation != null)
      {
        Console.WriteLine("External SchemaLocation: {0}", external.SchemaLocation);
      }

      if (external is XmlSchemaImport)
      {
        XmlSchemaImport import = external as XmlSchemaImport;
        Console.WriteLine("Imported namespace: {0}", import.Namespace);
      }

      if (external.Schema != null)
      {
        external.Schema.Write(Console.Out);
        RecurseExternals(external.Schema);
      }
    }
  }

  static void ValidationCallback(object sender, ValidationEventArgs args)
  {
    if (args.Severity == XmlSeverityType.Warning)
      Console.Write("WARNING: ");
    else if (args.Severity == XmlSeverityType.Error)
      Console.Write("ERROR: ");

    Console.WriteLine(args.Message);
  }
}
Imports System
Imports System.Xml
Imports System.Xml.Schema

Class XmlSchemaImportExample

  Shared Sub Main()
    ' Add the customer and address schemas to a new XmlSchemaSet and compile them.
    ' Any schema validation warnings and errors encountered reading or 
    ' compiling the schemas are handled by the ValidationEventHandler delegate.
    Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()
    AddHandler schemaSet.ValidationEventHandler, AddressOf ValidationCallback
    schemaSet.Add("http://www.tempuri.org", "customer.xsd")
    schemaSet.Add("http://www.example.com/IPO", "address.xsd")
    schemaSet.Compile()

    ' Retrieve the compiled XmlSchema objects for the customer and
    ' address schema from the XmlSchemaSet by iterating over 
    ' the Schemas property.
    Dim customerSchema As XmlSchema = Nothing
    Dim addressSchema As XmlSchema = Nothing
    For Each schema As XmlSchema In schemaSet.Schemas()
      If schema.TargetNamespace = "http://www.tempuri.org" Then
        customerSchema = schema
      ElseIf schema.TargetNamespace = "http://www.example.com/IPO" Then
        addressSchema = schema
      End If
    Next

    ' Create an XmlSchemaImport object, set the Namespace property
    ' to the namespace of the address schema, the Schema property 
    ' to the address schema, and add it to the Includes property
    ' of the customer schema.
    Dim import As XmlSchemaImport = New XmlSchemaImport()
    import.Namespace = "http://www.example.com/IPO"
    import.Schema = addressSchema
    customerSchema.Includes.Add(import)

    ' Reprocess and compile the modified XmlSchema object 
    ' of the customer schema and write it to the console.  
    schemaSet.Reprocess(customerSchema)
    schemaSet.Compile()
    customerSchema.Write(Console.Out)

    ' Recursively write all of the schemas imported into the
    ' customer schema to the console using the Includes 
    ' property of the customer schema.
    RecurseExternals(customerSchema)
  End Sub

  Shared Sub RecurseExternals(ByVal schema As XmlSchema)
    For Each external As XmlSchemaExternal In Schema.Includes

      If Not external.SchemaLocation = Nothing Then
        Console.WriteLine("External SchemaLocation: {0}", external.SchemaLocation)
      End If

      If external.GetType() Is GetType(XmlSchemaImport) Then
        Dim import As XmlSchemaImport = CType(external, XmlSchemaImport)
        Console.WriteLine("Imported namespace: {0}", import.Namespace)
      End If

      If Not external.Schema Is Nothing Then
        external.Schema.Write(Console.Out)
        RecurseExternals(external.Schema)
      End If
    Next
  End Sub

  Shared Sub ValidationCallback(ByVal sender As Object, ByVal args As ValidationEventArgs)
    If args.Severity = XmlSeverityType.Warning Then
      Console.Write("WARNING: ")
    Else
      If args.Severity = XmlSeverityType.Error Then
        Console.Write("ERROR: ")
      End If
    End If
    Console.WriteLine(args.Message)
  End Sub
End Class
<?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>  

有关详细信息 <xs:import /><xs:include /> ,和 <xs:redefine /> 元素和 XmlSchemaImport , XmlSchemaInclude 和 XmlSchemaRedefine 类,请参阅 W3C XML 架构 和 System.Xml.Schema 命名空间类参考文档。

另请参阅

XML 架构对象模型概述
读取和写入 XML 架构
生成 XML 架构
遍历 XML 架构
编辑 XML 架构
编译架构的 XmlSchemaSet

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文