简体   繁体   中英

Getting all text nodes in xml using lxml xpath with original order

 <a> aaaa <b> bbbb </b> cccc <d> dddd <f> ffff </f> gggg </d> eeee </a> 

how to use lxml and xpath to get a list. result must be [aaaa,bbbb,cccc,dddd,fff,gggg,eeee]. I can use the function xpath(u"/descendant:: [text()]") and xpath(u"/descendant:: [text()]") to get the result, but the order is incorrect

>>> xml = "<a>aaaa<b>bbbb</b>cccc<d>dddd<f>ffff</f>gggg</d>eeee</a>"
>>> root = lxml.etree.fromstring(xml)
>>> root.xpath(".//text()")
['aaaa', 'bbbb', 'cccc', 'dddd', 'ffff', 'gggg', 'eeee']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM