Validation should fail on error

This commit is contained in:
Simon Martens
2024-07-18 00:00:28 +02:00
parent ca4a2e1dbe
commit 46753854ce

View File

@@ -1,6 +1,5 @@
import os
from lxml import etree
from urllib.parse import urljoin
REPO_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
XML_DIR = os.path.join(REPO_ROOT, 'XML')
@@ -11,42 +10,52 @@ def validate_xml(xml_file):
tree = etree.parse(xml_file, parser)
root = tree.getroot()
# Get the schema location
schema_location = root.get('{http://www.w3.org/2001/XMLSchema-instance}schemaLocation')
if schema_location:
namespace, xsd_path = schema_location.split()
# Convert relative path to absolute
xsd_path = os.path.normpath(os.path.join(os.path.dirname(xml_file), xsd_path))
if os.path.exists(xsd_path):
xsd_doc = etree.parse(xsd_path)
schema = etree.XMLSchema(xsd_doc)
# Validate the XML against the schema
schema.assertValid(tree)
print(f"Validation successful: {xml_file}")
print(f"Validation erfolgreich: {xml_file}")
else:
print(f"Schema file not found: {xsd_path} for {xml_file}")
print(f"Schema-Datei nicht gefunden: {xsd_path} für {xml_file}")
return False
else:
print(f"No schema location found in {xml_file}")
print(f"Keine Schema-Location gefunden in {xml_file}")
return False
except etree.DocumentInvalid as e:
print(f"Validation error in {xml_file}:")
print(f"Validierungsfehler in {xml_file}:")
for error in e.error_log:
print(f" Line {error.line}, Column {error.column}: {error.message}")
print(f" Zeile {error.line}, Spalte {error.column}: {error.message}")
return False
except etree.XMLSyntaxError as e:
print(f"XML syntax error in {xml_file}:")
print(f" Line {e.lineno}, Column {e.offset}: {e.msg}")
print(f"XML-Syntaxfehler in {xml_file}:")
print(f" Zeile {e.lineno}, Spalte {e.offset}: {e.msg}")
return False
except Exception as e:
print(f"Error processing {xml_file}: {str(e)}")
print(f"Fehler bei der Verarbeitung von {xml_file}: {str(e)}")
return False
return True
def main():
validation_failed = False
for root, dirs, files in os.walk(XML_DIR):
for file in files:
if file.endswith('.xml'):
xml_file = os.path.join(root, file)
validate_xml(xml_file)
if not validate_xml(xml_file):
validation_failed = True
if validation_failed:
print("Validierung fehlgeschlagen. Bitte korrigieren Sie die oben genannten Fehler.")
exit(1)
else:
print("Alle XML-Dateien wurden erfolgreich validiert.")
if __name__ == "__main__":
main()