Closed phdowling closed 2 months ago
Solved using https://pydantic-xml.readthedocs.io/en/latest/pages/data-binding/elements.html#elements-search-mode
e.g.
from pydantic_xml import BaseXmlModel, element
from pydantic_xml.element import SearchMode
mode = SearchMode.UNORDERED # SearchMode.ORDERED also happens to work here
class MyModel1(BaseXmlModel, tag="model", extra="ignore", search_mode=mode):
field1: str = element()
# field2: str = element()
class MyModel2(BaseXmlModel, tag="model", extra="ignore", search_mode=mode):
# field1: str = element()
field2: str = element()
class MyModel3(BaseXmlModel, tag="model", extra="ignore", search_mode=mode):
field1: str = element()
# field2: str = element()
field3: str = element()
xml = """<?xml version="1.0" encoding="UTF-8"?>
<model>
<field1>1</field1>
<field2>2</field2>
<field3>3</field3>
</model>"""
m = MyModel1.from_xml(xml)
print(m)
m = MyModel2.from_xml(xml)
print(m)
m = MyModel3.from_xml(xml)
print(m)
I would like to define a model that only partially parses/validates XML according to fields that I care about, ignoring the rest. If the irrelevant fields come later on in the model, I can use
extra="ignore"
to accomplish this. However, I have not found a way to skip some fields at the beginning of the model, or intermediate fields (i.e. read field1 and field3, ignoring the fact that field2 comes inbetween).Minimal example: