Closed enadeau closed 10 months ago
Of course I only needed 5 minutes after writing the issue to figure out that I wanted to use the search mode parameter to achieve the behavior I was looking for.
I would suggest to maybe have this in the quick start guide. I think for many user coming from pydantic it is an unexpected behavior.
Maybe could be part if the error message as well but that might be more complicated as they come from pydantic core
Hi @enadeau
The model uses strict element search mode by default which means the element to which a field will be bound is searched sequentially without skipping unknown elements.
More info here
This should fix your problem:
>>> import pydantic_xml
>>>
>>>
>>> class Foo(pydantic_xml.BaseXmlModel, search_mode='ordered'):
... yy: str = pydantic_xml.element()
...
>>>
>>> data = """<?xml version="1.0" encoding="UTF-8"?>
... <Foo>
... <xx>hkjh</xx>
... <yy>jhkjh</yy>
... </Foo>
... """
>>>
>>> res = Foo.from_xml(data.encode())
>>> print(res)
yy='jhkjh'
Got it thanks for your help
Hi first time user of that package so maybe I'm doing something wrong but this looks a lot like a bug to me.
When running the following snippet I get a validation error saying the field
yy
is missing but it is clearly part of the document.If I uncomment the field
xx
then the error goes away.If I forbid extra then it complains about the
xx
and theyy
field being extra.Running on 3.11.4 with the following package installed: