XmlSchemaElement.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  1. //
  2. // System.Xml.Schema.XmlSchemaElement.cs
  3. //
  4. // Authors:
  5. // Dwivedi, Ajay kumar [email protected]
  6. // Enomoto, Atsushi [email protected]
  7. //
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Xml;
  31. using System.Xml.Serialization;
  32. using System.ComponentModel;
  33. namespace System.Xml.Schema
  34. {
  35. /// <summary>
  36. /// Summary description for XmlSchemaElement.
  37. /// </summary>
  38. public class XmlSchemaElement : XmlSchemaParticle
  39. {
  40. private XmlSchemaDerivationMethod block;
  41. private XmlSchemaObjectCollection constraints;
  42. private string defaultValue;
  43. private object elementType;
  44. private XmlSchemaDerivationMethod final;
  45. private string fixedValue;
  46. private XmlSchemaForm form;
  47. private bool isAbstract;
  48. private bool isNillable;
  49. private string name;
  50. private XmlQualifiedName refName;
  51. private XmlSchemaType schemaType;
  52. private XmlQualifiedName schemaTypeName;
  53. private XmlQualifiedName substitutionGroup;
  54. // Post compilation items.
  55. XmlSchema schema;
  56. internal bool parentIsSchema = false;
  57. private XmlQualifiedName qName;
  58. private XmlSchemaDerivationMethod blockResolved;
  59. private XmlSchemaDerivationMethod finalResolved;
  60. private XmlSchemaParticle substChoice;
  61. private XmlSchemaElement referencedElement;
  62. private ArrayList substitutingElements = new ArrayList ();
  63. private XmlSchemaElement substitutionGroupElement;
  64. private bool actualIsAbstract;
  65. private bool actualIsNillable;
  66. private string validatedDefaultValue;
  67. private string validatedFixedValue;
  68. const string xmlname = "element";
  69. public XmlSchemaElement()
  70. {
  71. block = XmlSchemaDerivationMethod.None;
  72. final = XmlSchemaDerivationMethod.None;
  73. constraints = new XmlSchemaObjectCollection();
  74. refName = XmlQualifiedName.Empty;
  75. schemaTypeName = XmlQualifiedName.Empty;
  76. substitutionGroup = XmlQualifiedName.Empty;
  77. InitPostCompileInformations ();
  78. }
  79. private void InitPostCompileInformations ()
  80. {
  81. qName = XmlQualifiedName.Empty;
  82. schema = null;
  83. // parentIsSchema = false; ... it is set in Schema's Compile()
  84. blockResolved = XmlSchemaDerivationMethod.None;
  85. finalResolved = XmlSchemaDerivationMethod.None;
  86. substChoice = null;
  87. referencedElement = null;
  88. substitutingElements.Clear ();
  89. substitutionGroupElement = null;
  90. actualIsAbstract = false;
  91. actualIsNillable = false;
  92. validatedDefaultValue = null;
  93. validatedFixedValue = null;
  94. }
  95. #region Attributes
  96. [DefaultValue(false)]
  97. [System.Xml.Serialization.XmlAttribute("abstract")]
  98. public bool IsAbstract
  99. {
  100. get{ return isAbstract; }
  101. set{ isAbstract = value; }
  102. }
  103. [DefaultValue(XmlSchemaDerivationMethod.None)]
  104. [System.Xml.Serialization.XmlAttribute("block")]
  105. public XmlSchemaDerivationMethod Block
  106. {
  107. get{ return block; }
  108. set{ block = value; }
  109. }
  110. [DefaultValue(null)]
  111. [System.Xml.Serialization.XmlAttribute("default")]
  112. public string DefaultValue
  113. {
  114. get{ return defaultValue; }
  115. set{ defaultValue = value; }
  116. }
  117. [DefaultValue(XmlSchemaDerivationMethod.None)]
  118. [System.Xml.Serialization.XmlAttribute("final")]
  119. public XmlSchemaDerivationMethod Final
  120. {
  121. get{ return final; }
  122. set{ final = value; }
  123. }
  124. [DefaultValue(null)]
  125. [System.Xml.Serialization.XmlAttribute("fixed")]
  126. public string FixedValue
  127. {
  128. get{ return fixedValue; }
  129. set{ fixedValue = value; }
  130. }
  131. [DefaultValue(XmlSchemaForm.None)]
  132. [System.Xml.Serialization.XmlAttribute("form")]
  133. public XmlSchemaForm Form
  134. {
  135. get{ return form; }
  136. set{ form = value; }
  137. }
  138. [DefaultValue(null)]
  139. [System.Xml.Serialization.XmlAttribute("name")]
  140. public string Name
  141. {
  142. get{ return name; }
  143. set{ name = value; }
  144. }
  145. [DefaultValue(false)]
  146. [System.Xml.Serialization.XmlAttribute("nillable")]
  147. public bool IsNillable
  148. {
  149. get{ return isNillable; }
  150. set{ isNillable = value; }
  151. }
  152. [System.Xml.Serialization.XmlAttribute("ref")]
  153. public XmlQualifiedName RefName
  154. {
  155. get{ return refName; }
  156. set{ refName = value;}
  157. }
  158. [System.Xml.Serialization.XmlAttribute("substitutionGroup")]
  159. public XmlQualifiedName SubstitutionGroup
  160. {
  161. get{ return substitutionGroup; }
  162. set{ substitutionGroup = value; }
  163. }
  164. [System.Xml.Serialization.XmlAttribute("type")]
  165. public XmlQualifiedName SchemaTypeName
  166. {
  167. get{ return schemaTypeName; }
  168. set{ schemaTypeName = value; }
  169. }
  170. #endregion
  171. #region Elements
  172. [XmlElement("simpleType",typeof(XmlSchemaSimpleType),Namespace="http://www.w3.org/2001/XMLSchema")]
  173. [XmlElement("complexType",typeof(XmlSchemaComplexType),Namespace="http://www.w3.org/2001/XMLSchema")]
  174. public XmlSchemaType SchemaType
  175. {
  176. get{ return schemaType; }
  177. set{ schemaType = value; }
  178. }
  179. [XmlElement("unique",typeof(XmlSchemaUnique),Namespace="http://www.w3.org/2001/XMLSchema")]
  180. [XmlElement("key",typeof(XmlSchemaKey),Namespace="http://www.w3.org/2001/XMLSchema")]
  181. [XmlElement("keyref",typeof(XmlSchemaKeyref),Namespace="http://www.w3.org/2001/XMLSchema")]
  182. public XmlSchemaObjectCollection Constraints
  183. {
  184. get{ return constraints; }
  185. }
  186. #endregion
  187. #region Post Compilation Schema Info
  188. [XmlIgnore]
  189. public XmlQualifiedName QualifiedName
  190. {
  191. get{ return qName; }
  192. }
  193. [XmlIgnore]
  194. public object ElementType
  195. {
  196. get {
  197. if (referencedElement != null)
  198. return referencedElement.ElementType;
  199. else
  200. return elementType;
  201. }
  202. }
  203. #if NET_2_0
  204. [XmlIgnore]
  205. public XmlSchemaType ElementSchemaType
  206. {
  207. get {
  208. if (referencedElement != null)
  209. return referencedElement.ElementSchemaType;
  210. XmlSchemaDatatype dt = elementType as XmlSchemaDatatype;
  211. if (dt != null)
  212. return XmlSchemaType.GetBuiltInSimpleType (dt);
  213. else
  214. return elementType as XmlSchemaType;
  215. }
  216. }
  217. #endif
  218. [XmlIgnore]
  219. public XmlSchemaDerivationMethod BlockResolved
  220. {
  221. get{
  222. if (referencedElement != null)
  223. return referencedElement.BlockResolved;
  224. else
  225. return blockResolved;
  226. }
  227. }
  228. [XmlIgnore]
  229. public XmlSchemaDerivationMethod FinalResolved
  230. {
  231. get{
  232. if (referencedElement != null)
  233. return referencedElement.FinalResolved;
  234. else
  235. return finalResolved;
  236. }
  237. }
  238. internal bool ActualIsNillable {
  239. get {
  240. if (referencedElement != null)
  241. return referencedElement.ActualIsNillable;
  242. else
  243. return actualIsNillable;
  244. }
  245. }
  246. internal bool ActualIsAbstract {
  247. get {
  248. if (referencedElement != null)
  249. return referencedElement.ActualIsAbstract;
  250. else
  251. return actualIsAbstract;
  252. }
  253. }
  254. // Post compilation default value (normalized)
  255. internal string ValidatedDefaultValue {
  256. get{
  257. if (referencedElement != null)
  258. return referencedElement.ValidatedDefaultValue;
  259. else
  260. return validatedDefaultValue;
  261. }
  262. }
  263. // Post compilation fixed value (normalized)
  264. internal string ValidatedFixedValue {
  265. get{
  266. if (referencedElement != null)
  267. return referencedElement.ValidatedFixedValue;
  268. else
  269. return validatedFixedValue;
  270. }
  271. }
  272. internal ArrayList SubstitutingElements {
  273. get {
  274. if (referencedElement != null)
  275. return referencedElement.SubstitutingElements;
  276. else
  277. return this.substitutingElements;
  278. }
  279. }
  280. internal XmlSchemaElement SubstitutionGroupElement {
  281. get {
  282. if (referencedElement != null)
  283. return referencedElement.SubstitutionGroupElement;
  284. else
  285. return substitutionGroupElement;
  286. }
  287. }
  288. #endregion
  289. /// <remarks>
  290. /// a) If Element has parent as schema:
  291. /// 1. name must be present and of type NCName.
  292. /// 2. ref must be absent
  293. /// 3. form must be absent
  294. /// 4. minOccurs must be absent
  295. /// 5. maxOccurs must be absent
  296. /// b) If Element has parent is not schema and ref is absent
  297. /// 1. name must be present and of type NCName.
  298. /// 2. if form equals qualified or form is absent and schema's formdefault is qualifed,
  299. /// targetNamespace is schema's targetnamespace else empty.
  300. /// 3. type and either <simpleType> or <complexType> are mutually exclusive
  301. /// 4. default and fixed must not both be present.
  302. /// 5. substitutiongroup must be absent
  303. /// 6. final must be absent
  304. /// 7. abstract must be absent
  305. /// c) if the parent is not schema and ref is set
  306. /// 1. name must not be present
  307. /// 2. all of <simpleType>,<complexType>, <key>, <keyref>, <unique>, nillable,
  308. /// default, fixed, form, block and type, must be absent.
  309. /// 3. substitutiongroup is prohibited
  310. /// 4. final is prohibited
  311. /// 5. abstract is prohibited
  312. /// 6. default and fixed must not both be present.(Actually both are absent)
  313. /// </remarks>
  314. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  315. {
  316. // If this is already compiled this time, simply skip.
  317. if (this.IsComplied (schema.CompilationId))
  318. return 0;
  319. InitPostCompileInformations ();
  320. this.schema = schema;
  321. if(this.defaultValue != null && this.fixedValue != null)
  322. error(h,"both default and fixed can't be present");
  323. if(parentIsSchema || isRedefineChild)
  324. {
  325. if(this.refName != null && !RefName.IsEmpty)
  326. error(h,"ref must be absent");
  327. if(this.name == null) //b1
  328. error(h,"Required attribute name must be present");
  329. else if(!XmlSchemaUtil.CheckNCName(this.name)) // b1.2
  330. error(h,"attribute name must be NCName");
  331. else
  332. this.qName = new XmlQualifiedName (this.name, schema.TargetNamespace);
  333. if(form != XmlSchemaForm.None)
  334. error(h,"form must be absent");
  335. if(MinOccursString != null)
  336. error(h,"minOccurs must be absent");
  337. if(MaxOccursString != null)
  338. error(h,"maxOccurs must be absent");
  339. XmlSchemaDerivationMethod allfinal = (XmlSchemaDerivationMethod.Extension | XmlSchemaDerivationMethod.Restriction);
  340. if(final == XmlSchemaDerivationMethod.All)
  341. finalResolved = allfinal;
  342. else if(final == XmlSchemaDerivationMethod.None)
  343. finalResolved = XmlSchemaDerivationMethod.Empty;
  344. else
  345. {
  346. // if((final & ~allfinal) != 0)
  347. if ((final | XmlSchemaUtil.FinalAllowed) != XmlSchemaUtil.FinalAllowed)
  348. error (h,"some values for final are invalid in this context");
  349. finalResolved = final & allfinal;
  350. }
  351. if(schemaType != null && schemaTypeName != null && !schemaTypeName.IsEmpty)
  352. {
  353. error(h,"both schemaType and content can't be present");
  354. }
  355. //Even if both are present, read both of them.
  356. if(schemaType != null)
  357. {
  358. if(schemaType is XmlSchemaSimpleType)
  359. {
  360. errorCount += ((XmlSchemaSimpleType)schemaType).Compile(h,schema);
  361. }
  362. else if(schemaType is XmlSchemaComplexType)
  363. {
  364. errorCount += ((XmlSchemaComplexType)schemaType).Compile(h,schema);
  365. }
  366. else
  367. error(h,"only simpletype or complextype is allowed");
  368. }
  369. if(schemaTypeName != null && !schemaTypeName.IsEmpty)
  370. {
  371. if(!XmlSchemaUtil.CheckQName(SchemaTypeName))
  372. error(h,"SchemaTypeName must be an XmlQualifiedName");
  373. }
  374. if(SubstitutionGroup != null && !SubstitutionGroup.IsEmpty)
  375. {
  376. if(!XmlSchemaUtil.CheckQName(SubstitutionGroup))
  377. error(h,"SubstitutionGroup must be a valid XmlQualifiedName");
  378. }
  379. foreach(XmlSchemaObject obj in constraints)
  380. {
  381. if(obj is XmlSchemaUnique)
  382. errorCount += ((XmlSchemaUnique)obj).Compile(h,schema);
  383. else if(obj is XmlSchemaKey)
  384. errorCount += ((XmlSchemaKey)obj).Compile(h,schema);
  385. else if(obj is XmlSchemaKeyref)
  386. errorCount += ((XmlSchemaKeyref)obj).Compile(h,schema);
  387. }
  388. }
  389. else
  390. {
  391. if(substitutionGroup != null && !substitutionGroup.IsEmpty)
  392. error(h,"substitutionGroup must be absent");
  393. if(final != XmlSchemaDerivationMethod.None)
  394. error(h,"final must be absent");
  395. CompileOccurence (h, schema);
  396. if(refName == null || RefName.IsEmpty)
  397. {
  398. string targetNamespace = String.Empty;
  399. if(form == XmlSchemaForm.Qualified || (form == XmlSchemaForm.None && schema.ElementFormDefault == XmlSchemaForm.Qualified))
  400. targetNamespace = schema.TargetNamespace;
  401. if(this.name == null) //b1
  402. error(h,"Required attribute name must be present");
  403. else if(!XmlSchemaUtil.CheckNCName(this.name)) // b1.2
  404. error(h,"attribute name must be NCName");
  405. else
  406. this.qName = new XmlQualifiedName(this.name, targetNamespace);
  407. if(schemaType != null && schemaTypeName != null && !schemaTypeName.IsEmpty)
  408. {
  409. error(h,"both schemaType and content can't be present");
  410. }
  411. //Even if both are present, read both of them.
  412. if(schemaType != null)
  413. {
  414. if(schemaType is XmlSchemaSimpleType)
  415. {
  416. errorCount += ((XmlSchemaSimpleType)schemaType).Compile(h,schema);
  417. }
  418. else if(schemaType is XmlSchemaComplexType)
  419. {
  420. errorCount += ((XmlSchemaComplexType)schemaType).Compile(h,schema);
  421. }
  422. else
  423. error(h,"only simpletype or complextype is allowed");
  424. }
  425. if(schemaTypeName != null && !schemaTypeName.IsEmpty)
  426. {
  427. if(!XmlSchemaUtil.CheckQName(SchemaTypeName))
  428. error(h,"SchemaTypeName must be an XmlQualifiedName");
  429. }
  430. if(SubstitutionGroup != null && !SubstitutionGroup.IsEmpty)
  431. {
  432. if(!XmlSchemaUtil.CheckQName(SubstitutionGroup))
  433. error(h,"SubstitutionGroup must be a valid XmlQualifiedName");
  434. }
  435. foreach(XmlSchemaObject obj in constraints)
  436. {
  437. if(obj is XmlSchemaUnique)
  438. errorCount += ((XmlSchemaUnique)obj).Compile(h,schema);
  439. else if(obj is XmlSchemaKey)
  440. errorCount += ((XmlSchemaKey)obj).Compile(h,schema);
  441. else if(obj is XmlSchemaKeyref)
  442. errorCount += ((XmlSchemaKeyref)obj).Compile(h,schema);
  443. }
  444. }
  445. else
  446. {
  447. if(!XmlSchemaUtil.CheckQName(RefName))
  448. error(h,"RefName must be a XmlQualifiedName");
  449. if(name != null)
  450. error(h,"name must not be present when ref is present");
  451. if(Constraints.Count != 0)
  452. error(h,"key, keyref and unique must be absent");
  453. if(isNillable)
  454. error(h,"nillable must be absent");
  455. if(defaultValue != null)
  456. error(h,"default must be absent");
  457. if(fixedValue != null)
  458. error(h,"fixed must be null");
  459. if(form != XmlSchemaForm.None)
  460. error(h,"form must be absent");
  461. if(block != XmlSchemaDerivationMethod.None)
  462. error(h,"block must be absent");
  463. if(schemaTypeName != null && !schemaTypeName.IsEmpty)
  464. error(h,"type must be absent");
  465. if(SchemaType != null)
  466. error(h,"simpleType or complexType must be absent");
  467. qName = RefName;
  468. }
  469. }
  470. switch (block) {
  471. case XmlSchemaDerivationMethod.All:
  472. blockResolved = XmlSchemaDerivationMethod.All;
  473. break;
  474. case XmlSchemaDerivationMethod.None:
  475. blockResolved = XmlSchemaDerivationMethod.Empty;
  476. break;
  477. default:
  478. if ((block | XmlSchemaUtil.ElementBlockAllowed) != XmlSchemaUtil.ElementBlockAllowed)
  479. error (h,"Some of the values for block are invalid in this context");
  480. blockResolved = block;
  481. break;
  482. }
  483. if (Constraints != null) {
  484. XmlSchemaObjectTable table = new XmlSchemaObjectTable ();
  485. foreach (XmlSchemaIdentityConstraint c in Constraints) {
  486. XmlSchemaUtil.AddToTable (table, c, c.QualifiedName, h);
  487. }
  488. }
  489. XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);
  490. this.CompilationId = schema.CompilationId;
  491. return errorCount;
  492. }
  493. [MonoTODO ("Return clone in case when it returns itself")]
  494. internal override XmlSchemaParticle GetOptimizedParticle (bool isTop)
  495. {
  496. if (OptimizedParticle != null)
  497. return OptimizedParticle;
  498. if (RefName != null && RefName != XmlQualifiedName.Empty) {
  499. referencedElement = schema.Elements [RefName] as XmlSchemaElement;
  500. }
  501. // if (this.referencedElement != null)
  502. // OptimizedParticle = referencedElement.GetOptimizedParticle (isTop);
  503. // else
  504. if (ValidatedMaxOccurs == 0)
  505. OptimizedParticle = XmlSchemaParticle.Empty;
  506. // Substitution Group
  507. else if (SubstitutingElements != null && SubstitutingElements.Count > 0) {
  508. XmlSchemaChoice choice = new XmlSchemaChoice ();
  509. choice.MinOccurs = MinOccurs;
  510. choice.MaxOccurs = MaxOccurs;
  511. substChoice = choice;
  512. choice.Compile (null, schema); // compute Validated Min/Max Occurs.
  513. XmlSchemaElement item = this.MemberwiseClone () as XmlSchemaElement;
  514. item.MinOccurs = 1;
  515. item.MaxOccurs = 1;
  516. item.substitutionGroupElement = null;
  517. item.substitutingElements = null;
  518. for (int i = 0; i < SubstitutingElements.Count; i++) {
  519. XmlSchemaElement se = SubstitutingElements [i] as XmlSchemaElement;
  520. // choice.Items.Add (se);
  521. // choice.CompiledItems.Add (se);
  522. this.AddSubstElementRecursively (choice.Items, se);
  523. this.AddSubstElementRecursively (choice.CompiledItems, se);
  524. }
  525. if (!choice.Items.Contains (item)) {
  526. choice.Items.Add (item);
  527. choice.CompiledItems.Add (item);
  528. }
  529. OptimizedParticle = choice;
  530. }
  531. else
  532. OptimizedParticle = this;//.MemberwiseClone () as XmlSchemaElement;
  533. return OptimizedParticle;
  534. }
  535. private void AddSubstElementRecursively (XmlSchemaObjectCollection col, XmlSchemaElement el)
  536. {
  537. if (el.SubstitutingElements != null)
  538. for (int i = 0; i < el.SubstitutingElements.Count; i++)
  539. this.AddSubstElementRecursively (col, el.SubstitutingElements [i] as XmlSchemaElement);
  540. if (!col.Contains (el))
  541. col.Add (el);
  542. }
  543. internal void FillSubstitutionElementInfo ()
  544. {
  545. if (this.substitutionGroupElement != null)
  546. return;
  547. if (this.SubstitutionGroup != XmlQualifiedName.Empty) {
  548. XmlSchemaElement substElem = schema.Elements [SubstitutionGroup] as XmlSchemaElement;
  549. this.substitutionGroupElement = substElem;
  550. if (substElem != null)
  551. substElem.substitutingElements.Add (this);
  552. }
  553. }
  554. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  555. {
  556. if (IsValidated (schema.CompilationId))
  557. return errorCount;
  558. // See XML Schema Structures 3.6 for the complete description.
  559. // Element Declaration Properties Correct
  560. // 1. = 3.3.1 (modulo 5.3)
  561. // 3.3.1:
  562. // {annotation} is as is.
  563. // {name}, {target namespace}, {scope}, {disallowed substitution},
  564. // {substitution group exclusions} (handled the same as 'disallowed substitution')
  565. // and {identity-constraint-definitions} are Compile()d.
  566. // {value constraint} is going to be filled in step 2.
  567. // actual {nillable}, {abstract}
  568. this.actualIsNillable = IsNillable;
  569. this.actualIsAbstract = IsAbstract;
  570. // Before determining element type, we need to validate substituting element
  571. if (this.SubstitutionGroup != XmlQualifiedName.Empty) {
  572. XmlSchemaElement substElem = substitutionGroupElement;
  573. if (substElem != null)
  574. substElem.Validate (h, schema);
  575. }
  576. // {type} from here
  577. XmlSchemaDatatype datatype = null;
  578. if (schemaType != null)
  579. elementType = schemaType;
  580. else if (SchemaTypeName != XmlQualifiedName.Empty) {
  581. XmlSchemaType type = schema.SchemaTypes [SchemaTypeName] as XmlSchemaType;
  582. if (type != null) {
  583. type.Validate (h, schema);
  584. elementType = type;
  585. }
  586. else if (SchemaTypeName == XmlSchemaComplexType.AnyTypeName)
  587. elementType = XmlSchemaComplexType.AnyType;
  588. else if (XmlSchemaUtil.IsBuiltInDatatypeName (SchemaTypeName)) {
  589. datatype = XmlSchemaDatatype.FromName (SchemaTypeName);
  590. if (datatype == null)
  591. error (h, "Invalid schema datatype was specified.");
  592. else
  593. elementType = datatype;
  594. }
  595. // otherwise, it might be missing sub components.
  596. else if (!schema.IsNamespaceAbsent (SchemaTypeName.Namespace))
  597. error (h, "Referenced element schema type " + SchemaTypeName + " was not found in the corresponding schema.");
  598. }
  599. else if (RefName != XmlQualifiedName.Empty)
  600. {
  601. XmlSchemaElement refElem = schema.Elements [RefName] as XmlSchemaElement;
  602. // If el is null, then it is missing sub components .
  603. if (refElem != null) {
  604. this.referencedElement = refElem;
  605. errorCount += refElem.Validate (h, schema);
  606. }
  607. // otherwise, it might be missing sub components.
  608. else if (!schema.IsNamespaceAbsent (RefName.Namespace))
  609. error (h, "Referenced element " + RefName + " was not found in the corresponding schema.");
  610. }
  611. // Otherwise if there are substitution group, then the type of the substitution group element.
  612. if (referencedElement == null) {
  613. if (elementType == null && this.substitutionGroupElement != null)
  614. elementType = substitutionGroupElement.ElementType;
  615. // Otherwise, the -ur type- definition.
  616. if (elementType == null)
  617. elementType = XmlSchemaComplexType.AnyType;
  618. }
  619. XmlSchemaType xsType = elementType as XmlSchemaType;
  620. if (xsType != null) {
  621. errorCount += xsType.Validate (h, schema);
  622. datatype = xsType.Datatype;
  623. }
  624. // basic {type} is now filled, except for derivation by {substitution group}.
  625. // {substitution group affiliation}
  626. // 3. subsitution group's type derivation check.
  627. if (this.SubstitutionGroup != XmlQualifiedName.Empty) {
  628. XmlSchemaElement substElem = schema.Elements [SubstitutionGroup] as XmlSchemaElement;
  629. // If el is null, then it is missing sub components .
  630. if (substElem != null) {
  631. XmlSchemaType substSchemaType = substElem.ElementType as XmlSchemaType;
  632. if (substSchemaType != null) {
  633. // 3.3.6 Properties Correct 3.
  634. if ((substElem.FinalResolved & XmlSchemaDerivationMethod.Substitution) != 0)
  635. error (h, "Substituted element blocks substitution.");
  636. if (xsType != null && (substElem.FinalResolved & xsType.DerivedBy) != 0)
  637. error (h, "Invalid derivation was found. Substituted element prohibits this derivation method: " + xsType.DerivedBy + ".");
  638. }
  639. XmlSchemaComplexType xsComplexType = xsType as XmlSchemaComplexType;
  640. if (xsComplexType != null)
  641. xsComplexType.ValidateTypeDerivationOK (substElem.ElementType, h, schema);
  642. else {
  643. XmlSchemaSimpleType xsSimpleType = xsType as XmlSchemaSimpleType;
  644. if (xsSimpleType != null)
  645. xsSimpleType.ValidateTypeDerivationOK (substElem.ElementType, h, schema, true);
  646. }
  647. }
  648. // otherwise, it might be missing sub components.
  649. else if (!schema.IsNamespaceAbsent (SubstitutionGroup.Namespace))
  650. error (h, "Referenced element type " + SubstitutionGroup + " was not found in the corresponding schema.");
  651. }
  652. // 2. ElementDefaultValid
  653. // 4. ID with {value constraint} is prohibited.
  654. if (defaultValue != null || fixedValue != null) {
  655. ValidateElementDefaultValidImmediate (h, schema);
  656. if (datatype != null && // Such situation is basically an error. For ValidationEventHandler.
  657. datatype.TokenizedType == XmlTokenizedType.ID)
  658. error (h, "Element type is ID, which does not allows default or fixed values.");
  659. }
  660. // Identity constraints (3.11.3 / 3.11.6)
  661. foreach (XmlSchemaIdentityConstraint ident in Constraints)
  662. ident.Validate (h, schema);
  663. ValidationId = schema.ValidationId;
  664. return errorCount;
  665. }
  666. internal override bool ParticleEquals (XmlSchemaParticle other)
  667. {
  668. XmlSchemaElement element = other as XmlSchemaElement;
  669. if (element == null)
  670. return false;
  671. if (this.ValidatedMaxOccurs != element.ValidatedMaxOccurs ||
  672. this.ValidatedMinOccurs != element.ValidatedMinOccurs)
  673. return false;
  674. if (this.QualifiedName != element.QualifiedName ||
  675. this.ElementType != element.ElementType ||
  676. this.Constraints.Count != element.Constraints.Count)
  677. return false;
  678. for (int i = 0; i < this.Constraints.Count; i++) {
  679. XmlSchemaIdentityConstraint c1 = Constraints [i] as XmlSchemaIdentityConstraint;
  680. XmlSchemaIdentityConstraint c2 = element.Constraints [i] as XmlSchemaIdentityConstraint;
  681. if (c1.QualifiedName != c2.QualifiedName ||
  682. c1.Selector.XPath != c2.Selector.XPath ||
  683. c1.Fields.Count != c2.Fields.Count)
  684. return false;
  685. for (int f = 0; f < c1.Fields.Count; f++) {
  686. XmlSchemaXPath f1 = c1.Fields [f] as XmlSchemaXPath;
  687. XmlSchemaXPath f2 = c2.Fields [f] as XmlSchemaXPath;
  688. if (f1.XPath != f2.XPath)
  689. return false;
  690. }
  691. }
  692. if (this.BlockResolved != element.BlockResolved ||
  693. this.FinalResolved != element.FinalResolved ||
  694. this.ValidatedDefaultValue != element.ValidatedDefaultValue ||
  695. this.ValidatedFixedValue != element.ValidatedFixedValue)
  696. return false;
  697. return true;
  698. }
  699. internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
  700. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  701. {
  702. // element - NameAndTypeOK
  703. XmlSchemaElement baseElement = baseParticle as XmlSchemaElement;
  704. if (baseElement != null) {
  705. return ValidateDerivationByRestrictionNameAndTypeOK (baseElement, h, schema, raiseError);
  706. }
  707. // any - NSCompat
  708. XmlSchemaAny baseAny = baseParticle as XmlSchemaAny;
  709. if (baseAny != null) {
  710. // NSCompat
  711. if (!baseAny.ValidateWildcardAllowsNamespaceName (this.QualifiedName.Namespace, h, schema, raiseError))
  712. return false;
  713. return ValidateOccurenceRangeOK (baseAny, h, schema, raiseError);
  714. }
  715. //*
  716. // choice - RecurseAsIfGroup
  717. XmlSchemaGroupBase gb = null;
  718. if (baseParticle is XmlSchemaSequence)
  719. gb = new XmlSchemaSequence ();
  720. else if (baseParticle is XmlSchemaChoice)
  721. gb = new XmlSchemaChoice ();
  722. else if (baseParticle is XmlSchemaAll)
  723. gb = new XmlSchemaAll ();
  724. if (gb != null) {
  725. gb.Items.Add (this);
  726. gb.Compile (h, schema);
  727. gb.Validate (h, schema);
  728. // It looks weird, but here we never think about
  729. // _pointlessness_ of this groupbase particle.
  730. return gb.ValidateDerivationByRestriction (baseParticle, h, schema, raiseError);
  731. }
  732. //*/
  733. return true;
  734. }
  735. private bool ValidateDerivationByRestrictionNameAndTypeOK (XmlSchemaElement baseElement,
  736. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  737. {
  738. // 1.
  739. if (this.QualifiedName != baseElement.QualifiedName) {
  740. if (raiseError)
  741. error (h, "Invalid derivation by restriction of particle was found. Both elements must have the same name.");
  742. return false;
  743. }
  744. // 2.
  745. if (this.isNillable && !baseElement.isNillable) {
  746. if (raiseError)
  747. error (h, "Invalid element derivation by restriction of particle was found. Base element is not nillable and derived type is nillable.");
  748. return false;
  749. }
  750. // 3.
  751. if (!ValidateOccurenceRangeOK (baseElement, h, schema, raiseError))
  752. return false;
  753. // 4.
  754. if (baseElement.ValidatedFixedValue != null &&
  755. baseElement.ValidatedFixedValue != this.ValidatedFixedValue) {
  756. if (raiseError)
  757. error (h, "Invalid element derivation by restriction of particle was found. Both fixed value must be the same.");
  758. return false;
  759. }
  760. // 5. TODO: What is "identity constraints subset" ???
  761. // 6.
  762. if ((baseElement.BlockResolved | this.BlockResolved) != this.BlockResolved) {
  763. if (raiseError)
  764. error (h, "Invalid derivation by restriction of particle was found. Derived element must contain all of the base element's block value.");
  765. return false;
  766. }
  767. // 7.
  768. if (baseElement.ElementType != null) {
  769. XmlSchemaComplexType derivedCType = this.ElementType as XmlSchemaComplexType;
  770. if (derivedCType != null) {
  771. // FIXME: W3C REC says that it is Type Derivation OK to be check, but
  772. // in fact it should be DerivationValid (Restriction, Complex).
  773. derivedCType.ValidateDerivationValidRestriction (
  774. baseElement.ElementType as XmlSchemaComplexType, h, schema);
  775. derivedCType.ValidateTypeDerivationOK (baseElement.ElementType, h, schema);
  776. } else {
  777. XmlSchemaSimpleType derivedSType = this.ElementType as XmlSchemaSimpleType;
  778. if (derivedSType != null)
  779. derivedSType.ValidateTypeDerivationOK (baseElement.ElementType, h, schema, true);
  780. else if (baseElement.ElementType != XmlSchemaComplexType.AnyType && baseElement.ElementType != this.ElementType) {
  781. if (raiseError)
  782. error (h, "Invalid element derivation by restriction of particle was found. Both primitive types differ.");
  783. return false;
  784. }
  785. }
  786. }
  787. return true;
  788. }
  789. internal override void CheckRecursion (int depth, ValidationEventHandler h, XmlSchema schema)
  790. {
  791. XmlSchemaComplexType ct = this.ElementType as XmlSchemaComplexType;
  792. if (ct == null || ct.Particle == null)
  793. return;
  794. ct.Particle.CheckRecursion (depth + 1, h, schema);
  795. }
  796. internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames, ArrayList nsNames,
  797. ValidationEventHandler h, XmlSchema schema)
  798. {
  799. if (qnames.Contains (this.QualifiedName))// && !this.ParticleEquals ((XmlSchemaParticle) qnames [this.QualifiedName]))
  800. error (h, "Ambiguous element label was detected: " + this.QualifiedName);
  801. else {
  802. foreach (XmlSchemaAny any in nsNames) {
  803. if (any.ValidatedMaxOccurs == 0)
  804. continue;
  805. if (any.HasValueAny ||
  806. any.HasValueLocal && this.QualifiedName.Namespace == "" ||
  807. any.HasValueOther && this.QualifiedName.Namespace != this.QualifiedName.Namespace ||
  808. any.HasValueTargetNamespace && this.QualifiedName.Namespace == this.QualifiedName.Namespace) {
  809. error (h, "Ambiguous element label which is contained by -any- particle was detected: " + this.QualifiedName);
  810. break;
  811. } else if (!any.HasValueOther) {
  812. bool bad = false;
  813. foreach (string ns in any.ResolvedNamespaces) {
  814. if (ns == this.QualifiedName.Namespace) {
  815. bad = true;
  816. break;
  817. }
  818. }
  819. if (bad) {
  820. error (h, "Ambiguous element label which is contained by -any- particle was detected: " + this.QualifiedName);
  821. break;
  822. }
  823. } else {
  824. if (any.TargetNamespace.Length == 0 ||
  825. any.TargetNamespace != this.QualifiedName.Namespace)
  826. error (h, "Ambiguous element label which is contained by -any- particle with ##other value was detected: " + this.QualifiedName);
  827. }
  828. }
  829. qnames.Add (this.QualifiedName, this);
  830. }
  831. }
  832. internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  833. ValidationEventHandler h, XmlSchema schema)
  834. {
  835. XmlSchemaElement labeled = labels [this.QualifiedName] as XmlSchemaElement;
  836. if (labeled == null)
  837. labels.Add (this.QualifiedName, this);
  838. else if (labeled.ElementType != this.ElementType)
  839. error (h, "Different types are specified on the same named elements in the same sequence. Element name is " + QualifiedName);
  840. }
  841. // 3.3.6 Element Default Valid (Immediate)
  842. private void ValidateElementDefaultValidImmediate (ValidationEventHandler h, XmlSchema schema)
  843. {
  844. // This presumes that ElementType is already filled.
  845. XmlSchemaDatatype datatype = elementType as XmlSchemaDatatype;
  846. XmlSchemaSimpleType simpleType = elementType as XmlSchemaSimpleType;
  847. if (simpleType != null)
  848. datatype = simpleType.Datatype;
  849. if (datatype == null) {
  850. XmlSchemaComplexType complexType = elementType as XmlSchemaComplexType;
  851. switch (complexType.ContentType) {
  852. case XmlSchemaContentType.Empty:
  853. case XmlSchemaContentType.ElementOnly:
  854. error (h, "Element content type must be simple type or mixed.");
  855. break;
  856. }
  857. datatype = XmlSchemaSimpleType.AnySimpleType;
  858. }
  859. XmlNamespaceManager nsmgr = null;
  860. if (datatype.TokenizedType == XmlTokenizedType.QName) {
  861. if (this.Namespaces != null)
  862. foreach (XmlQualifiedName qname in Namespaces.ToArray ())
  863. nsmgr.AddNamespace (qname.Name, qname.Namespace);
  864. }
  865. try {
  866. if (defaultValue != null) {
  867. validatedDefaultValue = datatype.Normalize (defaultValue);
  868. datatype.ParseValue (validatedDefaultValue, null, nsmgr);
  869. }
  870. } catch (Exception ex) {
  871. // FIXME: This is not a good way to handle exception, but
  872. // I think there is no remedy for such Framework specification.
  873. error (h, "The Element's default value is invalid with respect to its type definition.", ex);
  874. }
  875. try {
  876. if (fixedValue != null) {
  877. validatedFixedValue = datatype.Normalize (fixedValue);
  878. datatype.ParseValue (validatedFixedValue, null, nsmgr);
  879. }
  880. } catch (Exception ex) {
  881. // FIXME: This is not a good way to handle exception.
  882. error (h, "The Element's fixed value is invalid with its type definition.", ex);
  883. }
  884. }
  885. //<element
  886. // abstract = boolean : false
  887. // block = (#all | List of (extension | restriction | substitution))
  888. // default = string
  889. // final = (#all | List of (extension | restriction))
  890. // fixed = string
  891. // form = (qualified | unqualified)
  892. // id = ID
  893. // maxOccurs = (nonNegativeInteger | unbounded) : 1
  894. // minOccurs = nonNegativeInteger : 1
  895. // name = NCName
  896. // nillable = boolean : false
  897. // ref = QName
  898. // substitutionGroup = QName
  899. // type = QName
  900. // {any attributes with non-schema namespace . . .}>
  901. // Content: (annotation?, ((simpleType | complexType)?, (unique | key | keyref)*))
  902. //</element>
  903. internal static XmlSchemaElement Read(XmlSchemaReader reader, ValidationEventHandler h)
  904. {
  905. XmlSchemaElement element = new XmlSchemaElement();
  906. Exception innerex;
  907. reader.MoveToElement();
  908. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  909. {
  910. error(h,"Should not happen :1: XmlSchemaElement.Read, name="+reader.Name,null);
  911. reader.Skip();
  912. return null;
  913. }
  914. element.LineNumber = reader.LineNumber;
  915. element.LinePosition = reader.LinePosition;
  916. element.SourceUri = reader.BaseURI;
  917. while(reader.MoveToNextAttribute())
  918. {
  919. if(reader.Name == "abstract")
  920. {
  921. element.IsAbstract = XmlSchemaUtil.ReadBoolAttribute(reader,out innerex);
  922. if(innerex != null)
  923. error(h,reader.Value + " is invalid value for abstract",innerex);
  924. }
  925. else if(reader.Name == "block")
  926. {
  927. element.block = XmlSchemaUtil.ReadDerivationAttribute(reader,out innerex, "block",
  928. XmlSchemaUtil.ElementBlockAllowed);
  929. if(innerex != null)
  930. error (h,"some invalid values for block attribute were found",innerex);
  931. }
  932. else if(reader.Name == "default")
  933. {
  934. element.defaultValue = reader.Value;
  935. }
  936. else if(reader.Name == "final")
  937. {
  938. element.Final = XmlSchemaUtil.ReadDerivationAttribute(reader,out innerex, "final",
  939. XmlSchemaUtil.FinalAllowed);
  940. if(innerex != null)
  941. error (h,"some invalid values for final attribute were found",innerex);
  942. }
  943. else if(reader.Name == "fixed")
  944. {
  945. element.fixedValue = reader.Value;
  946. }
  947. else if(reader.Name == "form")
  948. {
  949. element.form = XmlSchemaUtil.ReadFormAttribute(reader,out innerex);
  950. if(innerex != null)
  951. error(h,reader.Value + " is an invalid value for form attribute",innerex);
  952. }
  953. else if(reader.Name == "id")
  954. {
  955. element.Id = reader.Value;
  956. }
  957. else if(reader.Name == "maxOccurs")
  958. {
  959. try
  960. {
  961. element.MaxOccursString = reader.Value;
  962. }
  963. catch(Exception e)
  964. {
  965. error(h,reader.Value + " is an invalid value for maxOccurs",e);
  966. }
  967. }
  968. else if(reader.Name == "minOccurs")
  969. {
  970. try
  971. {
  972. element.MinOccursString = reader.Value;
  973. }
  974. catch(Exception e)
  975. {
  976. error(h,reader.Value + " is an invalid value for minOccurs",e);
  977. }
  978. }
  979. else if(reader.Name == "name")
  980. {
  981. element.Name = reader.Value;
  982. }
  983. else if(reader.Name == "nillable")
  984. {
  985. element.IsNillable = XmlSchemaUtil.ReadBoolAttribute(reader,out innerex);
  986. if(innerex != null)
  987. error(h,reader.Value + "is not a valid value for nillable",innerex);
  988. }
  989. else if(reader.Name == "ref")
  990. {
  991. element.refName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  992. if(innerex != null)
  993. error(h, reader.Value + " is not a valid value for ref attribute",innerex);
  994. }
  995. else if(reader.Name == "substitutionGroup")
  996. {
  997. element.substitutionGroup = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  998. if(innerex != null)
  999. error(h, reader.Value + " is not a valid value for substitutionGroup attribute",innerex);
  1000. }
  1001. else if(reader.Name == "type")
  1002. {
  1003. element.SchemaTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  1004. if(innerex != null)
  1005. error(h, reader.Value + " is not a valid value for type attribute",innerex);
  1006. }
  1007. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  1008. {
  1009. error(h,reader.Name + " is not a valid attribute for element",null);
  1010. }
  1011. else
  1012. {
  1013. XmlSchemaUtil.ReadUnhandledAttribute(reader,element);
  1014. }
  1015. }
  1016. reader.MoveToElement();
  1017. if(reader.IsEmptyElement)
  1018. return element;
  1019. // Content: annotation?,
  1020. // (simpleType | complexType)?,
  1021. // (unique | key | keyref)*
  1022. int level = 1;
  1023. while(reader.ReadNextElement())
  1024. {
  1025. if(reader.NodeType == XmlNodeType.EndElement)
  1026. {
  1027. if(reader.LocalName != xmlname)
  1028. error(h,"Should not happen :2: XmlSchemaElement.Read, name="+reader.Name,null);
  1029. break;
  1030. }
  1031. if(level <= 1 && reader.LocalName == "annotation")
  1032. {
  1033. level = 2; //Only one annotation
  1034. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  1035. if(annotation != null)
  1036. element.Annotation = annotation;
  1037. continue;
  1038. }
  1039. if(level <= 2)
  1040. {
  1041. if(reader.LocalName == "simpleType")
  1042. {
  1043. level = 3;
  1044. XmlSchemaSimpleType simple = XmlSchemaSimpleType.Read(reader,h);
  1045. if(simple != null)
  1046. element.SchemaType = simple;
  1047. continue;
  1048. }
  1049. if(reader.LocalName == "complexType")
  1050. {
  1051. level = 3;
  1052. XmlSchemaComplexType complex = XmlSchemaComplexType.Read(reader,h);
  1053. if(complex != null)
  1054. {
  1055. element.SchemaType = complex;
  1056. }
  1057. continue;
  1058. }
  1059. }
  1060. if(level <= 3)
  1061. {
  1062. if(reader.LocalName == "unique")
  1063. {
  1064. level = 3;
  1065. XmlSchemaUnique unique = XmlSchemaUnique.Read(reader,h);
  1066. if(unique != null)
  1067. element.constraints.Add(unique);
  1068. continue;
  1069. }
  1070. else if(reader.LocalName == "key")
  1071. {
  1072. level = 3;
  1073. XmlSchemaKey key = XmlSchemaKey.Read(reader,h);
  1074. if(key != null)
  1075. element.constraints.Add(key);
  1076. continue;
  1077. }
  1078. else if(reader.LocalName == "keyref")
  1079. {
  1080. level = 3;
  1081. XmlSchemaKeyref keyref = XmlSchemaKeyref.Read(reader,h);
  1082. if(keyref != null)
  1083. element.constraints.Add(keyref);
  1084. continue;
  1085. }
  1086. }
  1087. reader.RaiseInvalidElementError();
  1088. }
  1089. return element;
  1090. }
  1091. }
  1092. }