cancel
Showing results for 
Search instead for 
Did you mean: 

XML namespace prefix removal from XML document

vpalan
New Contributor

I would like to remove xml namespace prefix in the XML document for data retrieval. I have tried with mapper with mapKeys(), I cannot use mapper as $Header.['ns2:DocumentHeader']['ns2:Identifier'] because namespace prefix may be different for each records "ns2" as per our vendor. Could you please suggest possible solution for this?
Here is my input.

<Header>
  <ns2:DocumentHeader>
     <ns2:Identifier>3af8318ca9e0</ns2:Identifier>
  </ns2:DocumentHeader> 
</Header>

 Expected Output:

<Header>
  <DocumentHeader>
     <Identifier>3af8318ca9e0</Identifier>
  </DocumentHeader> 
</Header>


 

1 ACCEPTED SOLUTION

GjorgeArgirov
New Contributor

Hi @vpalan ,
One way to handle this is by using a Script Snap with JavaScript code to remove all XML namespaces. This approach is dynamic and works regardless of how deeply nested the namespaces are. You don’t need to worry about the XML structure or depth — the script will recursively clean all prefixed elements and attributes. Please check the attached pipeline for reference.

Alternatively, if you know exactly where and how deep the namespaces appear, you can use a SnapLogic expression like the one below:

$.mapKeys((v, k) => k.contains(':') ? k.split(':')[1] : k)
 .mapValues(v => v.mapKeys((v2, k2) => k2.contains(':') ? k2.split(':')[1] : k2))

This works for shallow structures but won’t handle deeply nested namespaces. For more complex XML, I recommend the Script Snap approach.

View solution in original post

2 REPLIES 2

GjorgeArgirov
New Contributor

Hi @vpalan ,
One way to handle this is by using a Script Snap with JavaScript code to remove all XML namespaces. This approach is dynamic and works regardless of how deeply nested the namespaces are. You don’t need to worry about the XML structure or depth — the script will recursively clean all prefixed elements and attributes. Please check the attached pipeline for reference.

Alternatively, if you know exactly where and how deep the namespaces appear, you can use a SnapLogic expression like the one below:

$.mapKeys((v, k) => k.contains(':') ? k.split(':')[1] : k)
 .mapValues(v => v.mapKeys((v2, k2) => k2.contains(':') ? k2.split(':')[1] : k2))

This works for shallow structures but won’t handle deeply nested namespaces. For more complex XML, I recommend the Script Snap approach.

koryknick
Employee
Employee

@vpalan - another solution is to use an expression library that recursively traverses the object tree, updating the keys as it goes down.  See the attached example pipeline.

Hope this helps!