スキーマのコンパイル後の情報セット

W3C 勧告『XML Schema』では、スキーマ検証前とスキーマ コンパイル後に必ず公開する情報セットについて解説されています。 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

たとえば、XmlSchemaElement クラスと XmlSchemaComplexType クラスには 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)