Closed MosGeo closed 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)
{}
@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.
@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.
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.
Hi,
This is probably a simple question but I cannot figure it out. I am trying to extract the attributes below
I already tried doing something like this but it doesn't work
Any help and guidance would be appreciated. Thanks!