Пример #1
0
 def test_variables_multiple_error(self):
     schema = Schema(
         get_file("schematron",
                  "variables/variables1_xslt2_multiple_error.sch"))
     xml_doc = etree.parse(
         get_file("xml", "variables/variables1_correct.xml"))
     self.assertRaises(SchematronError, schema.validate_document, xml_doc)
Пример #2
0
 def test_phase_with_unknown_pattern(self):
     schema = Schema(
         get_file("schematron", "malformed/bad_active_pattern.sch"))
     xml_doc = etree.parse(get_file("xml", "basic1_error_1.xml"))
     self.assertEqual("bad_phase", schema.get_phase("bad_phase").id)
     self.assertRaises(SchematronError, schema.validate_document, xml_doc,
                       "bad_phase")
Пример #3
0
def main(schematron_file,
         xml_file,
         phase="#DEFAULT",
         output_type="text",
         output_stream=sys.stdout,
         verbosity=1):
    schema = Schema(verbosity=verbosity)
    schema.read_from_file(schematron_file)
    schema.process_abstract_patterns()

    doc = etree.parse(xml_file)
    if output_type == 'text':
        return validate_to_text(schema, doc, xml_file, phase, output_stream,
                                verbosity)
    elif output_type == 'svrl':
        svrl = schema.validate_document_to_svrl(doc, phase)
        output_stream.write(
            etree.tostring(svrl.to_xml(),
                           pretty_print=True,
                           xml_declaration=True,
                           encoding='utf-8').decode('utf-8'))
        output_stream.write("\n")
        return 0
    else:
        raise Exception("Unknown output type: %s" % output_type)
Пример #4
0
 def get_schematron_minimal_xml(self, filename):
     # These are all schematrons, and schematrons with includes can fail
     # the schematron validation (for instance if a pattern which is defined in
     # an included file is referenced in the main file)
     # Therefore, we don't validate it directly, but convert it to a minimal version
     # first
     schema_to_check = Schema(get_file("schematron", filename))
     return schema_to_check.to_minimal_xml_document()
Пример #5
0
 def test_variables1(self):
     schema = Schema(
         get_file("schematron", "variables/variables1_xslt2.sch"))
     xml_doc = etree.parse(
         get_file("xml", "variables/variables1_correct.xml"))
     report = schema.validate_document(xml_doc)
     errors = report.get_failed_asserts()
     self.assertEqual([], errors, [e.test for e in errors])
Пример #6
0
    def test_simple_diagnostics(self):
        schema = Schema(get_file("schematron", "diagnostics.sch"))
        xml_doc = etree.parse(
            get_file("xml", "diagnostics/more_than_three_animals.xml"))
        report = schema.validate_document(xml_doc)

        self.assertEqual(3, len(report.get_failed_asserts()))
        self.assertEqual(1,
                         len(report.get_failed_asserts()[0][0].diagnostic_ids))
        self.assertEqual(
            """Noah, you must remove as many animals from the ark so that
      only two of one species live in this accommodation.""".strip(),
            report.get_failed_asserts()[0][0].get_diagnostic_text(
                report.get_failed_asserts()[0][0].diagnostic_ids[0]).strip())
Пример #7
0
    def test_basic_example(self):
        schema = Schema(get_file("schematron", "basic.sch"))

        doc = etree.parse(get_file("xml", "basic1_ok.xml"))

        variables = {}
        parser = XPath2Parser(schema.ns_prefixes, variables)
        for p in schema.patterns.values():
            # print("[XX] %s has %d rules" % (p.id, len(p.rules)))
            for r in p.rules:

                elements = select(doc, r.context)
                for element in elements:
                    context = XPathContext(root=doc, item=element)
                    for a in r.assertions:
                        root_token = parser.parse(a.test)
                        result = root_token.evaluate(context)
                        self.assertTrue(result, a.to_string())
Пример #8
0
    def check_schema_validation(self, schema_file, xml_file, expected_errors,
                                expected_warnings):
        """
        expected_errors is a list of the id values of the assertions that should fail with either no flag or flag="error"
        expected_warnings is a list of the id values of the assertions that should fail with either flag="warning"
        """
        schema = Schema(schema_file)
        xml_doc = etree.parse(xml_file)

        report = schema.validate_document(xml_doc)
        errors = report.get_failed_asserts()
        error_id_list = [
            err.id for err, element in errors if err.flag != 'warning'
        ]
        warnings = report.get_failed_asserts()
        warning_id_list = [
            w.id for w, element in warnings if w.flag == 'warning'
        ]
        self.assertEqual(expected_errors, error_id_list)
        self.assertEqual(expected_warnings, warning_id_list)
Пример #9
0
    def check_rule_order(self, schematron_file):
        schema = Schema(get_file("schematron", schematron_file))

        # Element a should match rule 1 only
        self.assertEqual([], self.validate(schema, '<a>1</a>'))
        # self.assertEqual(['r1'], self.validate("<a>2</a>"))
        self.assertEqual(['r1'],
                         self.validate(schema, '<root id="1"><a>2</a></root>'))

        # Element c should match rule 3 only
        self.assertEqual([], self.validate(schema, "<c>1</c>"))
        self.assertEqual(['r3'], self.validate(schema, "<c>2</c>"))

        # Element c should match rule 4 only
        self.assertEqual([], self.validate(schema, "<d>1</d>"))
        self.assertEqual(['r4'], self.validate(schema, "<d>2</d>"))

        # An arbitraty element should match rule 5
        self.assertEqual([], self.validate(schema, '<arb id="a">1</arb>'))
        self.assertEqual(['r5'], self.validate(schema, "<arb>1</arb>"))

        # Make sure this goes for nested elements too
        self.assertEqual(
            [],
            self.validate(
                schema,
                '<arb id="a"><a>1</a><b>1</b><c>1</c><d>1</d><e id="1">1</e></arb>'
            ))
        self.assertEqual(
            ['r4'],
            self.validate(
                schema,
                '<arb id="a"><a>1</a><b>1</b><c>1</c><d>2</d><e id="1">1</e></arb>'
            ))
        self.assertEqual(
            ['r1', 'r2', 'r3', 'r4'],
            self.validate(
                schema,
                '<arb id="a"><a>2</a><b>2</b><c>2</c><d>2</d><e id="1">2</e></arb>'
            ))
Пример #10
0
 def setUp(self):
     self.schema = Schema(get_file("schematron", "schematron.sch"))
Пример #11
0
 def setUp(self):
     self.schema = Schema(get_file("schematron", "orderchecks/xslt.sch"))
Пример #12
0
 def test_error_let_statement(self):
     schema = Schema(
         get_file("schematron", "xpath2/error_let_statement.sch"))
     xml_doc = etree.parse(get_file("xml", "basic1_ok.xml"))
     self.assertRaises(SchematronQueryBindingError,
                       schema.validate_document, xml_doc)
Пример #13
0
 def setUp(self):
     self.schema = Schema(get_file("schematron", "all_elements.sch"))