0

I need to parse and get the specific attribute for the given attribute from the XML by using findall function in Element Tree lib. For Example,

<Mapping>
<Map name="map1" sys_id="QAE567-hKLO890" path="\\sys1\map1\" uuid="EL8976-hF056" />
<Map name="map2" sys_id="RTY897-RT643DF" path="\\sys2\map2\" uuid="Jkl56W-yKP87" />
...
...
...
</Mapping>

We using following query to get the 'sys_id' and 'path' attribute for given uuid from the xml, but in some cases we are providing uuid with different case 'el8976-hf056' instead of 'EL8976-hF056'. In this case we are not able to get the path and sys_id of that uuid.

Elms = root.findall(".//Mapping/Map[@uuid='%s']" % "el8976-hf056")

How can we get the exact path and sys_id with correct case ?

Venkatesh
  • 171
  • 1
  • 1
  • 10

1 Answers1

1

If the value in your XML is always in upper-case, you can just convert the provided uuid to upper-case first :

Elms = root.findall(".//Mapping/Map[@uuid='%s']" % "el8976-hf056".upper())

or use lxml library if you really need to fiter the attribute value in case-insensitive way, as shown in the following thread : Is it possible for lxml to work in a case-insensitive manner?

update :

Using ElementTree you'll need to implement the case-insensitive checking logic in python:

  1. Loop through root.findall(".//Mapping/Map")
  2. Inside the for loop body, check the corresponding uuid attribute value case-insensitively and take action accordingly. For reference: How do I do a case insensitive string comparison in Python?
Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137