Hi Volker,

> A week ago I formulated a quite simple lxml.objectify question on 
> stackoverflow and got no answer, yet.
> https://stackoverflow.com/questions/71212909/python-lxml-objectify-gives-no-attribute-access-to-gcocharacterstring-node
> Since I don't get an easy answer I conclude that my feeling about something 
> is wrong in lxml.objectfy is not a mare.
> So I elevate this topic to the mailing list.
> Any help appreciated
> Volker Jaenisch

This is expected and just how lxml.objectify works. Since 
{http://www.isotc211.org/2005/gco}CharacterString is from another namespace
 than its parent element ({"http://www.isotc211.org/2005/gmd}fileIdentifier) it 
cannot be retrieved from the parent with "simple (dot) attribute lookup".

Instead you have to you use index access (just like you already did) or getattr:

```
>>> from lxml import etree, objectify
>>> data = objectify.fromstring(
... """<gmd:MD_Metadata
... xmlns:gmd="http://www.isotc211.org/2005/gmd";
... xmlns:gco="http://www.isotc211.org/2005/gco"; >
...   <gmd:fileIdentifier>
...     
<gco:CharacterString>2ce585df-df23-45f6-b8e1-184e64e7e3b5</gco:CharacterString>
...   </gmd:fileIdentifier>
...   <gmd:language>
...     <gmd:LanguageCode codeList="https://www.loc.gov/standards/iso639-2/"; 
codeListValue="ger">ger</gmd:LanguageCode>
...   </gmd:language>
... </gmd:MD_Metadata>
... """)
>>> data.fileIdentifier["{http://www.isotc211.org/2005/gco}CharacterString";]
'2ce585df-df23-45f6-b8e1-184e64e7e3b5'
>>> getattr(data.fileIdentifier, 
>>> "{http://www.isotc211.org/2005/gco}CharacterString";)
'2ce585df-df23-45f6-b8e1-184e64e7e3b5'
>>>
```

The reason for this is that obviously 
{http://www.isotc211.org/2005/gco}CharacterString is not a valid Python 
identifier and it makes sense
to restrict unqualified lookup to children from the same namespace.

See also "Namespace handling" in the official docs: 
https://lxml.de/objectify.html#the-lxml-objectify-api

Best,
Holger






Landesbank Baden-Wuerttemberg
Anstalt des oeffentlichen Rechts
Hauptsitze: Stuttgart, Karlsruhe, Mannheim, Mainz
HRA 12704
Amtsgericht Stuttgart
HRA 4356, HRA 104 440
Amtsgericht Mannheim
HRA 40687
Amtsgericht Mainz

Die LBBW verarbeitet gemaess Erfordernissen der DSGVO Ihre personenbezogenen 
Daten.
Informationen finden Sie unter https://www.lbbw.de/datenschutz.
_______________________________________________
lxml - The Python XML Toolkit mailing list -- lxml@python.org
To unsubscribe send an email to lxml-le...@python.org
https://mail.python.org/mailman3/lists/lxml.python.org/
Member address: arch...@mail-archive.com

Reply via email to