共用方式為


後結構描述編譯資訊集

全球資訊網協會 (W3C) XML 結構描述建議事項 (英文) 中討論為了進行前置結構描述驗證和後置結構描述編譯所必須公開的資訊集。 XML 結構描述物件模型 (SOM) 會在呼叫 XmlSchemaSetCompile 方法之前及之後,檢視此公開資訊集。

前結構描述驗證資訊集建置於結構描述的編輯期間。 後結構描述編譯資訊集是在結構描述的編譯期間,並於呼叫 XmlSchemaSetCompile 方法之後產生的,而且會公開為屬性。

SOM 是表示前結構描述驗證及後結構描述編譯資訊集的物件模型,它是由 System.Xml.Schema 命名空間中的類別組成。 System.Xml.Schema 命名空間中類別的所有讀取及寫入屬性都屬於前結構描述驗證資訊集,而 System.Xml.Schema 命名空間中類別的所有唯讀屬性都屬於後結構描述編譯資訊集。 下列屬性是此規則的例外,它們同時屬於前結構描述驗證資訊集及後結構描述編譯資訊集的屬性。

類別

屬性

XmlSchemaObject

Parent

XmlSchema

AttributeFormDefault, BlockDefault, ElementFormDefault, FinalDefault, TargetNamespace

XmlSchemaExternal

Schema

XmlSchemaAttributeGroup

AnyAttribute

XmlSchemaParticle

MaxOccurs, MinOccurs

XmlSchemaComplexType

AnyAttribute

例如,XmlSchemaElementXmlSchemaComplexType 類別都具有 BlockResolved 及 FinalResolved 屬性。 在編譯及驗證結構描述之後,這些屬性可用於儲存 Block 及 Final 屬性的值。 BlockResolved 及 FinalResolved 是唯讀屬性,其屬於後結構描述編譯資訊集。

下列範例顯示驗證結構描述之後,XmlSchemaElement 類別集的 ElementSchemaType 屬性。 驗證之前,該屬性包含 null 參考,且 SchemaTypeName 是設為所討論之型別的名稱。 驗證之後,SchemaTypeName 會解析為有效型別,而且型別物件可透過 ElementSchemaType 屬性來使用。

Imports System
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 refered 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
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 refered 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);
    }

}
#using <System.Xml.dll>

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

ref class PsciSample
{
private:
    static void ValidationCallbackOne(Object^ sender, ValidationEventArgs^ args)
    {
        Console::WriteLine(args->Message);
    }

public:
    static void Main()
    {
        XmlSchema^ schema = gcnew XmlSchema();

        // Create an element of type integer and add it to the schema.
        XmlSchemaElement^ priceElem = gcnew XmlSchemaElement();
        priceElem->Name = "Price";
        priceElem->SchemaTypeName = gcnew 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 = gcnew XmlSchemaSet();
        ValidationEventHandler^ eventHanlder = gcnew ValidationEventHandler(ValidationCallbackOne);
        schemaSet->ValidationEventHandler += eventHanlder;
        schemaSet->Add(schema);
        schemaSet->Compile();

        for each (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 refered to a valid type.
        Console::WriteLine("After compilation the ElementSchemaType of Price is "
           + priceElem->ElementSchemaType);

    }
};

int main()
{
    PsciSample::Main();
    return 0;
}

請參閱

其他資源

XML 結構描述物件模型 (SOM)