1
2
3
4
5
6
7
8
9
10
11
12
13
14 from lxml import etree
15
17 """
18 This validator uses lxml to validate XML files against schemas defined by
19 XSD files.
20 """
21
23 self.xsd_path = xsd_path
24 self.load_xsd(self.xsd_path)
25
27 """
28 Load the XSD schema at `xsd_path`.
29 """
30 with open(xsd_path) as schema_file:
31 xml_schema_doc = etree.parse(schema_file)
32 self.xml_schema = etree.XMLSchema(xml_schema_doc)
33 return self.xml_schema
34
36 """
37 Determine whether the file at `path` is valid using the configured xml schema.
38 """
39 with open(path) as xml_file:
40 return self.validate_file(xml_file)
41
43 """
44 Determine whether a file is valid using the configured xml schema.
45 """
46 return self.xml_schema.validate(etree.parse(f))
47
49 """
50 This method will throw exceptions when trying to validate a path.
51 """
52 with open(path) as xml_file:
53 return self.check_file(xml_file)
54
56 """
57 This method will throw exceptions when trying to validate a file.
58 """
59 return self.xml_schema.assertValid(etree.parse(xml_file))
60