后架构编译信息集
万维网联合会 (W3C) XML 架构建议介绍了必须公开的架构验证前和架构编译后信息集。 XML 架构对象模型 (SOM) 在调用 Compile 的 XmlSchemaSet 方法之前或之后查看此信息集。
前架构验证信息集在编辑架构过程中生成。 后架构编译信息集在调用 Compile 的 XmlSchemaSet 方法之后、编译架构期间生成,并以属性的形式公开。
SOM 是表示前架构验证和后架构编译信息集的对象模型;该模型由 System.Xml.Schema 命名空间中的类组成。 System.Xml.Schema 命名空间中的类的所有读取和写入属性属于前架构验证信息集,而 System.Xml.Schema 命名空间中的类的所有只读属性属于后架构编译信息集。 此规则不适用于下列属性,下列属性既是前架构验证信息集属性也是后架构编译信息集属性。
例如,XmlSchemaElement 和 XmlSchemaComplexType 类均具有 BlockResolved
和 FinalResolved
属性。 在编译并验证了架构之后,这些属性用于存放 Block
和 Final
属性的值。 BlockResolved
和 FinalResolved
是属于后架构编译信息集的只读属性。
以下示例显示在验证架构之后设置的 ElementSchemaType 类的 XmlSchemaElement 属性。 在验证之前,该属性包含 null
引用,SchemaTypeName 设置为相应类型的名称。 在验证之后,SchemaTypeName 被解析为有效类型,并且可以通过 ElementSchemaType 属性使用类型对象。
#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