Condividi tramite


Infoset di compilazione post-schema

La raccomandazione di XML Schema W3C (World Wide Web Consortium) illustra il set di informazioni (infoset) che deve essere esposto per la convalida pre-schema e la compilazione post-schema. Il modello SOM (XML Schema Object Model) visualizza questa esposizione prima e dopo la chiamata del Compile metodo di XmlSchemaSet .

L'infoset di convalida pre-schema viene compilato durante la modifica dello schema. L'infoset di compilazione post-schema viene generato dopo la chiamata del Compile metodo di XmlSchemaSet , durante la compilazione dello schema e viene esposto come proprietà.

SOM è il modello a oggetti che rappresenta gli infoset di convalida pre-schema e di compilazione post-schema; è costituito dalle classi nello spazio dei nomi System.Xml.Schema. Tutte le proprietà di lettura e scrittura delle classi nello spazio dei nomi appartengono all'infoset System.Xml.Schema di convalida pre-schema, mentre tutte le proprietà di sola lettura delle classi nello System.Xml.Schema spazio dei nomi appartengono all'infoset di compilazione post-schema. L'eccezione a questa regola sono le proprietà seguenti, che sono sia le proprietà infoset di convalida pre-schema che le proprietà infoset di compilazione post-schema.

Classe Proprietà
XmlSchemaObject Parent
XmlSchema AttributeFormDefault, BlockDefault, ElementFormDefault, FinalDefaultTargetNamespace
XmlSchemaExternal Schema
XmlSchemaAttributeGroup AnyAttribute
XmlSchemaParticle MaxOccurs, MinOccurs
XmlSchemaComplexType AnyAttribute

Ad esempio, le classi XmlSchemaElement e XmlSchemaComplexType hanno entrambe le proprietà BlockResolved e FinalResolved. Queste proprietà vengono utilizzate per contenere i valori per le Block proprietà e Final dopo la compilazione e la convalida dello schema. BlockResolved e FinalResolved sono proprietà di sola lettura che fanno parte dell'infoset di compilazione post-schema.

Nell'esempio seguente viene mostrata la proprietà ElementSchemaType della classe XmlSchemaElement impostata dopo la convalida dello schema. Prima della convalida, la proprietà contiene un null riferimento e SchemaTypeName viene impostata sul nome del tipo in questione. Dopo la convalida, l'oggetto SchemaTypeName viene risolto in un tipo valido e l'oggetto tipo è disponibile tramite la ElementSchemaType proprietà .

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

Vedere anche