sissaschool / xmlschema

XML Schema validator and data conversion library for Python
MIT License
423 stars 72 forks source link

Unable to load CSW schema from https://schemas.opengis.net/csw/2.0.2/csw.xsd #425

Open lion1109 opened 1 month ago

lion1109 commented 1 month ago

You may reproduce the error by:

import xmlschema from xmlschema.exceptions import XMLSchemaException schema_url = 'https://schemas.opengis.net/csw/2.0.2/csw.xsd' xml_schema = xmlschema.XMLSchema(schema_url)

The resulting output is:

C:\Users\eduard\miniconda3\envs\s4c\python.exe C:\Users\eduard\soil4climate\soil4climate\DataProvider\test\TestValidation.py Traceback (most recent call last): File "C:\Users\eduard\soil4climate\soil4climate\DataProvider\test\TestValidation.py", line 8, in xml_schema = xmlschema.XMLSchema(schema_url) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\eduard\miniconda3\envs\s4c\Lib\site-packages\xmlschema\validators\schemas.py", line 522, in init self.maps.build() File "C:\Users\eduard\miniconda3\envs\s4c\Lib\site-packages\xmlschema\validators\global_maps.py", line 682, in build self.check(filter(lambda x: x.meta_schema is not None, not_loaded_schemas), self.validation) File "C:\Users\eduard\miniconda3\envs\s4c\Lib\site-packages\xmlschema\validators\global_maps.py", line 735, in check xsd_type.parse_error(msg, validation=validation) File "C:\Users\eduard\miniconda3\envs\s4c\Lib\site-packages\xmlschema\validators\xsdbase.py", line 204, in parse_error raise error xmlschema.validators.exceptions.XMLSchemaParseError: the derived group is an illegal restriction:

Schema component:

Basic encoding for coordinate system objects, simplifying and restricting the DefinitionType as needed.

Path: /schema/complexType[4]

Schema URL: http://schemas.opengis.net/gml/3.1.1/base/coordinateSystems.xsd

Origin URL: https://schemas.opengis.net/csw/2.0.2/csw.xsd

My environment is: Win11, PyCharm, Miniconda: (s4c) C:\Users\eduard\miniconda3>conda list

packages in environment at C:\Users\eduard\miniconda3\envs\s4c:

#

Name Version Build Channel

