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 🙂

8 thoughts on “Stripping XML Namespaces using XQuery

  1. I am not sure if you are still working on SOA but I will post my question anyway.
    I have two requirements :
    1. if I pass DVM file name as input my BPEL service should return the dvm structure to me.
    2. if I pass DVM file name and corresponding values as input, they should get added in corresponding dvm file.

    I am not sure if its even possible to achieve these requirements.
    I am working on SOA 12c.

    Please help!

    Like

    • Hi Debasmita,

      This is an interesting question. I do not think you can do it through SOA suite directly.

      Your requirement number 1 can be achieved by leveraging the Oracle MDS APIs. Check out the APIs here:

      https://docs.oracle.com/middleware/1213/adf/api-reference-mds/toc.htm

      Your second requirement, I presume is to update your DVM values instead of reading it. I am not sure how this would be possible. If you really want to do this, then I would suggest dig into the product Jar’s to see if there is a method signature that allows you to append values in a DVM.

      Thanks

      Like

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