架构后编译信息集

万维网联盟 (W3C) XML 架构建议讨论了必须公开的信息集(信息集),以便进行架构前验证和架构后编译。 XML 架构对象模型 (SOM) 在调用该方法Compile之前和之后XmlSchemaSet查看此公开。

预架构验证信息集是在编辑架构期间生成的。 在架构编译过程中,调用架构后Compile编译信息集后XmlSchemaSet生成,并作为属性公开。

SOM 是表示预架构验证和架构后编译信息集的对象模型;它由命名空间中的 System.Xml.Schema 类组成。 命名空间中 System.Xml.Schema 类的所有读取和写入属性都属于预架构验证信息集,而命名空间中 System.Xml.Schema 类的所有只读属性都属于架构后编译信息集。 此规则的例外是以下属性,这些属性既是架构前验证信息集,也是架构后编译信息集属性。

班级 资产
XmlSchemaObject Parent
XmlSchema AttributeFormDefaultBlockDefaultElementFormDefaultFinalDefaultTargetNamespace
XmlSchemaExternal Schema
XmlSchemaAttributeGroup AnyAttribute
XmlSchemaParticle MaxOccursMinOccurs
XmlSchemaComplexType AnyAttribute

例如, XmlSchemaElement 类和 XmlSchemaComplexType 类都具有 BlockResolved 属性和 FinalResolved 属性。 这些属性用于在编译和验证架构后保存这些属性的值BlockFinalBlockResolvedFinalResolved 是属于架构后编译信息集的只读属性。

下面的示例演示了验证架构后设置的 ElementSchemaType 类的 XmlSchemaElement 属性。 在验证之前,该属性包含 null 引用,并且将 SchemaTypeName 设置为相关类型的名称。 验证后,将 SchemaTypeName 解析为有效类型,并且类型对象可通过 ElementSchemaType 属性获得。

using System;
using System.Xml;
using System.Xml.Schema;

public class PsciSample
{
    public static void Main(string[] args)
    {
        XmlSchema schema = new XmlSchema();

        // Create an element of type integer and add it to the schema.
        XmlSchemaElement priceElem = new XmlSchemaElement();
        priceElem.Name = "Price";
        priceElem.SchemaTypeName = new XmlQualifiedName("integer", "http://www.w3.org/2001/XMLSchema");
        schema.Items.Add(priceElem);

        // Print the pre-compilation value of the ElementSchemaType property
        // of the XmlSchemaElement which is a PSCI property.
        Console.WriteLine("Before compilation the ElementSchemaType of Price is " + priceElem.ElementSchemaType);

        //Compile the schema which validates the schema and
        // if valid will place the PSCI values in certain properties.
        XmlSchemaSet schemaSet = new XmlSchemaSet();
        schemaSet.ValidationEventHandler += ValidationCallbackOne;
        schemaSet.Add(schema);
        schemaSet.Compile();

        foreach (XmlSchema compiledSchema in schemaSet.Schemas())
        {
            schema = compiledSchema;
        }

        // After compilation of the schema, the ElementSchemaType property of the
        // XmlSchemaElement will contain a reference to a valid object because the
        // SchemaTypeName referred to a valid type.
        Console.WriteLine("After compilation the ElementSchemaType of Price is "
           + priceElem.ElementSchemaType);
    }

    private static void ValidationCallbackOne(object sender, ValidationEventArgs args)
    {
        Console.WriteLine(args.Message);
    }
}
Imports System.Xml
Imports System.Xml.Schema

Public Class PsciSample

    Public Shared Sub Main()

        Dim schema As New XmlSchema()

        ' Create an element of type integer and add it to the schema.
        Dim priceElem As New XmlSchemaElement()
        priceElem.Name = "Price"
        priceElem.SchemaTypeName = New XmlQualifiedName("integer", "http://www.w3.org/2001/XMLSchema")
        schema.Items.Add(priceElem)

        ' Print the pre-compilation value of the ElementSchemaType property
        ' of the XmlSchemaElement which is a PSCI property.
        Console.WriteLine("Before compilation the ElementSchemaType of Price is {0}", priceElem.ElementSchemaType)

        ' Compile the schema which validates the schema and
        ' if valid will place the PSCI values in certain properties.
        Dim schemaSet As New XmlSchemaSet()
        AddHandler schemaSet.ValidationEventHandler, AddressOf ValidationCallbackOne
        schemaSet.Add(schema)
        schemaSet.Compile()

        For Each compiledSchema As XmlSchema In schemaSet.Schemas()
            schema = compiledSchema
        Next

        ' After compilation of the schema, the ElementSchemaType property of the
        ' XmlSchemaElement will contain a reference to a valid object because the
        ' SchemaTypeName referred to a valid type.
        Console.WriteLine("After compilation the ElementSchemaType of Price is {0}", _
                priceElem.ElementSchemaType)

    End Sub

    Private Shared Sub ValidationCallbackOne(ByVal sender As Object, ByVal args As ValidationEventArgs)
        Console.WriteLine(args.Message)
    End Sub

End Class

另请参阅