2 // XmlSchemaValidatorTests.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // (C) 2008 Novell Inc.
13 using System.Collections;
16 using System.Xml.Schema;
17 using NUnit.Framework;
19 namespace MonoTests.System.Xml
22 public class XmlSchemaValidatorTests
24 void Validate (string xml, string xsd)
26 XmlSchema schema = XmlSchema.Read (new StringReader (xsd), null);
27 XmlReaderSettings settings = new XmlReaderSettings ();
28 settings.ValidationType = ValidationType.Schema;
29 settings.Schemas.Add (schema);
30 XmlReader reader = XmlReader.Create (new StringReader (xml), settings);
31 while (reader.Read ())
36 public void XsdAnyToSkipAttributeValidation ()
39 XmlSchemaSet schemas = new XmlSchemaSet ();
40 schemas.Add (null, "Test/XmlFiles/xsd/358408.xsd");
41 XmlSchemaValidator v = new XmlSchemaValidator (
44 new XmlNamespaceManager (new NameTable ()),
45 XmlSchemaValidationFlags.ProcessIdentityConstraints);
47 v.ValidateWhitespace (" ");
48 XmlSchemaInfo info = new XmlSchemaInfo ();
49 ArrayList list = new ArrayList ();
51 v.ValidateElement ("configuration", "", info, null, null, null, null);
52 v.GetUnspecifiedDefaultAttributes (list);
53 v.ValidateEndOfAttributes (info);
55 v.ValidateWhitespace (" ");
57 v.ValidateElement ("host", "", info, null, null, null, null);
58 v.ValidateAttribute ("auto-start", "", "true", info);
60 v.GetUnspecifiedDefaultAttributes (list);
61 v.ValidateEndOfAttributes (info);
62 v.ValidateEndElement (null);//info);
64 v.ValidateWhitespace (" ");
66 v.ValidateElement ("service-managers", "", info, null, null, null, null);
68 v.GetUnspecifiedDefaultAttributes (list);
69 v.ValidateEndOfAttributes (info);
71 v.ValidateWhitespace (" ");
73 v.ValidateElement ("service-manager", "", info, null, null, null, null);
75 v.GetUnspecifiedDefaultAttributes (list);
76 v.ValidateEndOfAttributes (info);
78 v.ValidateWhitespace (" ");
80 v.ValidateElement ("foo", "", info, null, null, null, null);
81 v.ValidateAttribute ("bar", "", "", info);
85 public void SkipInvolved () // bug #422581
87 XmlReader schemaReader = XmlReader.Create ("Test/XmlFiles/xsd/422581.xsd");
88 XmlSchema schema = XmlSchema.Read (schemaReader, null);
89 XmlReaderSettings settings = new XmlReaderSettings ();
90 settings.ValidationType = ValidationType.Schema;
91 settings.Schemas.Add (schema);
92 XmlReader reader = XmlReader.Create ("Test/XmlFiles/xsd/422581.xml", settings);
93 while (reader.Read ());
97 public void Bug433774 ()
99 string xsd = @"<xs:schema targetNamespace='urn:foo' xmlns='urn:foo' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
100 <xs:element name='Root'>
102 <xs:sequence></xs:sequence>
103 <xs:attribute name='version' type='xs:string' fixed='3' />
107 XmlDocument doc = new XmlDocument ();
108 doc.LoadXml ("<Root version='3' xmlns='urn:foo'/>");
109 XmlSchemaSet schemaSet = new XmlSchemaSet();
110 schemaSet.Add (XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null));
111 doc.Schemas = schemaSet;
112 XmlNode root = doc.DocumentElement;
113 doc.Validate (null, root);
117 [ExpectedException (typeof (XmlSchemaValidationException))]
118 public void Bug435206 ()
120 string xsd = @"<xs:schema attributeFormDefault='unqualified' elementFormDefault='qualified' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
121 <xs:element name='myDoc'>
123 <xs:attribute name='foo' type='xs:unsignedLong' use='required' />
124 <xs:attribute name='bar' type='xs:dateTime' use='required' />
128 string xml = @"<myDoc foo='12' bar='January 1st 1900'/>";
133 public void Bug469713 ()
135 string xsd = @"<xs:schema elementFormDefault='qualified' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
136 <xs:element name='Message'>
139 <xs:element name='MyDateTime' nillable='true' type='xs:dateTime' />
144 string xml = @"<Message xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='test.xsd'>
145 <MyDateTime xsi:nil='true'></MyDateTime>
151 public void Bug496192_496205 ()
153 using (var xmlr = new StreamReader ("Test/XmlFiles/496192.xml"))
154 using (var xsdr = new StreamReader ("Test/XmlFiles/496192.xsd"))
155 Validate (xmlr.ReadToEnd (), xsdr.ReadToEnd ());
159 public void Bug501666 ()
162 <xs:schema id='Settings'
163 targetNamespace='foo'
165 xmlns:xs='http://www.w3.org/2001/XMLSchema'>
167 <xs:element name='Settings' type='Settings'/>
169 <xs:complexType name='Settings'>
170 <xs:attribute name='port' type='PortNumber' use='required'/>
173 <xs:simpleType name='PortNumber'>
174 <xs:restriction base='xs:positiveInteger'>
175 <xs:minInclusive value='1'/>
176 <xs:maxInclusive value='65535'/>
181 string xml = @"<Settings port='1337' xmlns='foo'/>";
183 XmlDocument doc = new XmlDocument ();
185 doc.Schemas.Add (XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null));
189 public void Bug502251 ()
192 <xs:schema id='foo' targetNamespace='foo'
193 elementFormDefault='qualified'
195 xmlns:xs='http://www.w3.org/2001/XMLSchema'>
197 <xs:group name='LayoutElementTypes'>
199 <xs:element name='Rows' type='Rows' />
200 <xs:element name='Conditional' type='Conditional' />
204 <xs:complexType name='Element' abstract='true'>
205 <xs:attribute name='id' type='xs:ID' use='optional'/>
208 <xs:complexType name='SingleChildElement' abstract='true'>
210 <xs:extension base='Element'>
211 <xs:group ref='LayoutElementTypes' minOccurs='1' maxOccurs='1' />
216 <xs:complexType name='Rows'>
218 <xs:extension base='Element'>
219 <xs:sequence minOccurs='1' maxOccurs='unbounded'>
220 <xs:element name='Row' type='Row' />
226 <xs:complexType name='Row'>
228 <xs:extension base='SingleChildElement'>
233 <xs:complexType name='Conditional'>
235 <xs:extension base='Element'>
240 <xs:complexType name='Layout'>
242 <xs:extension base='SingleChildElement'>
247 <xs:element name='Layout' type='Layout' />
250 XmlDocument doc = new XmlDocument ();
251 doc.LoadXml (@"<Layout xmlns='foo'>
253 <Row><Conditional/></Row>
257 XmlSchema schema = XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null);
259 doc.Schemas.Add (schema);
264 public void Bug557452 ()
267 <xs:schema id='Settings'
268 targetNamespace='foo'
270 xmlns:xs='http://www.w3.org/2001/XMLSchema'>
272 <xs:element name='Settings' type='Settings'/>
274 <xs:complexType name='Settings'>
275 <xs:attribute name='port' type='PortNumber' use='required'/>
278 <xs:simpleType name='PortNumber'>
279 <xs:restriction base='xs:decimal'>
280 <xs:minInclusive value='1'/>
281 <xs:maxInclusive value='65535'/>
286 string xml = @"<Settings port='1337' xmlns='foo'/>";
288 XmlDocument doc = new XmlDocument ();
290 doc.Schemas.Add (XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null));
295 public void Bug584664 ()
297 Validate (File.ReadAllText ("Test/XmlFiles/xsd/584664a.xml"), File.ReadAllText ("Test/XmlFiles/xsd/584664a.xsd"));
298 Validate (File.ReadAllText ("Test/XmlFiles/xsd/584664b.xml"), File.ReadAllText ("Test/XmlFiles/xsd/584664b.xsd"));
302 public void MultipleMissingIds ()
304 var schema = XmlSchema.Read (new StringReader (@"<?xml version=""1.0"" encoding=""utf-8""?>
305 <xs:schema targetNamespace=""urn:multiple-ids"" elementFormDefault=""qualified"" xmlns=""urn:multiple-ids"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
306 <xs:element name=""root"">
308 <xs:sequence minOccurs=""0"" maxOccurs=""unbounded"">
309 <xs:element name=""item"">
311 <xs:attribute name=""id"" type=""xs:ID"" />
312 <xs:attribute name=""parent"" type=""xs:IDREF"" />
318 </xs:schema>"), null);
319 var xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
320 <root xmlns=""urn:multiple-ids"">
321 <item id=""id2"" parent=""id1"" />
322 <item id=""id3"" parent=""id1"" />
323 <item id=""id1"" parent=""id1"" />
325 var document = new XmlDocument ();
326 document.LoadXml (xml);
327 document.Schemas = new XmlSchemaSet ();
328 document.Schemas.Add (schema);
329 document.Validate (null);