[英]retrieving attribute values from all elements in XPATH
I have an XML File which has a root called "Node", and the rest of the tags are called Variables, Shapes etc.我有一个 XML 文件,它的根名为“节点”,标签的 rest 称为变量、形状等。
<Node Name="X0578">
<Dynamo Name="grpConditionDispAlarm" Description="" Class="Group" Category="Shape">
<ContainedObjects>
<Shape Name="grpSettings18" Description="" Class="Group" Category="Shape">
<ContainedObjects>
<Shape Name="Text279" Description="" Class="Text" Category="Shape">
</Shape>
</ContainedObjects>
</Shape>
</ContainedObjects>
</Dynamo>
</Node>
When I try to parse the XML and get a list of Shapes with their attribute "Name" and their XPATH (which i am getting it from tree.getpath(shapes)
)当我尝试解析 XML 并获取具有属性“名称”的形状列表及其 XPATH (我从
tree.getpath(shapes)
获取它)
I get a list我得到一个清单
/Node/Dynamo[1]/ContainedObjects/Shape[1]
/Node/Dynamo[1]/ContainedObjects/Shape[1]/ContainedObjects/Shape
My files contain multiple instances of tag called Dynamo
.我的文件包含多个名为
Dynamo
的标签实例。 I need to get a list of all those Dynamos with each shape that's inside.我需要获取所有包含内部每个形状的 Dynamo 的列表。
for dynamo in source_tree.iterfind(".//Dynamo"):
dynamo_tree = etree.ElementTree(dynamo)
object_list = dynamo_tree.findall(".//Shape")
for each_obj in object_list:
### Extract Info
Obj_Name = each_obj.attrib["Name"]
obj_Path = source_tree.getpath(each_obj)
## Uses Pandas DF to save the data
Output as below: Output 如下:
Object Name ![]() |
XPATH ![]() |
---|---|
grpSettings18 ![]() |
/Node/Dynamo[1]/ContainedObjects/Shape[1] ![]() |
Text279![]() |
/Node/Dynamo[1]/ContainedObjects/Shape[1]/ContainedObjects/Shape ![]() |
Now, technically for us, the XPATH doesn't make much sense.现在,从技术上讲,对我们来说,XPATH 没有多大意义。
But every Shape, Dynamo has an Attribute Called "Name".但是每个Shape,Dynamo都有一个名为“Name”的属性。
So we want to replace that Dynamo[1], Shape[1], Shape in that XPATH with their respective attribute value "Name".所以我们想用它们各自的属性值“名称”替换 XPATH 中的 Dynamo[1]、Shape[1]、Shape。
def resolvepath(docroot, shape_path):
lstPath = shape_path.split('/')
lstPath.pop(0)
strxpath = "/"
resolvepath = "/"
for elem in lstPath:
if not elem == 'ContainedObjects':
strxpath = strxpath + '/' + elem
resolvepath = resolvepath + '/' + docroot.xpath(strxpath)[0].attrib['Name']
elif elem == 'ContainedObjects':
strxpath = strxpath + '/' + 'ContainedObjects'
return resolvepath
I used the above logic to iterate through Objects XPath and create a new string resolvepath
which then replaces Dynamo[1]
, Shape[1]
with their attrib.我使用上述逻辑遍历对象
resolvepath
并创建一个新的字符串解析路径,然后将Dynamo[1]
、 Shape[1]
替换为其属性。 Name
. Name
。
Output as Below: Output 如下:
Object Name ![]() |
XPATH ![]() |
Node Name![]() |
Dynamo Name![]() |
Resolved Path![]() |
---|---|---|---|---|
grpSettings18 ![]() |
/Node/Dynamo[1]/ContainedObjects/Shape[1] ![]() |
X0579 ![]() |
grpConditionDispAlarm ![]() |
//X0579/grpConditionDispAlarm/grpSettings18 ![]() |
Text279![]() |
/Node/Dynamo[1]/ContainedObjects/Shape[1]/ContainedObjects/Shape ![]() |
X0579 ![]() |
grpConditionDispAlarm ![]() |
//X0579/grpConditionDispAlarm/grpSettings18/Text279 ![]() |
Now my main concern is that def resolvepath
takes a lot of time when we consider that depth of shapes may go till 7-8 level.现在我主要担心的是,当我们考虑到形状的深度可能 go 直到 7-8 级时,
def resolvepath
需要很多时间。
Is it possible to resolve the XPATH and get a path according to their Element & Attribute Value?
是否可以解决 XPATH 并根据其元素和属性值获取路径?
You mean something like你的意思是
/*[@Name="RandomNodeName"]/ContainedObjects/*[@Name="RandomName32"]/ContainedObjects/*[@Name="RandomName33"]/ContainedObjects/*[@Name="RandomName34"]
? ?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.