public void ComplexErrorMessage2() { SchematronValidator validator = new SchematronValidator(SchematronReader.ReadSchematron("Schematron/Samples/All.sch")); XmlDocument doc = new XmlDocument(); doc.LoadXml("<document><a><foobar>foobar</foobar><foobar>x</foobar></a></document>"); ExceptionAssert.Throws<SchematronValidationException>(() => validator.Validate(doc), "The element '/document/a/foobar[2]' must be foobar"); }
public void BadPhaseName() { SchematronValidator validator = new SchematronValidator(SchematronReader.ReadSchematron("Schematron/Samples/All.sch")); XmlDocument doc; doc = new XmlDocument(); doc.LoadXml("<document><a><foobar>x</foobar></a></document>"); validator.ValidationPhase = "undefined"; ExceptionAssert.Throws<ArgumentException>(() => validator.Validate(doc), "'undefined' is not a defined phase."); }
public void IgnoreXpathErrors() { XmlDocument dogs = new XmlDocument(); dogs.LoadXml(@" <dogs> <dog petname='spot'><nose/><ear/><bone/><ear/></dog> <dog petname='hungry'><nose/><ear/><ear/></dog> <dog petname='smelly'><ear/><bone/><ear/></dog> </dogs>"); StringBuilder svrl = new StringBuilder(); SchematronValidator validator = new SchematronValidator("Schematron/Samples/DogXPathError.sch") { IgnoreQueryExpressionErrrors = true }; ValidationReport report = new ValidationReport(validator, svrl); validator.Validate(dogs); Assert.IsTrue(report.HasValidationErrors); }
public void ValidateEmptyDocumentWithAllSampleSchematron() { var xml = new XmlDocument(); xml.LoadXml("<document />"); foreach (var name in Directory.EnumerateFiles("Schematron/Samples", "*.sch", SearchOption.AllDirectories)) { // Current schematron can not validate old schematron. if (Path.GetFileName(name).StartsWith("Bad") || "Schematron-1.5.sch" == Path.GetFileName(name)) continue; Console.WriteLine(name); var validator = new SchematronValidator(name); validator.Validate(xml); } }
public void ValidatingSchematronDocument() { SchematronValidator validator = new SchematronValidator(SchematronReader.ReadSchematron("Schematron/Samples/Schematron-1.5.sch")); validator.ValidationPhase = Phase.All; XmlDocument doc = new XmlDocument(); doc.Load("Schematron/Samples/All.sch"); Assert.IsTrue(TryValidating(validator, doc)); }
/// <summary> /// Creates a new instance of the <see cref="ValidationReport"/> class using the specified <see cref="Stream"/>. /// </summary> /// <param name="validator"> /// The <see cref="SchematronValidator"/> to report. /// </param> /// <param name="stream"> /// The <see cref="Stream"/> used to write the report. /// </param> public ValidationReport(SchematronValidator validator, Stream stream) : this(validator, XmlWriter.Create(stream, defaultWriterSettings)) { }
public void SimpleValidating2() { SchematronValidator validator = new SchematronValidator("Schematron/Samples/All.sch"); XmlDocument instance = new XmlDocument(); instance.LoadXml("<document><a><foo>bar</foo></a></document>"); validator.Validate(instance); }
public void PostalZoneWithXPath() { SchematronValidator validator = new SchematronValidator("Schematron/Samples/PostalZone.sch"); XPathDocument instance = new XPathDocument("Schematron/Samples/PostalZone.ubl"); StringBuilder svrl = new StringBuilder(); ValidationReport report = new ValidationReport(validator, svrl); validator.Validate(instance); Console.WriteLine(svrl.ToString()); Assert.IsFalse(report.HasValidationErrors); }
public void SimpleErrorMessage() { SchematronValidator validator = new SchematronValidator(SchematronReader.ReadSchematron("Schematron/Samples/All.sch")); XmlDocument doc = new XmlDocument(); doc.LoadXml("<document><a><foo>x</foo></a></document>"); ExceptionAssert.Throws<SchematronValidationException>(() => validator.Validate(doc), "foo must be bar"); }
public void PhasedValidating() { SchematronValidator validator = new SchematronValidator(SchematronReader.ReadSchematron("Schematron/Samples/All.sch")); XmlDocument doc; doc = new XmlDocument(); doc.LoadXml("<document><a><foobar>x</foobar></a></document>"); Assert.IsFalse(TryValidating(validator, doc)); validator.ValidationPhase = "min"; Assert.IsTrue(TryValidating(validator, doc)); }
public void PostalZone() { SchematronValidator validator = new SchematronValidator("Schematron/Samples/PostalZone.sch"); XmlDocument instance = new XmlDocument(); instance.Load("Schematron/Samples/PostalZone.ubl"); StringBuilder svrl = new StringBuilder(); ValidationReport report = new ValidationReport(validator, svrl); validator.Validate(instance); Console.WriteLine(svrl.ToString()); }
/// <summary> /// Creates a new instance of the <see cref="ValidationReport"/> class using the specified <see cref="TextWriter"/>. /// </summary> /// <param name="validator"></param> /// <param name="writer"> /// The <see cref="TextWriter"/> used to write the report. /// </param> public ValidationReport(SchematronValidator validator, TextWriter writer) : this(validator, XmlWriter.Create(writer, defaultWriterSettings)) { }
/// <summary> /// Creates a new instance of the <see cref="ValidationReport"/> class using the specified filename. /// </summary> /// <param name="validator"></param> /// <param name="filename"> /// The name of file to write the report to. /// </param> public ValidationReport(SchematronValidator validator, string filename) : this(validator, XmlWriter.Create(filename, defaultWriterSettings)) { closeStream = true; }
/// <summary> /// Creates a new instance of the <see cref="ValidationReport"/> class using the specified <see cref="StringBuilder"/>. /// </summary> /// <param name="validator"> /// The <see cref="SchematronValidator"/> to report. /// </param> /// <param name="s"> /// The <see cref="StringBuilder"/> used to write the report. /// </param> public ValidationReport(SchematronValidator validator, StringBuilder s) : this(validator, XmlWriter.Create(s, defaultWriterSettings)) { }
public void MultipleErrors() { SchematronValidator validator = new SchematronValidator(SchematronReader.ReadSchematron("Schematron/Samples/All.sch")); XmlDocument doc = new XmlDocument(); doc.LoadXml("<document><a><foobar>x</foobar><foobar>x</foobar></a></document>"); errors.Clear(); validator.Validate(doc, new SchematronValidationEventHandler(handler)); Assert.AreEqual(2, errors.Count); Assert.AreEqual("The element '/document/a/foobar[1]' must be foobar", errors[0].Message); Assert.AreEqual("The element '/document/a/foobar[2]' must be foobar", errors[1].Message); }
public void SimpleValidating() { SchematronValidator validator = new SchematronValidator(SchematronReader.ReadSchematron("Schematron/Samples/All.sch")); XmlDocument doc; doc = new XmlDocument(); doc.LoadXml("<document><a><foo>bar</foo></a></document>"); Assert.IsTrue(TryValidating(validator, doc)); doc = new XmlDocument(); doc.LoadXml("<document><a><foo>x</foo></a></document>"); Assert.IsFalse(TryValidating(validator, doc)); doc = new XmlDocument(); doc.LoadXml("<document><a><foo>bar</foo><dummy/></a></document>"); Assert.IsFalse(TryValidating(validator, doc)); }
internal SchematronDocument ReadSchema(SchematronDocument schematron, bool validateWithSchematron) { // Check that the namespace is a schematron namespace. XmlElement e = xml.DocumentElement; if (e.NamespaceURI != Schematron.IsoNamespace && e.NamespaceURI != Schematron.OriginalNamespace) throw new Exception(String.Format("'{0}' is not a namepsace for schematron document.")); schematronNamespaceURI = e.NamespaceURI; // Check that the document validates against the W3C XSD schema. xml.Schemas = Schematron.Default.XsdSet; xml.Validate(null); // Check that the document validates against the Schematron schema. if (validateWithSchematron) { SchematronValidator validator = new SchematronValidator(); if (e.NamespaceURI == Schematron.OriginalNamespace) { validator.SchemaDocument = Schematron.Default.OriginalSchematronSchema; validator.ValidationPhase = "Full"; } else if (e.NamespaceURI == Schematron.IsoNamespace) { validator.SchemaDocument = Schematron.Default.IsoSchematronSchema; } validator.Validate(xml); } // Read the document element and all children. schematron.ID = e.GetAttribute(id); schematron.Fpi = e.GetAttribute(fpi); schematron.Icon = e.GetAttribute(icon); schematron.See = e.GetAttribute(see); schematron.SchemaVersion = e.GetAttribute(schemaVersion); if (e.HasAttribute(defaultPhase)) schematron.DefaultPhase = e.GetAttribute(defaultPhase); if (e.HasAttribute(queryBinding)) schematron.QueryLanguage = e.GetAttribute(queryBinding); if (e.HasChildNodes) { foreach (XmlNode childNode in e.ChildNodes) { XmlElement child = childNode as XmlElement; if (child != null && child.NamespaceURI == schematronNamespaceURI) { if (child.LocalName == ns) schematron.Namespaces.Add(ReadNamespaceDefinition(child)); else if (child.LocalName == pattern) schematron.Patterns.Add(ReadPattern(child)); else if (child.LocalName == phase) schematron.Phases.Add(ReadPhase(child)); else if (child.LocalName == diagnostics) schematron.Diagnostics = ReadDiagnostics(child); else if (child.LocalName == p) schematron.Annotation.Add(child); else if (child.LocalName == title) schematron.Title = ReadAnnotation(child); else if (child.LocalName == p) schematron.Annotation.Add(child); else if (child.LocalName == let) schematron.Parameters.Add(child.GetAttribute(name), child.GetAttribute(value)); else throw new Exception(String.Format("'{0}' is an unknown schematron element.", child.Name)); } } } return schematron; }
/// <summary> /// Creates a new instance of the <see cref="ValidationReport"/> class using the specified <see cref="XmlWriter"/>. /// </summary> /// <param name="validator"></param> /// <param name="report"> /// The <see cref="XmlWriter"/> used to write the report. /// </param> public ValidationReport(SchematronValidator validator, XmlWriter report) { if (validator == null) throw new ArgumentNullException("validator"); if (report == null) throw new ArgumentNullException("report"); this.validator = validator; this.report = report; // Hookup the events. validator.RuleFired += RuleFired; validator.ActivePattern += ActivePattern; validator.AssertionFailed += AssertionFailed; validator.Start += Start; validator.End += End; }