/// <summary> /// Creates a new instance of the ValidationEvaluator containing the /// the validation schema and a specific XML document to be validated. /// </summary> /// <param name="schema">Validation schema. Must not be null.</param> /// <param name="xInstance">An instance of an XML document to be validated. /// Must not be null.</param> /// <param name="fullValidation">Indicates whether to validate the /// whole document regardless of any assertion, or to stop validation at /// the first assertion.</param> public ValidationEvaluator(Schema schema, XDocument xInstance, bool fullValidation) { this.schema = schema; this.xInstance = xInstance; this.fullValidation = fullValidation; this.xNavigator = xInstance.CreateNavigator(); }
/// <summary> /// Converts a schema from the XML form (XDocument) to the internal /// representation (Schema). /// </summary> /// <param name="xSchema">Schema in serialized XML form. Must not be null.</param> /// <param name="nsManager">Namespace manager. Must not be null.</param> /// <returns>Schema in internal representation (Schema)</returns> /// <exception cref="ArgumentNullException" /> public static Schema Deserialize(XDocument xSchema, XmlNamespaceManager nsManager) { if (xSchema == null) { throw new ArgumentNullException("xSchema"); } if (nsManager == null) { throw new ArgumentNullException("nsManager"); } Schema schema = new Schema(); schema.Namespaces = DeserializeNamespaces(xSchema.Root, nsManager); schema.Patterns = DeserializePatterns(xSchema.Root, nsManager); return schema; }
/// <summary> /// Compiles XPath expressions in schema patterns. /// </summary> /// <remarks> /// The XPath expression are compiled in place inside the schema. /// </remarks> /// <param name="schema">Schema which contains the expressions to be /// compiled</param> /// <exception cref="SyntaxException">Thrown if any of the expressions /// does not conform to XPath 1.0. The exceptions may contain multiple /// error messages.</exception> private static void CompileXPathExpressions(Schema schema) { List<string> messages = new List<string>(); // resolve namespaces XmlNamespaceManager nsManager = new XmlNamespaceManager(new NameTable()); foreach (Namespace ns in schema.Namespaces) { nsManager.AddNamespace(ns.Prefix, ns.Uri); } // compile XPath expressions foreach (Pattern pattern in schema.Patterns) { // compile contexts foreach (Rule rule in pattern.Rules) { // alter xpath context string context = rule.Context; if (context.Length > 0 && context[0] != '/') { context = String.Concat("//", context); } try { rule.CompiledContext = System.Xml.XPath.XPathExpression.Compile(context, nsManager); } catch (XPathException e) { messages.Add(String.Format("Invalid XPath 1.0 context='{0}': {1}", rule.Context, e.Message)); } // compile tests foreach (Assert assert in rule.Asserts) { try { assert.CompiledTest = System.Xml.XPath.XPathExpression.Compile(assert.Test, nsManager); } catch (XPathException e) { messages.Add(String.Format("Invalid XPath 1.0 test='{0}': {1}", assert.Test, e.Message)); } // compile diagnostics if (assert.Diagnostics.Length > 0) { assert.CompiledDiagnostics = new XPathExpression[assert.Diagnostics.Length]; for (int i = 0; i < assert.Diagnostics.Length; i++) { string diag = assert.Diagnostics[i]; try { assert.CompiledDiagnostics[i] = XPathExpression.Compile(diag); } catch (XPathException e) { if (assert.DiagnosticsIsValueOf[i]) { messages.Add(String.Format("Invalid XPath 1.0 select='{0}': {1}", diag, e.Message)); } else { messages.Add(String.Format("Invalid XPath 1.0 path='{0}']: {1}", diag, e.Message)); } } } } } } } // syntax errors if (messages.Count > 0) { throw new SyntaxException(messages); } }