Stripping XML Namespaces using XQuery

A lot of the times I had found people complaining about unneccessary namespaces that flood their xml. Offcourse namespaces are very important but during certain times when we have to process complex and large xmls using XPath it becomes necessary to strip the namespaces. Here is a demonstration of a small XQuery code that is used to strip any samespaces from an XML input.

(:

=====================================
Description: xquery to remove namespaces from an xml
@author Arun Pareek
=====================================
$Resource/XQUERY/stripNamespace.xq  $

🙂

declare variable $inputRequest as element() external;
declare function strip-namespace($inputRequest  as element()) as element()
{
element {xs:QName(local-name($inputRequest ))}
{
for $child in $inputRequest /(@*,node())
return
if ($child instance of element())
then strip-namespace($child)
else $child
}
};

document
{ strip-namespace($inputRequest )}

Before explaining the code let me give a small example of how this piece of code works.

Input XML with Namespaces

<TestXML xmlns:sp-en=”sch.soap.org”>
<ZBAPI_COIB.ZBUR xmlns:sp-en=”sch.soap.org”>
<I_OUTPUT xmlns:sp-en=”sch.soap.org”/>
<RETURN xmlns:sp-en=”sch.soap.org”>
<item xmlns:sp-en=”sch.soap.org”>
<TYPE xmlns:sp-en=”sch.soap.org”>S</TYPE>
<CODE xmlns:sp-en=”sch.soap.org”/>
<MESSAGE xmlns:sp-en=”sch.soap.org”/>
<LOG_NO xmlns:sp-en=”sch.soap.org”/>
<MESSAGE_V1 xmlns:sp-en=”sch.soap.org”/>
</item>
<item xmlns:sp-en=”sch.soap.org”>
<TYPE xmlns:sp-en=”sch.soap.org”>S</TYPE>
<CODE xmlns:sp-en=”sch.soap.org”/>
<MESSAGE xmlns:sp-en=”sch.soap.org”/>
<LOG_NO xmlns:sp-en=”sch.soap.org”/>
<LOG_MSG_NO xmlns:sp-en=”sch.soap.org”>000000</LOG_MSG_NO>
<MESSAGE_V1 xmlns:sp-en=”sch.soap.org”/>
</item>
</RETURN>
</ZBAPI_COIB.ZBUR>
</TestXML>

And the Output of the Xquery is

<TestXML>
<ZBAPI_COIB.ZBUR>
<I_OUTPUT/>
<RETURN>
<item>
<TYPE>S</TYPE>
<CODE/>
<MESSAGE/>
<LOG_NO/>
<MESSAGE_V1/>
</item>
<item>
<TYPE>S</TYPE>
<CODE/>
<MESSAGE/>
<LOG_NO/>
<LOG_MSG_NO>000000</LOG_MSG_NO>
<MESSAGE_V1/>
</item>
</RETURN>
</ZBAPI_COIB.ZBUR>
</TestXML>

As you can see the code is very much self-explanatory. The Xquery snippet expects an xml input to it. The Xpath entity crawls through each of the elements in the input and returns just the child elemnt and discards the namespace. A very much similar to recursive fucntions in C/C++.

Enjoi 🙂

6 thoughts on “Stripping XML Namespaces using XQuery

If you have any comments, suggestions or feedback about the post, please feel free to type it here and I will do my best to address them asap

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s