from lxml import etree as E class DenyExternal(E.Resolver): def resolve(self, url, public_id, context): raise ValueError("External resources are disabled") def parse(text): if "\n Marigold12flower\n Fenugreek0herb\n Basil7herb\n" root = parse(source) print(root.xpath("sum(packet/quantity)")) print(root.xpath("string(packet/name)")) print(root.xpath("boolean(packet[@id='missing'])"))