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.");
 }
        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;
        }
 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 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 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 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 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 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());
 }