1
1
# Copyright (C) 2022 Anthony Harrison
2
2
# SPDX-License-Identifier: GPL-3.0-or-later
3
-
4
3
import logging
5
4
from pathlib import Path
6
5
13
12
14
13
15
14
def _validate_xml (filename , xsd_file ):
16
- # Resolve folder where schemas are present
17
15
"""
16
+ Validates an XML file against a specified XSD schema.
17
+
18
18
The XSD schema file contains the 'grammar rules' that the XML file should follow.
19
19
It first constructs the path to the XSD schema file, then it creates an XMLSchema object using the xmlschema library.
20
20
It logs a debug message about the validation process, then attempts to validate the XML file against the schema.
@@ -29,16 +29,16 @@ def _validate_xml(filename, xsd_file):
29
29
Returns:
30
30
bool: True if the XML file is valid according to the schema, False otherwise.
31
31
"""
32
-
32
+ # Resolve the folder where schemas are located.
33
33
schemas_file = Path (__file__ ).resolve ().parent / "schemas" / xsd_file
34
34
the_schema = xmlschema .XMLSchema (Path (schemas_file ))
35
35
36
- LOGGER .debug (f"Validate { filename } against the_schema in { schemas_file } " )
36
+ LOGGER .debug (f"Validating { filename } against the schema in { schemas_file } " )
37
37
try :
38
38
result = the_schema .validate (filename )
39
39
except Exception as e :
40
40
LOGGER .debug (f"Failed to validate { filename } against { xsd_file } . Exception { e } " )
41
- result = "Fail"
41
+ result = False
42
42
return result is None
43
43
44
44
@@ -54,7 +54,6 @@ def validate_spdx(filename):
54
54
Returns:
55
55
bool: True if the SPDX file is valid according to the schema, False otherwise.
56
56
"""
57
-
58
57
SPDX_SCHEMA = "spdx.xsd"
59
58
return _validate_xml (filename , SPDX_SCHEMA )
60
59
0 commit comments