dapper91 / pydantic-xml

python xml for humans
https://pydantic-xml.readthedocs.io
The Unlicense
141 stars 14 forks source link

Load XML with arbitrary namespaces #134

Closed nick10032 closed 8 months ago

nick10032 commented 8 months ago

I have this simple model

class Model1(BaseXmlModel, tag="items"):
    item: str = element()

This model needs to load different XML with arbitrary namespaces like

<items xmlns="https://website.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <item>1</item>
    <item>2</item>
</items>

or

<items xmlns="https://another-website.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <item>1</item>
    <item>2</item>
</items>

Reading such XML results in

pydantic_xml.errors.ParsingError: root element not found (actual: {https://website.com}items, expected: items)

The problem is I don't know namespaces in advance and can't use them is nsmap, neither I control what XML we receive How can such namespaces be easily ignored?

dapper91 commented 8 months ago

@nick10032 Hi.

Unfortunately there is no way to ignore element namespaces. The simplest solution I see is to remove the document namespaces at all before parsing.