2016-10-01 55 views
0

我已經下載了this XML文件。查找在損壞的命名空間中定義的節點

我試圖讓includingNote如下:

... 
namespaces = { "skos" : "http://www.w3.org/2004/02/skos/core#", "xml" : "http://www.w3.org/XML/1998/namespace", 
       "udc" : "http://udcdata.info/udc-schema#" } 
... 


includingNote = child.find("udc:includingNote[@xml:lang='en']", namespaces) 
if includingNote: 
    print includingNote.text.encode("utf8") 

該計劃位於here並且似乎已損壞。

有沒有一種方法可以爲每個子節點打印includingNote

回答

1

的確,skos前綴沒有在udc-scheme中聲明,但是搜索XML文檔不是問題。

下面的程序中提取639個includingNote元素:

from xml.etree import cElementTree as ET 

namespaces = {"udc" : "http://udcdata.info/udc-schema#", 
       "xml" : "http://www.w3.org/XML/1998/namespace"} 

doc = ET.parse("udcsummary-skos.rdf") 
includingNotes = doc.findall(".//udc:includingNote[@xml:lang='en']", namespaces) 

print len(includingNotes) # 639 

for i in includingNotes: 
    print i.text 

注以搜索整個文檔中的元素名稱的前面使用的findall().//


下面是通過首先找到所有Concept元素返回相同的信息的變種:

from xml.etree import cElementTree as ET 

namespaces = {"udc" : "http://udcdata.info/udc-schema#", 
       "skos" : "http://www.w3.org/2004/02/skos/core#", 
       "xml" : "http://www.w3.org/XML/1998/namespace"} 

doc = ET.parse("udcsummary-skos.rdf") 
concepts = doc.findall(".//skos:Concept", namespaces) 

for c in concepts: 
    includingNote = c.find("udc:includingNote[@xml:lang='en']", namespaces) 
    if includingNote is not None: 
     print includingNote.text 

注意使用is not None。沒有這個,它不起作用。這似乎是ElementTree的一個特點。請參閱Why does bool(xml.etree.ElementTree.Element) evaluate to False?

+0

len(includingNotes)prints 0 – xralf

+0

嗯,它適用於我。請提供更多細節。你使用的是什麼版本的Python?我使用2.7.12。 – mzjn

+1

'不是沒有'是問題。現在它可以工作。 – xralf