Monday, January 9, 2012

Adding namespace to XML Structure

I came across a situation while doing CSV to XML transformation, the MFL action in OSB transforms CSV to XML or vice -versa but the resulted XML structure does n't have a namespace added to it.

Namespace is required while doing service orchestration (by using Service Callout or Route action ).
There are two ways u can add namespace to xml structure .

1. Make a XSD of the xml structure and use Xquery transformation selecting newly created xsd of non-namespace as Source and in Target select as xsd/wsdl element which u want as result.

2. We can use xquery to add a default namespace to xml structure.(which is obtained using mfl action and storing the result in variable named InputRequestXmlBody

xquery version "1.0" encoding "Cp1252";
(:: pragma parameter="$noNamespaceXML" type="xs:anyType" ::)
(:: pragma parameter="$namespaceURI" type="xs:string" ::)
(:: pragma type="xs:anyType" ::)
declare namespace xf = "";

declare function xf:addNamespaceToXML($noNamespaceXML as element(*),$namespaceURI as xs:string) as element(*)
element {fn:expanded-QName($namespaceURI,fn:local-name($noNamespaceXML))}
for $node in $noNamespaceXML/node()
if (exists($node/node())) then xf:addNamespaceToXML($node,$namespaceURI)
else if ($node instance of element()) then element {fn:expanded-QName($namespaceURI,fn:local-name($node))}{$node/@*}
else $node }

declare variable $noNamespaceXML as element(*) external;
declare variable $namespaceURI as xs:string external ;
xf:addNamespaceToXML($noNamespaceXML, $namespaceURI)

We can use replace action then to add namespace to xml structure .

So XML Structure like this :


would end up like this :
<req:Transaction xmlns:req="http://in.abhinav/schema/request">

Happy Flows !!


  1. which one would have better performance, the 1st or 2nd option ?

  2. The second option would be better as your whole xml (not qualified) is being replaced with qualified namespace at the beginning rather than changing at individual steps !!

  3. Hi thanks, for sharing this nice how-to tip. Could you tell us how do you set the namespace prefix? I see that in your example it was set to "req", but it not clear for me how you do that in the code.
    Thanks, Rob

    1. Rob,

      req: namespace declaration was done inside xquery selection box--- under the bind variables section .. see liink below !

      $namespaceURI is the variable declared inside the xquery ..

      Regards, Abhinav