dapper91 / pydantic-xml

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

Extracting attributes with colons #49

Closed MosGeo closed 1 year ago

MosGeo commented 1 year ago

Hi,

This is probably a simple question but I cannot figure it out. I am trying to extract the attributes below

<Group xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</Group >

I already tried doing something like this but it doesn't work

class Group(BaseXmlModel, tag="Group"):
    xmlns_xsd: str = attr(name="xmlns:xsd")
    xmlns_xsi : str = attr(name="xmlns:xsi")

Any help and guidance would be appreciated. Thanks!

dapper91 commented 1 year ago

@MosGeo Hi,

You can't extract xmlns:xsd and xmlns:xsi because they are not attributes, they are namespace declaration. XML parsers don't consider them as attributes:

>>> from xml.etree import ElementTree as etree
>>> xml = '<Group xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></Group >'
>>> group = etree.fromstring(xml)
>>> print(group.attrib)
{}
MosGeo commented 1 year ago

@dapper91 thanks for the quick reply. Understood.

One last question: is there a way to include these manually when saving the object back to xml?

The only workaround that I can think of is to replace the first line manually after converting to xml text.

dapper91 commented 1 year ago

@MosGeo in most cases you don't have to add xmlns:... manually. It is included to a document automatically if that namespace is used by any of the document entity (element or attribute). For example:

>>> from pydantic_xml import BaseXmlModel, attr
>>>
>>> class Group(
...     BaseXmlModel,
...     tag="Group",
...     nsmap={
...         'xsi': 'http://www.w3.org/2001/XMLSchema-instance',
...         'xsd': 'http://www.w3.org/2001/XMLSchema',
...     }
... ):
...     xsins_attr: str = attr(name='attribute', ns='xsi')
...     xsdns_attr: str = attr(name='attribute', ns='xsd')
...
>>> g = Group(xsins_attr='value1', xsdns_attr='value2')
>>>
>>> print(g.to_xml())
b'<Group xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:attribute="value1" xsd:attribute="value2"/>'

As you can see xmlns:xsi and xmlns:xsd are included to the resulting document.

MosGeo commented 1 year ago

Thanks alot for the help. That is very clear. I will close the issue.

p.s. thanks for making the package. It is my first time using it and it is very useful and easy to use.