如何在 Python 中使用 XPath 限制特定 xml 元素的开始和结束标记之间的元素提取范围? [英] How to limit the Scope of element extraction between the start and end tag of a particular xml element using XPath in Python?

查看:50
本文介绍了如何在 Python 中使用 XPath 限制特定 xml 元素的开始和结束标记之间的元素提取范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 RDF/XML 元素,想找出特定标签开头和结尾之间的所有元素.我怎么能这样做?

I have an RDF/XML Element and would like to find out all the elements between the start and end of a particular tag. How could I do that?

例如:

<cim:BaseVoltage rdf:ID="_0526B48408F744919E7C03672FCD0D71">       
<cim:BaseVoltage.isDC>false</cim:BaseVoltage.isDC>  
<cim:BaseVoltage.nominalVoltage>400.000000000</cim:BaseVoltage.nominalVoltage>    
</cim:BaseVoltage>

我想提取值 BaseVoltage.isDC 和 BaseVoltage.nominalVoltage,因为它们位于 .如前所述,这只是一个例子,我还有更多这样的开始和结束标签.

I would like to extract the values BaseVoltage.isDC and BaseVoltage.nominalVoltage, since they are between the start and end tag of . As mentioned this is just an example and I have many more such starting and ending tag.

我想过使用 Xpath 来做,但我不确定如何做.

I thought of doing it using Xpath, but am not really sure how.

推荐答案

对于这个问题,使用 XPath 解析 XML 文件似乎是一个非常糟糕的主意.Rdflib 让它变得非常简单.

Parsing the XML File using XPath seemed to be a really bad idea for the question. Rdflib makes it very easy.

import rdflib
from rdflib import Graph
from rdflib.namespace import Namespace

BASE = Namespace('http://example.org/')

graph = rdflib.Graph()
graph.parse('rdf.xml', format='xml', publicID=BASE)

for p,o in graph[BASE['#_0526B48408F744919E7C03672FCD0D71']]:
   print(p, o)

这篇关于如何在 Python 中使用 XPath 限制特定 xml 元素的开始和结束标记之间的元素提取范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