System.Xml.Schema 名前空間内のクラスは、W3C 勧告『XML Schema』で定義された構造に割り当てられます。スキーマ要素は、属性を持っていたり子要素を含んでいる場合があるため、スキーマ オブジェクト モデル (SOM) 対応のクラスは、属性と子要素を含めることができるプロパティを持っています。
メモリ内のスキーマは、SOM の走査およびプロパティの設定や設定の解除を行うことによって、スキーマ ドキュメントのノードおよび属性の作成、挿入、または削除の操作ができます。W3C 勧告『XML Schema』に基づいて、スキーマのコンテンツ モデルは次のように指定されます。
Content: ((include | import | redefine | annotation)*, (((simpleType | complexType | group | attributeGroup) | element | attribute | notation), annotation*)*).
SOM に新しい項目を追加するときに、コンテンツ モデル内の最初の項目のセット (インクルード、インポート、および再定義) が Schema.Includes コレクションに追加され、そのコンテンツ モデル内のその他の項目が Schema.Items コレクションに追加されます。
空の XML スキーマを作成し、xs:integer
型を持つ要素 Price
を含めるようにそのスキーマを変更し、xs:positiveInteger
という制約ベースがある simpleType 要素を入れ子にするようにそのスキーマを変更する例を次に示します。
Imports System.IO
Imports System
Imports System.Xml
Imports System.Xml.Schema
Imports System.Text
Imports Microsoft.VisualBasic
Public Class EditSOM
Public Shared Sub ValidationCallbackOne(sender As Object, args As ValidationEventArgs)
If args.Severity = XmlSeverityType.Warning Then
Console.Write("WARNING: ")
Else
If args.Severity = XmlSeverityType.Error Then
Console.Write("ERROR: ")
End If
End If
Console.WriteLine(args.Message)
End Sub ' ValidationCallbackOne
Public Shared Sub Main()
Dim schema As New XmlSchema()
Console.WriteLine("Printing empty schema...")
schema.Compile(AddressOf ValidationCallbackOne)
schema.Write(Console.Out)
Console.WriteLine(ControlChars.CrLf & ControlChars.CrLf)
Dim priceElem As New XmlSchemaElement()
priceElem.Name = "Price"
priceElem.SchemaTypeName = New XmlQualifiedName("integer", "http://www.w3.org/2001/XMLSchema")
schema.Items.Add(priceElem)
schema.Compile(AddressOf ValidationCallbackOne)
Console.WriteLine("Printing modified schema containing element with type=""integer""")
schema.Write(Console.Out)
Console.WriteLine(ControlChars.CrLf & ControlChars.CrLf)
priceElem.SchemaTypeName = Nothing
Dim PriceType As New XmlSchemaSimpleType()
Dim PriceRestriction As New XmlSchemaSimpleTypeRestriction()
PriceRestriction.BaseTypeName = New XmlQualifiedName("positiveInteger", "http://www.w3.org/2001/XMLSchema")
PriceType.Content = PriceRestriction
Dim maxExclusive As New XmlSchemaMaxExclusiveFacet()
maxExclusive.Value = "100"
PriceRestriction.Facets.Add(maxExclusive)
priceElem.SchemaType = PriceType
schema.Compile(AddressOf ValidationCallbackOne)
Console.WriteLine("Printing modified schema containing element with nested simpleType")
schema.Write(Console.Out)
End Sub 'Main
End Class 'EditSOM
' Main(string[])
' EditSOM
[C#]
using System.IO;
using System;
using System.Xml;
using System.Xml.Schema;
using System.Text;
public class EditSOM {
public static void ValidationCallbackOne(object sender, ValidationEventArgs args) {
if(args.Severity == XmlSeverityType.Warning)
Console.Write("WARNING: ");
else if(args.Severity == XmlSeverityType.Error)
Console.Write("ERROR: ");
Console.WriteLine(args.Message);
}
public static void Main(string[] args){
XmlSchema schema = new XmlSchema();
Console.WriteLine("Printing empty schema...");
schema.Compile(new ValidationEventHandler(ValidationCallbackOne));
schema.Write(Console.Out);
Console.WriteLine("\n\n");
XmlSchemaElement priceElem = new XmlSchemaElement();
priceElem.Name = "Price";
priceElem.SchemaTypeName = new XmlQualifiedName("integer", "http://www.w3.org/2001/XMLSchema");
schema.Items.Add(priceElem);
schema.Compile(new ValidationEventHandler(ValidationCallbackOne));
Console.WriteLine("Printing modified schema containing element with type=\"integer\"");
schema.Write(Console.Out);
Console.WriteLine("\n\n");
priceElem.SchemaTypeName = null;
XmlSchemaSimpleType PriceType = new XmlSchemaSimpleType();
XmlSchemaSimpleTypeRestriction PriceRestriction =
new XmlSchemaSimpleTypeRestriction();
PriceRestriction.BaseTypeName =
new XmlQualifiedName("positiveInteger", "http://www.w3.org/2001/XMLSchema");
PriceType.Content = PriceRestriction;
XmlSchemaMaxExclusiveFacet maxExclusive =
new XmlSchemaMaxExclusiveFacet();
maxExclusive.Value = "100";
PriceRestriction.Facets.Add(maxExclusive);
priceElem.SchemaType = PriceType;
schema.Compile(new ValidationEventHandler(ValidationCallbackOne));
Console.WriteLine("Printing modified schema containing element with nested simpleType");
schema.Write(Console.Out);
}/* Main(string[]) */
}// EditSOM
上のコード例から次の出力が生成されます。
Printing empty schema...
<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema">
</schema>
Printing modified schema containing element with type="integer"
<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema">
<element name="Price" type="integer"/>
</schema>
Printing modified schema containing element with nested simpleType
<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:apo="http://www.example.com/PO1">
<element name="Price">
<simpleType>
<restiction base="positiveInteger">
<maxExclusive="100" />
</restriction>
</simpleType>
</element>
</schema>
参照
XML スキーマ オブジェクト モデル (SOM) | XML スキーマ リファレンス (XSD) | System.Xml.Schema