The provider tag is located in the http://apple.com/itunes/importer namespace, so you need to either use the full name
{http://apple.com/itunes/importer}provider
or use one of the lxml methods that has a namespaces parameter , for example root.xpath . Then you can specify it with a namespace prefix (for example, ns:provider ):
from lxml import etree parser = etree.XMLParser(remove_blank_text=True) tree = etree.parse(metadata, parser) root = tree.getroot() namespaces = {'ns':'http://apple.com/itunes/importer'} items = iter(root.xpath('//ns:provider/text()|//ns:actor/@name', namespaces=namespaces)) for provider, actor in zip(*[items]*2): print(provider, actor)
gives
('filmgroup', 'John Smith')
Note that the XPath used above assumes that the <provider> and <actor> elements always appear in rotation. If this is not the case, then there are, of course, ways to handle this, but the code becomes a bit more verbose:
for package in root.xpath('//ns:package', namespaces=namespaces): for provider in package.xpath('ns:provider', namespaces=namespaces): providerValue = provider.text print providerValue for actor in package.xpath('ns:actor', namespaces=namespaces): print actor.attrib['name']
source share