xmlschema: XML Schema Download incorrectly modifies the schema
I am trying to download XML schema from a remote URL and it seems to be modifying one of the schema document incorrectly.
Here’s a snippet of the code to reproduce the issue:
import xmlschema
import os.path
import urllib
def main():
# Schema Base URL resource
xsd_base_uri = "http://www.accellera.org/XMLSchema/IPXACT/1685-2022/index.xsd"
# Extract Path from URI
path = urllib.parse.urlparse(xsd_base_uri).path
print(path)
# Split path into path + resource name
schema_path = os.path.split(path)
print(schema_path)
target_path = f"schemas/{schema_path[0]}"
# Create Directory if it doesn't exists:
os.makedirs(target_path, exist_ok=True)
local_target_path = f"{target_path}/{schema_path[1]}"
if not os.path.isfile(f"{local_target_path}"):
schema = xmlschema.XMLSchema(xsd_base_uri)
schema.export(target=target_path, save_remote=True)
schema = xmlschema.XMLSchema(local_target_path)
if __name__ == '__main__':
main()
The library seems to be modifying the xs:import line in the autoConfigure.xsd document:
The Left side is the original file downloaded from the url: http://www.accellera.org/XMLSchema/IPXACT/1685-2022/autoConfigure.xsd
Because of the edit the XML Validation would fail due to incorrect XML Schema specification with the following error:
xmlschema.validators.exceptions.XMLSchemaParseError: the QName 'xml:id' is mapped to the namespace 'http://www.w3.org/XML/1998/namespace', but this namespace has not an xs:import statement in the schema:
Schema component:
<xs:attribute xmlns:xs="http://www.w3.org/2001/XMLSchema" ref="xml:id" />
Path: /xs:schema/xs:attributeGroup[2]/xs:attribute
For now there are other changes also but are of no significant impact. Is there an option to download the XML Schemas without editing?
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 22 (14 by maintainers)
Commits related to this issue
- Fix schema export (issue #387) - Fix the replacement pattern in export_schema() - Add loglevel argument, apply with a decorator - Add logger.debug statements - Don't remove non-remote residua... — committed to sissaschool/xmlschema by brunato 3 years ago
- Fix schema XSD sources export/download (issue #387) - Modify dataclass XsdSource: now takes a path and an XMLResorce, other attributes are set in __init__; - Schema locations now are extracte... — committed to sissaschool/xmlschema by brunato 3 years ago
Sorry, for the delay. Closing the issue.
Changing that for export is not recommendable because xml.xsd is already included in base schema set, so the xmlschema library doesn’t need to save another copy of xml.xsd. If you want you can try an export after creating the schema providing
use_meta=False.Anyway the
download_schema()API will download all the referred XSD resources.Ok, maybe better to abandon regex for extracting schemaLocation list from text and use an iteration on ElementTree structure instead. For the next bugfix release.
thank you
@AmeyaVS: I will not change schema export for downloading skipped schemas like the case of xml.xsd, but in the next minor release I will add a new API
download_schemas()for download a set of schemas giving an URL as the starting point.