attrname - get value of attrname

Hi I was following this rather old thread https://mailman-mail5.webfaction.com/pipermail/lxml/2011-May/006000.html I noticed in it this excerpt.
* I tend to rather use XPath instead of lxml's ElementPath so you might do s.th <http://s.th>. like: *>>>>>* doc.xpath('//gruppe/@*') *>* ['Einstellung1', 'feld-server', 'feld-bertlap'] *>>>>* for attrResult in doc.xpath('//gruppe/@*'): *>* ... print attrResult.attrname*
Ok so applying this to my own xml file I get it to work. In [11]: with open('20160402RAND0.xml', 'r') as f: ...: tree = ET.parse(f) ...: for attrResult in tree.xpath('//meeting/@*'): ...: print(attrResult.attrname) id barriertrial venue date gearchanges stewardsreport gearlist racebook postracestewards meetingtype rail weather trackcondition nomsdeadline weightsdeadline acceptdeadline jockeydeadline Now however I want the values associated with the attributes but how do I get that? For example I cannot seem to iter the result to get the value of the attribute. In [13]: with open('20160402RAND0.xml', 'r') as f: ...: tree = ET.parse(f) ...: for attrResult in tree.xpath('//meeting/@*'): ...: for attrval in attrResult: ...: print(attrResult.get(attrval)) --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-13-3d77432ea68d> in <module>() 3 for attrResult in tree.xpath('//meeting/@*'): 4 for attrval in attrResult: ----> 5 print(attrResult.get(attrval)) 6 AttributeError: 'lxml.etree._ElementUnicodeResult' object has no attribute 'get' Ideas? Sayth

Hi,
[...] Ok so applying this to my own xml file I get it to work. In [11]: with open('20160402RAND0.xml', 'r') as f: ...: tree = ET.parse(f) ...: for attrResult in tree.xpath('//meeting/@*'): ...: print(attrResult.attrname) id barriertrial venue date gearchanges stewardsreport gearlist racebook postracestewards meetingtype rail weather trackcondition nomsdeadline weightsdeadline acceptdeadline jockeydeadline Now however I want the values associated with the attributes but how do I get that? For example I cannot seem to iter the result to get the value of the attribute. In [13]: with open('20160402RAND0.xml', 'r') as f: ...: tree = ET.parse(f) ...: for attrResult in tree.xpath('//meeting/@*'): ...: for attrval in attrResult: ...: print(attrResult.get(attrval))
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last) <ipython-input-13-3d77432ea68d> in <module>() 3 for attrResult in tree.xpath('//meeting/@*'): 4 for attrval in attrResult: ----> 5 print(attrResult.get(attrval)) 6
AttributeError: 'lxml.etree._ElementUnicodeResult' object has no attribute 'get' Ideas?
The result of tree.xpath('//meeting/@*') is already a list of attribute *values*. These are "smart strings" i.e. strings with additional niceties (like the .attrname attribute, see http://lxml.de/xpathxslt.html#xpath) and should just work in your usual string contexts. E.g.
root = etree.fromstring("""<root><x a="a attr"/><y b="b attr"/> </root>""") root.xpath('//@*') ['a attr', 'b attr'] root.xpath('//@*')[0] 'a attr' root.xpath('//@*')[0].attrname 'a' root.xpath('//@*')[1] + " is nice to have" 'b attr is nice to have'
If need be you can always convert them to plain strings using unicode() or str(), depending on your Python version. Holger Landesbank Baden-Wuerttemberg Anstalt des oeffentlichen Rechts Hauptsitze: Stuttgart, Karlsruhe, Mannheim, Mainz HRA 12704 Amtsgericht Stuttgart
participants (2)
-
Holger Joukl
-
Sayth Renshaw