スキーマの検証時に発生する可能性のある例外イベントは 2 つあります。警告とエラーです。パーサーが、解析できないために検証できない要素を検出すると、警告が発生します。警告の発生後も、パーサーはインスタンス ドキュメントの処理を継続できます。これに対してエラーが発生するのは、無効な状況を検出したことによってパーサーが処理を完了できなかった場合です。
警告
警告は、通常、検証するスキーマ情報を持っていない要素をパーサーが検出すると発生します。
次の例では、XmlValidatingReader が html
、p
、br
、b
、i
など、理解できない要素についての警告を出します。XmlValidatingReader は、これらの要素を検出してもエラーを生成しません。any 要素の示すとおり、subject
要素は XML ドキュメント内のどの名前空間からでもマークアップを無制限に使用できるからです。
Warning.xsd の内容についての概要を示す XML スキーマを次に示します。
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xs:element name="content" type="contentType" />
<xs:complexType name="contentType">
<xs:sequence>
<xs:element name="subject" type="subjectType" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="subjectType" >
<xs:sequence>
<xs:any namespace="##any" processContents="skip" maxOccurs="unbounded"/>
</xs:sequence>
<xs:anyAttribute namespace="##any" processContents="skip" />
</xs:complexType>
</xs:schema>
Warning.xsd に準拠する Warning.xml の内容についての概要を次に示します。
<?xml version="1.0" ?>
<content xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:noNamespaceSchemaLocation="Warning.xsd">
<subject>
<html>
<p>sdfsdf</p>
<br/>
<b> bold or <i> italic </i> or strong </b>
</html>
</subject>
</content>
エラー
エラーとは、パーサーが検証の対象である XML ドキュメントの処理を継続できないようにするイベントのことです。要素がその親要素のコンテンツ モデル内に正しく配置されていない場合に、エラーが発生します。
extension 要素が complexType 要素の有効な子要素でないためにエラーが生成される例を次に示します。
<xs:complexType>
<xs:extension base='xs:decimal'>
<xs:attribute name='sizing' type='xs:string' />
</xs:extension>
</xs:complexType" />
検証イベントの処理
スキーマ オブジェクト モデル (SOM) では、エラーや警告を処理するために、ValidationEventHandler デリゲートを使用できます。このデリゲートは、ユーザーによってデザインされるコールバックです。このコールバックは、検証時にエラーや警告が発生すると呼び出されます。
ValidationEventHandler が登録されている場合、パーサーは無効な情報を破棄し、エラーに関する情報を ValidationEventHandler に渡すことによって、そのエラーから回復できます。ValidationEventHandler が指定されていない場合、エラーから回復せずに処理が停止します。
検証イベントが発生すると、ValidationEventHandler は検証イベントの重大度を判断するために使用できる Severity プロパティを持つ ValidationEventArgs オブジェクトを渡します。また、ValidationEventArgs は、エラー メッセージを含む Message プロパティと、ValidationEventHandler が指定されない場合にスローされる例外を含む Exception プロパティも保持しています。
エラーおよび警告の ValidationEventHandler との関係を次の表に示します。
ValidationEventHandler がある場合 | ValidationEventHandler がない場合 | |
---|---|---|
警告 | ValidationEventHandler が呼び出され、XmlSeverityType.Warning と等価の ValidationEventArgs.Severity が渡された場合、ドキュメントの処理は継続します。 | 例外はスローされず、スキーマ ドキュメントの処理は継続します。 |
エラー | ValidationEventHandler が呼び出され、XmlSeverityType.Error と等価の ValidationEventArgs.Severity が渡された場合、ドキュメントの処理は継続しますが、無効なデータは破棄されます。 | 例外がスローされ、スキーマ ドキュメントの処理が停止します。 |
ValidationEventHandler を使用して XML スキーマ ファイルでの検証エラーを処理する例を次に示します。ファイルに検証エラーがあっても、エラーの最初の出現時には例外をスローせずに、ファイル全体を検証していることに注意してください。
Imports System.IO
Imports System
Imports System.Xml
Imports System.Xml.Schema
Imports System.Text
Class ReadWriteSample
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) ' Prints the error to the screen.
End Sub 'ValidationCallbackOne
Public Shared Sub Main()
Try
Dim reader As New XmlTextReader("Bad_example.xsd")
Dim myschema As XmlSchema = XmlSchema.Read(reader, New ValidationEventHandler(AddressOf ValidationCallbackOne))
myschema.Write(Console.Out)
Dim file As New FileStream("New.xsd", FileMode.Create, FileAccess.ReadWrite)
Dim xwriter As New XmlTextWriter(file, New UTF8Encoding())
xwriter.Formatting = Formatting.Indented
myschema.Write(xwriter)
Catch e As Exception
Console.WriteLine(e)
End Try
End Sub 'Main
End Class 'ReadWriteSample
[C#]
using System.IO;
using System;
using System.Xml;
using System.Xml.Schema;
using System.Text;
class ReadWriteSample {
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); // Print the error to the screen.
}
public static void Main() {
try{
XmlTextReader reader = new XmlTextReader ("Bad_example.xsd");
XmlSchema myschema = XmlSchema.Read(reader, new ValidationEventHandler(ValidationCallbackOne));
myschema.Write(Console.Out);
FileStream file = new FileStream ("New.xsd", FileMode.Create, FileAccess.ReadWrite);
XmlTextWriter xwriter = new XmlTextWriter (file, new UTF8Encoding());
xwriter.Formatting = Formatting.Indented;
myschema.Write (xwriter);
}catch(Exception e){
Console.WriteLine(e);
}
}/* Main() */
}//ReadWriteSample
次の XML スキーマで、入力ファイル Bad_example.xsd の内容の概要を示します。
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="Play"
targetNamespace="http://tempuri.org/Play.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/Play.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name='myShoeSize'>
<xs:complexType>
<xs:extension base='xs:decimal'>
<xs:attribute name='sizing' type='xs:string' />
<xs:extension />
</xs:extension>
</xs:complexType>
</xs:element>
<player></player>
</xs:schema>