Partager via


Jeu d'informations de post-compilation de schéma

La recommandation du World Wide Web Consortium (W3C) sur le schéma XML décrit le jeu d'informations qui doit être exposé pour la pré-validation de schéma et la post-compilation de schéma. Le Modèle Objet du schéma (SOM) XML observe cette exposition avant et après l'appel à la méthode Compile de l'objet XmlSchemaSet.

Le jeu d'informations de pré-validation de schéma est généré pendant la modification du schéma. Le jeu d'informations de post-compilation de schéma est généré après l'appel à la méthode Compile de l'objet XmlSchemaSet, lors de la compilation du schéma et est exposé comme des propriétés.

Le SOM est le modèle d'objet qui représente les jeux d'informations de pré-validation de schéma et de post-compilation de schéma. Il est composé de classes dans l'espace de noms System.Xml.Schema. Toutes les propriétés de lecture et d'écriture des classes de l'espace de noms System.Xml.Schema appartiennent au jeu d'informations de pré-validation de schéma, alors que toutes les propriétés en lecture seule des classes de l'espace de noms System.Xml.Schema appartiennent au jeu d'informations de post-compilation de schéma. Les propriétés suivantes, qui sont des propriétés des jeux d'informations de pré-validation de schéma et de post-compilation de schéma, constituent une exception à la règle.

Classe

Propriété

XmlSchemaObject

Parent

XmlSchema

AttributeFormDefault, BlockDefault, ElementFormDefault, FinalDefault, TargetNamespace

XmlSchemaExternal

Schema

XmlSchemaAttributeGroup

AnyAttribute

XmlSchemaParticle

MaxOccurs, MinOccurs

XmlSchemaComplexType

AnyAttribute

Par exemple, les classes XmlSchemaElement et XmlSchemaComplexType ont des propriétés BlockResolved et FinalResolved. Ces propriétés permettent de contenir les valeurs des propriétés Block et Final après validation et compilation du schéma. BlockResolved et FinalResolved sont des propriétés en lecture seule qui font partie du jeu d'informations de post-compilation de schéma.

L'exemple suivant montre la propriété ElementSchemaType de la classe XmlSchemaElement définie après la validation du schéma. Avant la validation, la propriété contient une référence null et la propriété SchemaTypeName est définie sur le nom du type en question. Après la validation, la propriété SchemaTypeName est résolue en un type valide et l'objet de type est disponible par l'intermédiaire de la propriété 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;
}

Voir aussi

Autres ressources

Modèle Objet du schéma (SOM) XML