Closed ndmitri closed 2 months ago
@ndmitri Hi,
Namespace map is not inherited by nested models, so namespace map must be defined explicitly for each sub-model. This code should work correctly:
>>> from pydantic_xml import BaseXmlModel, element
>>> from typing import List
>>>
>>> NSMAP = {'': 'http://cisco.com/ns/yang/cisco-nx-os-device'}
>>>
>>> class InstList(BaseXmlModel, tag='Inst-list', nsmap=NSMAP):
... name: int = element()
...
>>> class InstItems(BaseXmlModel, tag='inst-items', nsmap=NSMAP):
... inst_list: List[InstList]
...
>>> class OspfItems(BaseXmlModel, tag='ospf-items', nsmap=NSMAP):
... inst_items: InstItems
...
>>> class System(BaseXmlModel, nsmap=NSMAP):
... ospf_items: OspfItems
...
>>> xml_data = """
... <System xmlns="http://cisco.com/ns/yang/cisco-nx-os-device">
... <ospf-items>
... <inst-items>
... <Inst-list>
... <name>100</name>
... </Inst-list>
... <Inst-list>
... <name>200</name>
... </Inst-list>
... </inst-items>
... </ospf-items>
... </System>
... """
>>>
>>> system = System.from_xml(source=xml_data)
>>> print(system)
ospf_items=OspfItems(inst_items=InstItems(inst_list=[InstList(name=100), InstList(name=200)]))
@dapper91, thank you very much for explaining. :)
I am experiencing issues with parsing XML using the pydantic-xml library, specifically when namespaces are involved. The parsing works correctly without namespaces, but fails when I include
nsmap
to handle namespaces.Code Example Without
nsmap
(Works Correctly)Output
ospf_items=OspfItems(inst_items=InstItems(inst_list=[InstList(name=100), InstList(name=200)]))
Code Example With nsmap (Fails)
Error Message:
Expected Behavior I expect the XML parsing to handle namespaces similarly to how it handles non-namespaced XML.
Actual Behavior When namespaces are introduced, the parsing fails with a ValidationError indicating that required fields are missing.
Environment Python version: 3.10 pydantic-xml version: 2.9.2
Questions Is there a specific way to configure nsmap or related settings to handle namespaces correctly in this context? Are there known issues with namespace handling in pydantic-xml that might be causing this behavior? Any help or guidance on resolving this namespace handling issue would be greatly appreciated!