2013-01-02 22 views
6

मैं इस के साथ नाम स्थान रजिस्टर करने की कोशिश की: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 और पढ़ने में आसान)

+0

यह उत्तर आपके जैसा ही दिखता है http://stackoverflow.com/a/12861866/735204 –

उत्तर

14

ऐसा लगता है कि नामस्थानों और .findall() का उपयोग करने के तरीके पर दस्तावेज़ों को अपडेट नहीं किया गया है।

.findall() समारोह (और साथ ही .find(), .findtext() and .iterfind() ) takes a namespaces` तर्क जो एक मानचित्रण माना जाता है यही कारण है कि केवल संरचना जब टैग को ढूंढने के विचार-विमर्श किया है:।

root.findall('inv:invoiceHeader', namespaces=dict(inv='http://www.stormware.cz/schema/version_2/invoice.xsd')) 

.register_namespace() समारोह जब पेड़ को फिर से पाठ करने के लिए क्रमबद्ध किया जाता है तो केवल उपयोगी होता है।

+0

धन्यवाद, यह काम करता है! '.get()' I के लिए '{नेमस्पेस-यूआरएल}' के साथ उपसर्ग करने की आवश्यकता है, उदाहरण के लिए । 'Element.get ({} http://www.w3.org/1999/02/22-rdf-syntax-ns# आईडी)'। –

+1

@ विन्सेंट: हां, विशेषता पहुंच के पास नेमस्पेस उपसर्ग अनुवाद के लिए कोई समर्थन नहीं है और आपको पूरी तरह से योग्य नेमस्पेस उपसर्ग में पास करने की आवश्यकता है। –

संबंधित मुद्दे