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を '要素 'オブジェクトには属性がありません' nsマップ」
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
結果:OK動作します。
名前空間を一度に登録する機会はありますか?次に、listInvoice.find( '.// {http://www.stormware.cz/schema/version_2/invoice.xsd} id')の代わりにlistInvoice.find( 'inv:id')。textを使用したいと思います。 .text(より読みやすいコード)
この回答はあなたのものによく似ています。http://stackoverflow.com/a/12861866/735204 –