I have old and new namespaces in the XML file. When I run the job, it only works for one of the two, but I'd like to be able to handle both.
Here is an example of my old XML:
<element1 xmlns="http:/ss/dd">
<element2 xmlns="http:/uu/ee/rr">
<element3>value1</element3>
<element4>value2</element4>
</element2>
</element1>
And here is an example of my new XML:
<element1 xmlns="http:/ss/dd/version1_0">
<element2 xmlns="http:/uu/ee/rr">
<element3>value1</element3>
<element4>value2</element4>
</element2>
</element1>
In the future it is is going to be
New XML:
<element1 xmlns="http:/ss/dd/version2_0">
<element2 xmlns="http:/uu/ee/rr">
<element3>value1</element3>
<element4>value2</element4>
</element2>
</element1>
My code is:
ele3 = getvalue(xml_content,'x:element1/y:element2/y:element3',
namespace{x:'http:/ss/dd',y:http:/uu/ee/rr})
I tried this, but it didn't work:
ele3 = getvalue(xml_content,'x:element1/y:element2/y:element3',
namespace{x:'http:/ss/dd*',y:'http:/uu/ee/rr'})
my code is using the default xml parser :
def getvalue(xml_content, x_path, namespace):
data= etree.formstring(xml_content)
elements = data.xpath(x_path, namespaces = namespace)
if(len(elements) ==1):
value = elements[0].text
if(value == None):
value ="Unknown"
return value
else:
return "Unknown"
My code works for the old version but I want make it work for all future versions also. How can I do that?