मैं इस के साथ नाम स्थान रजिस्टर करने की कोशिश की:ElementTree रजिस्टर नाम स्थान त्रुटि
ET.register_namespace("inv", "http://www.stormware.cz/schema/version_2/invoice.xsd")
लेकिन यह काम नहीं करता:
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
>>>
क्या इस के साथ गलत क्या है?
धन्यवाद Martinj
मैंने कोशिश की - 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
परिणाम: (खाली)
2 .:
nsmap=root.nsmap
print nsmap
परिणाम: AttributeError: ' तत्व 'ऑब्जेक्ट में कोई विशेषता नहीं है' एनएस नक्शा '
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
परिणाम: काम करता है ठीक।
क्या नामस्थानों को एक बार में पंजीकृत करने का कोई मौका है? फिर मैं listInvoice.find ('inv: id') का उपयोग करना चाहूंगा। ListInvoice.find ('.// {http://www.stormware.cz/schema/version_2/invoice.xsd} id' के बजाय टेक्स्ट। .text (nicer code और पढ़ने में आसान)
यह उत्तर आपके जैसा ही दिखता है http://stackoverflow.com/a/12861866/735204 –