mirror of
https://github.com/Theodor-Springmann-Stiftung/KGPZ.git
synced 2025-10-29 09:05:30 +00:00
Validation should fail on error
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
import os
|
import os
|
||||||
from lxml import etree
|
from lxml import etree
|
||||||
from urllib.parse import urljoin
|
|
||||||
|
|
||||||
REPO_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
|
REPO_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
|
||||||
XML_DIR = os.path.join(REPO_ROOT, 'XML')
|
XML_DIR = os.path.join(REPO_ROOT, 'XML')
|
||||||
@@ -11,42 +10,52 @@ def validate_xml(xml_file):
|
|||||||
tree = etree.parse(xml_file, parser)
|
tree = etree.parse(xml_file, parser)
|
||||||
root = tree.getroot()
|
root = tree.getroot()
|
||||||
|
|
||||||
# Get the schema location
|
|
||||||
schema_location = root.get('{http://www.w3.org/2001/XMLSchema-instance}schemaLocation')
|
schema_location = root.get('{http://www.w3.org/2001/XMLSchema-instance}schemaLocation')
|
||||||
if schema_location:
|
if schema_location:
|
||||||
namespace, xsd_path = schema_location.split()
|
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))
|
xsd_path = os.path.normpath(os.path.join(os.path.dirname(xml_file), xsd_path))
|
||||||
|
|
||||||
if os.path.exists(xsd_path):
|
if os.path.exists(xsd_path):
|
||||||
xsd_doc = etree.parse(xsd_path)
|
xsd_doc = etree.parse(xsd_path)
|
||||||
schema = etree.XMLSchema(xsd_doc)
|
schema = etree.XMLSchema(xsd_doc)
|
||||||
|
|
||||||
# Validate the XML against the schema
|
|
||||||
schema.assertValid(tree)
|
schema.assertValid(tree)
|
||||||
print(f"Validation successful: {xml_file}")
|
print(f"Validation erfolgreich: {xml_file}")
|
||||||
else:
|
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:
|
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:
|
except etree.DocumentInvalid as e:
|
||||||
print(f"Validation error in {xml_file}:")
|
print(f"Validierungsfehler in {xml_file}:")
|
||||||
for error in e.error_log:
|
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:
|
except etree.XMLSyntaxError as e:
|
||||||
print(f"XML syntax error in {xml_file}:")
|
print(f"XML-Syntaxfehler in {xml_file}:")
|
||||||
print(f" Line {e.lineno}, Column {e.offset}: {e.msg}")
|
print(f" Zeile {e.lineno}, Spalte {e.offset}: {e.msg}")
|
||||||
|
return False
|
||||||
except Exception as e:
|
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():
|
def main():
|
||||||
|
validation_failed = False
|
||||||
for root, dirs, files in os.walk(XML_DIR):
|
for root, dirs, files in os.walk(XML_DIR):
|
||||||
for file in files:
|
for file in files:
|
||||||
if file.endswith('.xml'):
|
if file.endswith('.xml'):
|
||||||
xml_file = os.path.join(root, file)
|
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__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user