Udostępnij za pośrednictwem


Zestaw informacji po kompilacji schematu

Zalecenie dotyczące schematu XML konsorcjum World Wide Web Consortium (W3C) omawia zestaw informacji (infoset), który należy uwidocznić w celu weryfikacji schematu wstępnego i kompilacji po schemacie. Model obiektów schematu XML (SOM) wyświetla tę ekspozycję przed wywołaną Compile metodą i po metodzie XmlSchemaSet .

Zestaw informacji sprawdzania poprawności wstępnego schematu jest kompilowany podczas edytowania schematu. Zestaw informacji kompilacji po schemacie jest generowany po Compile wywołaniu metody XmlSchemaSet , podczas kompilacji schematu i jest udostępniany jako właściwości.

SOM to model obiektów, który reprezentuje zestawy informacji weryfikacji przed schematem i po kompilacji schematu; składa się z klas w System.Xml.Schema przestrzeni nazw. Wszystkie właściwości odczytu i zapisu klas w System.Xml.Schema przestrzeni nazw należą do zestawu informacji sprawdzania poprawności przed schematem, podczas gdy wszystkie właściwości klas tylko do odczytu w System.Xml.Schema przestrzeni nazw należą do zestawu informacji kompilacji po schemacie. Wyjątkiem od tej reguły są następujące właściwości, które są zarówno właściwościami infoset weryfikacji przed schematem, jak i po kompilacji schematu.

Klasa Właściwości
XmlSchemaObject Parent
XmlSchema AttributeFormDefault, , BlockDefault, ElementFormDefault, , FinalDefaultTargetNamespace
XmlSchemaExternal Schema
XmlSchemaAttributeGroup AnyAttribute
XmlSchemaParticle MaxOccurs, MinOccurs
XmlSchemaComplexType AnyAttribute

Na przykład XmlSchemaElement klasy i XmlSchemaComplexType mają BlockResolved właściwości i FinalResolved . Te właściwości są używane do przechowywania wartości właściwości Block i Final po skompilowaniu i zweryfikowaniu schematu. BlockResolved i FinalResolved są właściwościami tylko do odczytu, które są częścią zestawu informacji kompilacji po schemacie.

Poniższy przykład przedstawia ElementSchemaType właściwość klasy ustawionej XmlSchemaElement po zweryfikowaniu schematu. Przed walidacją null właściwość zawiera odwołanie, a SchemaTypeName właściwość jest ustawiona na nazwę danego typu. Po weryfikacji obiekt SchemaTypeName jest rozpoznawany jako prawidłowy typ, a obiekt typu jest dostępny za pośrednictwem ElementSchemaType właściwości .

#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^ eventHandler = gcnew ValidationEventHandler(ValidationCallbackOne);
        schemaSet->ValidationEventHandler += eventHandler;
        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 referred to a valid type.
        Console::WriteLine("After compilation the ElementSchemaType of Price is "
           + priceElem->ElementSchemaType);

    }
};

int main()
{
    PsciSample::Main();
    return 0;
}
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

Zobacz też