tefra / xsdata

Naive XML & JSON Bindings for python
https://xsdata.readthedocs.io
MIT License
310 stars 56 forks source link

schema check on root element #1052

Closed Thomasb81 closed 1 month ago

Thomasb81 commented 1 month ago

Hello

I try to use xsdata library with following schema : http://www.accellera.org/XMLSchema/IPXACT/1685-2022/index.xsd

I generate the data model with following command:

 xsdata schema/2022/index.xsd --package tgi2022 --structure-style namespaces

This schema define 9 different root element image

with following code:

from lxml import etree
from xsdata.formats.dataclass.parsers.handlers import LxmlEventHandler
from xsdata.formats.dataclass.parsers import XmlParser

from tgi2022.org.accellera.xmlschema.ipxact.mod_1685_2022 import Component

ns = {'ipxact':"http://www.accellera.org/XMLSchema/IPXACT/1685-2022"}

parser = XmlParser(handler=LxmlEventHandler)
tree = etree.parse("dummy_catalog.xml")
ComponentObj = parser.parse(tree,Component)
print(ComponentObj)

dummy_catalog.xml

<?xml version="1.0" encoding="UTF-8"?>
<ipxact:catalog xmlns:ipxact="http://www.accellera.org/XMLSchema/IPXACT/1685-2022" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.accellera.org/XMLSchema/IPXACT/1685-2022 http://www.accellera.org/XMLSchema/IPXACT/1685-2022/index.xsd" >
    <ipxact:vendor>test.com</ipxact:vendor>
    <ipxact:library>test_lib</ipxact:library>
    <ipxact:name>catalog</ipxact:name>
    <ipxact:version>1.0</ipxact:version>
</ipxact:catalog>

The tool produce:

Component(vendor='test.com', library='test_lib', name='catalog', version='1.0', display_name=None, short_description=None, description=None, type_definitions=None, power_domains=None, bus_interfaces=None, indirect_interfaces=None, channels=None, modes=None, address_spaces=None, memory_maps=None, model=None, component_generators=None, choices=None, file_sets=None, clearbox_elements=None, cpus=None, other_clock_drivers=None, reset_types=None, parameters=None, assertions=None, vendor_extensions=None, id=None)

Which is incorrect. If I render back the data, my root <ipxact:catalog> has been transform into <ipxact:component>

Soon as I try to load a document which is more specific, then the non compliance with schema is somehow report.

Is there a way to check root element or is that not in the scope of this library ? If not I should probably address this another dedicated library (lxml or xmlschema)

BR Thomas

tefra commented 1 month ago

Hi @Thomasb81 I think you should be using the Catalog model not the Component

from lxml import etree
from xsdata.formats.dataclass.parsers.handlers import LxmlEventHandler
from xsdata.formats.dataclass.parsers import XmlParser

from tgi2022.org.accellera.xmlschema.ipxact.mod_1685_2022 import Component, Catalog
from xsdata.formats.dataclass.serializers import XmlSerializer

ns = {'ipxact':"http://www.accellera.org/XMLSchema/IPXACT/1685-2022"}

raw = """<?xml version="1.0" encoding="UTF-8"?>
<ipxact:catalog xmlns:ipxact="http://www.accellera.org/XMLSchema/IPXACT/1685-2022" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.accellera.org/XMLSchema/IPXACT/1685-2022 http://www.accellera.org/XMLSchema/IPXACT/1685-2022/index.xsd" >
    <ipxact:vendor>test.com</ipxact:vendor>
    <ipxact:library>test_lib</ipxact:library>
    <ipxact:name>catalog</ipxact:name>
    <ipxact:version>1.0</ipxact:version>
</ipxact:catalog>"""
parser = XmlParser(handler=LxmlEventHandler)
catalogObj = parser.from_string(raw, Catalog)
print(catalogObj)

serializer = XmlSerializer()
serializer.config.indent = "  "
print(serializer.render(catalogObj, ns_map=ns))

# <ipxact:catalog xmlns:ipxact="http://www.accellera.org/XMLSchema/IPXACT/1685-2022">
#   <ipxact:vendor>test.com</ipxact:vendor>
#   <ipxact:library>test_lib</ipxact:library>
#   <ipxact:name>catalog</ipxact:name>
#   <ipxact:version>1.0</ipxact:version>
# </ipxact:catalog>
Thomasb81 commented 1 month ago

Clear ! I figure out that I can even not specify the model:

Obj = parser.from_string(raw)

Th library will choose the right model ... it is written in the doc!

Thanks Thomas