Дано: схема 1:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
<?xml version="1.0" encoding="utf-8"?>
<xs:schema
attributeFormDefault="unqualified"
elementFormDefault="qualified"
targetNamespace="urn:print-settings"
xmlns="urn:print-settings"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="PrintSettingsType">
<xs:sequence>
<xs:element name="DeviceInfo" type="DeviceInfoType" minOccurs="0" />
<xs:element name="Landscape" type="xs:string" minOccurs="0" />
<xs:element name="PrinterName" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="DeviceInfoType">
<xs:sequence>
<xs:element name="OutputFormat" type="xs:string" minOccurs="0" />
<xs:element name="PageWidth" type="xs:string" minOccurs="0" />
<xs:element name="PageHeight" type="xs:string" minOccurs="0" />
<xs:element name="MarginTop" type="xs:string" minOccurs="0" />
<xs:element name="MarginLeft" type="xs:string" minOccurs="0" />
<xs:element name="MarginRight" type="xs:string" minOccurs="0" />
<xs:element name="MarginBottom" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:schema>
Схема 2, импортирующая схему 1:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
<?xml version="1.0" encoding="utf-8"?>
<xs:schema
attributeFormDefault="unqualified"
elementFormDefault="qualified"
targetNamespace="urn:plugin-data"
xmlns="urn:plugin-data"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:p="urn:print-settings">
<xs:import namespace="urn:print-settings" schemaLocation="file:///D:/Projects/.Net/_tests/test/test/PrintSettings.xsd" />
<xs:element name="plugin" type="PluginType" />
<xs:complexType name="ItemType">
<xs:sequence minOccurs="0" maxOccurs="1">
<xs:element name="PrintSettings" type="p:PrintSettingsType" minOccurs="0" maxOccurs="1" />
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="description" type="xs:string" use="required" />
<xs:attribute name="printform" type="xs:string" />
</xs:complexType>
<xs:complexType name="FolderType">
<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:element name="folder" type="FolderType" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="item" type="ItemType" minOccurs="0" maxOccurs="unbounded" />
</xs:choice>
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="description" type="xs:string" use="required" />
</xs:complexType>
<xs:complexType name="MenuDataType">
<xs:sequence minOccurs="1" maxOccurs="1">
<xs:element name="folder" type="FolderType" minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="PluginType">
<xs:sequence minOccurs="1" maxOccurs="1">
<xs:element name="PrintSettings" type="p:PrintSettingsType" minOccurs="0" maxOccurs="1" />
<xs:element name="MenuData" type="MenuDataType" minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:schema>
XML для валидации:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="urn:plugin-data" xmlns:ps="urn:print-settings">
<ps:PrintSettings>
<DeviceInfo>
<OutputFormat>EMF</OutputFormat>
<PageWidth>8.27in</PageWidth>
<PageHeight>11.69in</PageHeight>
<MarginTop>0.25in</MarginTop>
<MarginLeft>0.25in</MarginLeft>
<MarginRight>0.25in</MarginRight>
<MarginBottom>0.25in</MarginBottom>
</DeviceInfo>
<Landscape>False</Landscape>
<PrinterName>HP LaserJet 3052 PCL5</PrinterName>
</ps:PrintSettings>
<MenuData>
<folder name="root" description="Корень">
<item name="view" description="Просмотр" />
<item name="print" description="Печать">
<ps:PrintSettings>
<DeviceInfo>
<OutputFormat>EMF</OutputFormat>
<PageWidth>8.27in</PageWidth>
<PageHeight>11.69in</PageHeight>
<MarginTop>0.25in</MarginTop>
<MarginLeft>0.25in</MarginLeft>
<MarginRight>0.25in</MarginRight>
<MarginBottom>0.25in</MarginBottom>
</DeviceInfo>
<Landscape>False</Landscape>
<PrinterName>HP LaserJet 3052 PCL5</PrinterName>
</ps:PrintSettings>
</item>
</folder>
</MenuData>
</plugin>
Валидируем документ при загрузке (C#):
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
class Program
{
static void Main(string[] args)
{
var xrs = new XmlReaderSettings();
using (var fs = new FileStream(@"D:\Projects\.Net\_tests\test\test\PluginData.xsd", FileMode.Open)) // Схема 2
using (var xr = XmlReader.Create(fs))
xrs.Schemas.Add("urn:plugin-data", xr);
xrs.ValidationEventHandler += ValidationEventHandler;
xrs.ValidationFlags = xrs.ValidationFlags | XmlSchemaValidationFlags.ReportValidationWarnings;
xrs.ValidationType = ValidationType.Schema;
var xd = new XmlDocument();
using (var fs = new FileStream(@"D:\Projects\.Net\_tests\test\test\plugin.menudata", FileMode.Open))
using (var xr = XmlReader.Create(fs, xrs))
xd.Load(xr);
Console.WriteLine("done");
Console.ReadKey(true);
}
static void ValidationEventHandler(object sender, ValidationEventArgs args)
{
switch (args.Severity)
{
case XmlSeverityType.Warning:
Console.Write("\nWARNING: ");
break;
case XmlSeverityType.Error:
Console.Write("\nERROR: ");
break;
}
Console.WriteLine(args.Message);
}
}
и в консоли получаем ошибку валидации:
1.
ERROR: Элемент "plugin" в пространстве имен "urn:plugin-data" имеет недопустимый дочерний элемент "PrintSettings" в пространстве имен "urn:print-settings".
Список ожидаемых элементов: "PrintSettings, MenuData" в пространстве имен "urn:plugin-data".
Опытным путем установлено, что валидатор видит, и загружает импортируемую схему (например, если в schemaLocation задать неверный путь, то при загрузке документа вылетит XmlSchemaValidationException: Тип "urn:print-settings:PrintSettingsType" не объявлен).
В чем может быть причина ошибки валидации?