$ cat test.py str = """ This is the configuration of our network in the Boston office. agatha.example.edu mail.example.edu 192.168.0.4 SMTP POP3 IMAP4 gil.example.edu www.example.edu 192.168.0.5 HTTP HTTPS baron.example.edu dns.example.edu ntp.example.edu ldap.example.edu 192.168.0.6 DNS NTP LDAP LDAPS mr-tock.example.edu fw.example.edu 192.168.0.1 firewall krosp.example.edu 192.168.0.100 krosp.wireless.example.edu 192.168.100.100 zeetha.example.edu 192.168.0.101 zeetha.wireless.example.edu 192.168.100.101 """ sel = "/child::network/child::host/child::interface/following-sibling::interface/parent::host/attribute::name" from lxml.etree import fromstring x = fromstring(str) print x.xpath("/child::network/child::host/child::interface/ancestor::host/child::interface/following-sibling::interface/preceding::host/attribute::name") $ python test.py ['agatha', 'gil', 'baron', 'mr-tock', 'krosp']