Udostępnij za pośrednictwem


Post-Schema Compilation Infoset

The World Wide Web Consortium (W3C) XML Schema Recommendation discusses the information set (infoset) that must be exposed for pre-schema validation and post-schema compilation. The XML Schema Object Model (SOM) views this exposure before and after the Compile method of the XmlSchemaSet is called.

The pre-schema validation infoset is built during the editing of the schema. The post-schema compilation infoset is generated after the Compile method of the XmlSchemaSet is called, during compilation of the schema, and is exposed as properties.

The SOM is the object model that represents the pre-schema validation and post-schema compilation infosets; it consists of the classes in the System.Xml.Schema namespace. All read and write properties of classes in the System.Xml.Schema namespace belong to the pre-schema validation infoset, while all read-only properties of classes in the System.Xml.Schema namespace belong to the post-schema compilation infoset. The exception to this rule are the following properties, which are both pre-schema validation infoset and post-schema compilation infoset properties.

Class

Property

XmlSchemaObject

Parent

XmlSchema

AttributeFormDefault, BlockDefault, ElementFormDefault, FinalDefault, TargetNamespace

XmlSchemaExternal

Schema

XmlSchemaAttributeGroup

AnyAttribute

XmlSchemaParticle

MaxOccurs, MinOccurs

XmlSchemaComplexType

AnyAttribute

For example, the XmlSchemaElement and XmlSchemaComplexType classes both have BlockResolved and FinalResolved properties. These properties are used to hold the values for the Block and Final properties after the schema has been compiled and validated. BlockResolved and FinalResolved are read-only properties that are part of the post-schema compilation infoset.

The following example shows the ElementSchemaType property of the XmlSchemaElement class set after validating the schema. Before validation, the property contains a null reference, and the SchemaTypeName is set to the name of the type in question. After validation, the SchemaTypeName is resolved to a valid type, and the type object is available through the ElementSchemaType property.

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;
}

See Also

Other Resources

XML Schema Object Model (SOM)