我需要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'"
謝謝!
到目前爲止您嘗試了什麼? – Marat