2017-03-20 26 views
0

我需要lxml做兩件事: 1)列出xml文件中使用的所有各種前綴; 2)指定前綴後,讓lxml返回給我所有的元素命名其多個屬性。lxml只抓取前綴,然後返回元素名稱

對於這個LXML:

<pref:MiscDetails contentRef='01-01_2016' misc='wha'>1000</pref:MiscDetails> 
<pref:TestingThis contentRef='03-02_2017' misc='t' qual='5'>50</pref:TestingThis> 
<pref:AnotherExample contentRef='01-01_2015' misc='x'>100000</pref:AnotherExample> 
<test:AFinalExample contentRef='' te='t'>test</test:AFinalExample> 

代碼應該先告訴我,在這個文件中的前綴是「期望」和「測試」,然後我想要的代碼列出元素的名稱和它們的屬性與「pref」相關聯,然後是「test」。

輸出1:

"Listing prefixes:" 
"pref" 
"test" 

輸出2:

"Listing the prefix 'pref' element names and their attributes:" 
"Element MiscDetails with attributes contentRef='01-01_2016' misc='wha'" 
"Element TestingThis with attributes contentRef='03-02_2017' misc='t' qual='5'" 
"Element AnotherExample with attributes contentRef='01-01_2015' misc='x'" 

"Listing the prefix 'test' element names and their attributes:" 
"Element AFinalExample with attributes contentRef='' te='t'" 

謝謝!

+0

到目前爲止您嘗試了什麼? – Marat

回答

1

在文檔或元素的nsmap屬性將列出任何命名空間前綴:

使用iter(){namespace-uri}*返回該命名空間的所有元素(必須使用這裏的URI,這是有意義的一部分命名空間,沒有前綴,這僅僅是用於人的方便):

>>> doc = etree.fromstring("<doc xmlns:pref='http://example.com'> 
<pref:foo/><pref:bar/></doc>") 
>>> [ el.tag for el in doc.iter('{http://example.com}*') ] 
['{http://example.com}foo', '{http://example.com}bar'] 

的lxml的文檔中的更多信息:http://lxml.de/tutorial.html#namespaces

+0

謝謝,我不得不使用getroot(),但現在它的工作原理! – JackBurton

+0

Next q:如何找到與每個前綴關聯的所有元素? – JackBurton

+0

增加了一個額外的例子。 –

相關問題