aiohappyeyeballs 2.4.0 py312haa95532_0 aiohttp 3.10.5 py312h827c3e9_0 aiosignal 1.2.0 pyhd3eb1b0_0 annotated-types 0.6.0 py312haa95532_0 anyio 4.2.0 py312haa95532_0 argcomplete 3.5.1 pypi_0 pypi attrs 24.2.0 pyh71513ae_0 conda-forge aws-c-auth 0.7.31 hce3b56f_0 conda-forge aws-c-cal 0.7.4 hf1fc857_1 conda-forge aws-c-common 0.9.28 h2466b09_0 conda-forge aws-c-compression 0.2.19 hf1fc857_1 conda-forge aws-c-event-stream 0.4.3 hd0ca3c1_2 conda-forge aws-c-http 0.8.10 heca9ddf_0 conda-forge aws-c-io 0.14.18 h3831a8d_9 conda-forge aws-c-mqtt 0.10.5 hf27581b_1 conda-forge aws-c-s3 0.6.5 h56e9fbd_7 conda-forge aws-c-sdkutils 0.1.19 hf1fc857_3 conda-forge aws-checksums 0.1.20 hf1fc857_0 conda-forge aws-crt-cpp 0.28.3 he121d5a_3 conda-forge aws-sdk-cpp 1.11.379 h25dd3c2_11 conda-forge azure-core-cpp 1.13.0 haf5610f_0 conda-forge azure-identity-cpp 1.8.0 h148e6f0_2 conda-forge azure-storage-blobs-cpp 12.12.0 hf03c1c4_0 conda-forge azure-storage-common-cpp 12.7.0 h148e6f0_1 conda-forge blosc 1.21.6 h85f69ea_0 conda-forge bzip2 1.0.8 h2bbff1b_6 c-ares 1.33.1 h2466b09_0 conda-forge ca-certificates 2024.8.30 h56e8100_0 conda-forge cachecontrol 0.14.0 pypi_0 pypi cairo 1.18.0 h32b962e_3 conda-forge certifi 2024.8.30 pypi_0 pypi cfitsio 4.4.1 hc2ea260_0 conda-forge charset-normalizer 3.3.2 pypi_0 pypi click 8.1.7 win_pyh7428d3b_0 conda-forge click-plugins 1.1.1 py_0 conda-forge cligj 0.7.2 pyhd8ed1ab_1 conda-forge colorama 0.4.6 pyhd8ed1ab_0 conda-forge coloredlogs 15.0.1 pypi_0 pypi contourpy 1.3.0 pypi_0 pypi cwl-upgrader 1.2.11 pypi_0 pypi cwl-utils 0.35 pypi_0 pypi cwltool 3.1.20241007082533 pypi_0 pypi cycler 0.12.1 pypi_0 pypi descartes 1.1.0 pypi_0 pypi elementpath 4.5.0 pypi_0 pypi expat 2.6.3 h5da7b33_0 fastapi 0.112.2 py312haa95532_0 filelock 3.16.1 pypi_0 pypi fiona 1.10.1 py312hd215820_1 conda-forge fmt 11.0.2 h7f575de_0 conda-forge font-ttf-dejavu-sans-mono 2.37 hab24e00_0 conda-forge font-ttf-inconsolata 3.000 h77eed37_0 conda-forge font-ttf-source-code-pro 2.038 h77eed37_0 conda-forge font-ttf-ubuntu 0.83 h77eed37_2 conda-forge fontconfig 2.14.2 hbde0cde_0 conda-forge fonts-conda-ecosystem 1 0 conda-forge fonts-conda-forge 1 0 conda-forge fonttools 4.53.1 pypi_0 pypi freetype 2.12.1 hdaf720e_2 conda-forge freexl 2.0.0 h8276f4a_0 conda-forge frozenlist 1.4.0 py312h2bbff1b_0 gdal 3.9.2 py312h16ac12d_3 conda-forge geos 3.12.2 h5a68840_1 conda-forge geotiff 1.7.3 h496ac4d_3 conda-forge h11 0.14.0 py312haa95532_0 hdf4 4.2.15 h5557f11_7 conda-forge hdf5 1.14.3 nompi_h2b43c12_105 conda-forge humanfriendly 10.0 pypi_0 pypi icu 75.1 he0c23c2_0 conda-forge idna 3.10 pypi_0 pypi intel-openmp 2024.2.1 h57928b3_1083 conda-forge isodate 0.6.1 pypi_0 pypi kealib 1.5.3 h6c43f9b_2 conda-forge kiwisolver 1.4.7 pypi_0 pypi krb5 1.21.3 hdf4eb48_0 conda-forge lcms2 2.16 h67d730c_0 conda-forge lerc 4.0.0 h63175ca_0 conda-forge libabseil 20240116.2 cxx17_he0c23c2_1 conda-forge libaec 1.1.3 h63175ca_0 conda-forge libarchive 3.7.4 haf234dc_0 conda-forge libblas 3.9.0 23_win64_mkl conda-forge libcblas 3.9.0 23_win64_mkl conda-forge libcrc32c 1.1.2 h0e60522_0 conda-forge libcurl 8.10.1 h1ee3ff0_0 conda-forge libdeflate 1.21 h2466b09_0 conda-forge libexpat 2.6.3 he0c23c2_0 conda-forge libffi 3.4.4 hd77b12b_1 libgdal 3.9.2 h57928b3_3 conda-forge libgdal-core 3.9.2 h968bd71_3 conda-forge libgdal-fits 3.9.2 h0a0b71e_3 conda-forge libgdal-grib 3.9.2 hd2a089b_3 conda-forge libgdal-hdf4 3.9.2 h430f241_3 conda-forge libgdal-hdf5 3.9.2 had131a1_3 conda-forge libgdal-jp2openjpeg 3.9.2 hed4c6cb_3 conda-forge libgdal-kea 3.9.2 h95b1a77_3 conda-forge libgdal-netcdf 3.9.2 h55e78d3_3 conda-forge libgdal-pdf 3.9.2 ha1c78db_3 conda-forge libgdal-pg 3.9.2 ha693a0f_3 conda-forge libgdal-postgisraster 3.9.2 ha693a0f_3 conda-forge libgdal-tiledb 3.9.2 hb8b5d01_3 conda-forge libgdal-xls 3.9.2 hd0e23a6_3 conda-forge libglib 2.80.3 h7025463_2 conda-forge libgoogle-cloud 2.29.0 h5e7cea3_0 conda-forge libgoogle-cloud-storage 2.29.0 he5eb982_0 conda-forge libgrpc 1.62.2 h5273850_0 conda-forge libhwloc 2.11.1 default_h8125262_1000 conda-forge libiconv 1.17 hcfcfb64_2 conda-forge libintl 0.22.5 h5728263_3 conda-forge libjpeg-turbo 3.0.0 hcfcfb64_1 conda-forge libkml 1.3.0 h538826c_1021 conda-forge liblapack 3.9.0 23_win64_mkl conda-forge libnetcdf 4.9.2 nompi_h92078aa_114 conda-forge libpng 1.6.44 h3ca93ac_0 conda-forge libpq 16.4 hab9416b_1 conda-forge libprotobuf 4.25.3 h503648d_0 conda-forge libre2-11 2023.09.01 hf8d8778_2 conda-forge librttopo 1.1.0 h6c42fcb_16 conda-forge libspatialite 5.1.0 h655691a_10 conda-forge libsqlite 3.46.1 h2466b09_0 conda-forge libssh2 1.11.0 h7dfc565_0 conda-forge libtiff 4.6.0 hb151862_4 conda-forge libwebp-base 1.4.0 hcfcfb64_0 conda-forge libxml2 2.12.7 h0f24e4e_4 conda-forge libzip 1.10.1 h1d365fa_3 conda-forge libzlib 1.3.1 h2466b09_1 conda-forge loguru 0.7.2 pypi_0 pypi lxml 5.3.0 pypi_0 pypi lz4-c 1.9.4 hcfcfb64_0 conda-forge lzo 2.10 hcfcfb64_1001 conda-forge matplotlib 3.9.2 pypi_0 pypi minizip 4.0.6 hb638d1e_0 conda-forge mistune 3.0.2 pypi_0 pypi mkl 2024.1.0 h66d3029_694 conda-forge msgpack 1.1.0 pypi_0 pypi multidict 6.0.4 py312h2bbff1b_0 mypy-extensions 1.0.0 pypi_0 pypi networkx 3.4.1 pypi_0 pypi numpy 2.1.1 py312h49bc9c5_0 conda-forge ogc-ap-validator 0.5.0 pypi_0 pypi opencv-contrib-python 4.10.0.84 pypi_0 pypi openjpeg 2.5.2 h3d672ee_0 conda-forge openssl 3.3.2 h2466b09_0 conda-forge owslib 0.31.0 pypi_0 pypi packaging 24.1 pypi_0 pypi pcre2 10.44 h3d7b363_2 conda-forge pillow 10.4.0 pypi_0 pypi pip 24.2 py312haa95532_0 pixman 0.43.4 h63175ca_0 conda-forge platformdirs 4.3.6 pypi_0 pypi poppler 24.08.0 h9415970_1 conda-forge poppler-data 0.4.12 hd8ed1ab_0 conda-forge postgresql 16.4 hd835ec0_1 conda-forge proj 9.5.0 hd9569ee_0 conda-forge prov 1.5.1 pypi_0 pypi psutil 6.0.0 pypi_0 pypi pthreads-win32 2.9.1 hfa6e2cd_3 conda-forge pydantic 2.8.2 py312haa95532_0 pydantic-core 2.20.1 py312hefb1915_0 pydentic 0.0.1.dev3 pypi_0 pypi pydot 2.0.0 pypi_0 pypi pyparsing 3.1.4 pyhd8ed1ab_0 conda-forge pyproj 3.6.1 pypi_0 pypi pyreadline3 3.5.4 pypi_0 pypi python 3.12.5 h889d299_0_cpython conda-forge python-dateutil 2.9.0.post0 pypi_0 pypi python-stdnum 1.20 pypi_0 pypi python_abi 3.12 5_cp312 conda-forge pytz 2024.2 pypi_0 pypi pyyaml 6.0.2 pypi_0 pypi rdflib 7.0.0 pypi_0 pypi re2 2023.09.01 hd3b24a8_2 conda-forge requests 2.32.3 pypi_0 pypi requests-file 2.1.0 pypi_0 pypi requests-toolbelt 1.0.0 pypi_0 pypi ruamel-yaml 0.18.6 pypi_0 pypi ruamel-yaml-clib 0.2.8 pypi_0 pypi schema-salad 8.7.20241010092723 pypi_0 pypi scipy 1.14.1 pypi_0 pypi setuptools 72.1.0 py312haa95532_0 shapely 2.0.6 py312h3a88d77_1 conda-forge six 1.16.0 pypi_0 pypi snappy 1.2.1 h23299a8_0 conda-forge sniffio 1.3.0 py312haa95532_0 spdlog 1.14.1 h9f2357e_1 conda-forge spython 0.3.14 pypi_0 pypi sqlite 3.45.3 h2bbff1b_0 starlette 0.38.2 py312haa95532_0 tbb 2021.13.0 hc790b64_0 conda-forge tiledb 2.26.1 hfc7a24e_0 conda-forge tk 8.6.13 h5226925_1 conda-forge tornado 6.4.1 pypi_0 pypi typing-extensions 4.11.0 py312haa95532_0 typing_extensions 4.11.0 py312haa95532_0 tzdata 2024a h04d1e81_0 ucrt 10.0.22621.0 h57928b3_0 conda-forge uriparser 0.9.8 h5a68840_0 conda-forge urllib3 2.2.3 pypi_0 pypi uvicorn 0.20.0 py312haa95532_0 vc 14.40 h2eaa2aa_1 vc14_runtime 14.40.33810 ha82c5b3_21 conda-forge vs2015_runtime 14.40.33810 h3bf8584_21 conda-forge wheel 0.44.0 py312haa95532_0 win32-setctime 1.1.0 pypi_0 pypi xerces-c 3.2.5 he0c23c2_1 conda-forge xmlschema 3.4.2 pypi_0 pypi xz 5.4.6 h8cc25b3_1 yarl 1.11.0 py312h827c3e9_0 zeep 4.2.1 pypi_0 pypi zlib 1.3.1 h2466b09_1 conda-forge zstd 1.5.6 h0ea2cb4_0 conda-forge

brunato commented 1 month ago

Hi, this is related to a case of W3C XSD tests that is still in disputed state (see bug 4147).

I consider this valid only for XSD 1.1 where the equivalent choice group derivation is valid. So you have to use XMLSchema11 class to build the schema.

For disputed situations I will consider if adding a strict=False option could help.

brunato commented 1 month ago

For disputed situations I will consider if adding a strict=False option could help.

Nope, the XMLSchema/XMLSchema11 classes already have validation='strict' option. For controversial cases or also if the schema has in fact invalid restrictions validation='lax' can be provided.

>>> import xmlschema
>>> schema_url = 'https://schemas.opengis.net/csw/2.0.2/csw.xsd'
>>> schema = xmlschema.XMLSchema(schema_url, validation='lax')
>>>

The created schema is usable for validation in strict mode, with the limitation that effectively wrong restrictions can produce inconsistencies between validations with the base type and derived types.

For this case there are no differences, anyway you can use XMLSchema11 as well. With lax mode the schema parse errors can be retrieved using schema.all_errors (schema.maps.all_errors to get also the errors of included/imported schemas).