Domanda

I have a valid XHTML file. When I do

import xml.etree.ElementTree as ET
print ET._namespace_map

it lists:

'http://www.w3.org/1999/xhtml': 'html'

When I do:

root.find('{http://www.w3.org/1999/xhtml}head')

it finds:

<Element '{http://www.w3.org/1999/xhtml}head' at 0x104647168>

But when I do:

root.find('html:head')

it complains:

SyntaxError: prefix 'html' not found in prefix map

Is it possible to find an name-spaced-element with find using the syntax ns:element?

È stato utile?

Soluzione

You should specify namespaces argument:

namespaces = {'html': 'http://www.w3.org/1999/xhtml'}
root.find('html:head', namespaces=namespaces)

Also see:

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top