0

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?

newbieB
  • 1
  • 5

1 Answers1

0

Have you tried passing '*' instead of 'http:/ss/dd*'?

Anyway, using wildcards for namespaces is not a good practice as explained here

So why not getting the namespace value (this value I mean "xmlns="http:/ss/dd/version1_0") and use it after in your code?

Here there is some information on getting the namespace value

Hope this helps

Carlos
  • 1,340
  • 1
  • 10
  • 26