próbowałem zarejestrować nazw z tego:elementtree błąd nazw rejestr
ET.register_namespace("inv", "http://www.stormware.cz/schema/version_2/invoice.xsd")
ale to nie działa:
Traceback (most recent call last):
File "C:\tutorial\temp_xml2.py", line 34, in module>
for listInvoice in root.findall('inv:invoiceHeader'):
File "C:\Python27\LIB\xml\etree\ElementTree.py", line 390, in findall
return ElementPath.findall(self, path, namespaces)
File "C:\Python27\LIB\xml\etree\ElementPath.py", line 293, in findall
return list(iterfind(elem, path, namespaces))
File "C:\Python27\LIB\xml\etree\ElementPath.py", line 259, in iterfind
token = next()
File "C:\Python27\LIB\xml\etree\ElementPath.py", line 83, in xpath_tokenizer
raise SyntaxError("prefix %r not found in prefix map" % prefix)
SyntaxError: prefix 'inv' not found in prefix map
>>>
co z tego?
Dzięki Martinj
próbowałem - 1 .:
for listInvoice in root.findall('inv:invoiceHeader', namespaces=dict(inv='http://www.stormware.cz/schema/version_2/invoice.xsd')):
invoiceHeader = listInvoice.find('inv:id', namespaces=dict(inv='http://www.stormware.cz/schema/version_2/invoice.xsd')).text
print invoiceHeader
Wynik: (pusty)
2 .:
nsmap=root.nsmap
print nsmap
Wynik: AttributeError: " Obiekt "Element" nie ma atrybutu "ns mapa”
3 .:
for listInvoice in root.findall('.//{http://www.stormware.cz/schema/version_2/invoice.xsd}invoiceHeader'):
invoiceHeader = listInvoice.find('.//{http://www.stormware.cz/schema/version_2/invoice.xsd}id').text
print invoiceHeader
Wynik: Działa OK.
Czy istnieje szansa na zarejestrowanie przestrzeni nazw jednocześnie? Następnie chciałbym użyć listInvoice.find ("inv: id"). Text zamiast listInvoice.find ('.// {http://www.stormware.cz/schema/version_2/invoice.xsd} id') .text (ładniejszy kod i łatwe do odczytania)
Ta odpowiedź wygląda bardzo podobnie do twojej http://stackoverflow.com/a/12861866/735204 –