我如何找到兄弟姐妹xml.dom.minidom?

2024-09-30 16:32:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个块SKOS文件,我正试图用它来读取xml.dom.minidom. 下面是一个示例条目:

<rdf:Description rdf:about="http://...">
     <rdf:type rdf:resource="http://www.w3.org/2004/02/skos/core#Concept"/>
     <skos:narrowMatch rdf:resource="http://dbpedia.org/resource/Biology"/>
     <skos:narrowMatch rdf:resource="http://rdf.freebase.com/ns/m.01540"/>
     <skos:prefLabel xml:lang="en">Biology and Biochemistry</skos:prefLabel>
     <skos:scopeNote xml:lang="en">Used for all coverage of biology and biochemistry unless a more narrow term applies.</skos:scopeNote>
</rdf:Description>

我可以访问所有skos:预标签有点像。。。在

^{pr2}$

但我要的是skos:范围注释也是。我是不是用错了工具?在


Tags: andorghttplangrdfdescriptionxmlskos
2条回答

我不知道更好的方法,但我会做以下事情:

  1. 获取父节点
  2. 从父对象中搜索'skos:范围注释'

代码如下:

for element in doc.getElementsByTagName('skos:prefLabel'):
    print element.firstChild.data
    sibbling = element.parentNode.getElementsByTagName('skos:scopeNote')[0]
    print sibbling.firstChild.data

讨论

  • 由于getElementsByTagName()返回一个列表,而且我确信在父节点下有一个同名的节点,所以我继续并获取第一个节点(索引[0]
  • 我尝试了element.nextSibbling,但它将新行作为“node”返回。我可以一直查询下一个兄弟姐妹,直到找到我要查找的内容,但这需要大量代码。此外,无法保证scopeNote将跟随prelabel,因此更安全的做法是转到父级进行搜索。在

你可以试试这个

discriptions = doc.getElementsByTagName("rdf:Description")
for dis in discriptions:
    siblings = dis.childNodes
    for sib in siblings:
        if str(sib.nodeName)=="skos:prefLabel" :
            preflabel = sib.firstChild.data
        if str(sib.nodeName)=="skos:scopeNote":
            scopenote = sib.firstChild.data

相关问题 更多 >