Saturday, 25 May 2013

Add/Rename/Remove Namespace



In this tutorial I have tried to capture some xquery operation namespace.We are having lot many issues with these namepace.Sometimes source xml does not have any namespace.So middleware need to add those namespace and send it to target system with expected format.

Here I am creating one OSB proxy service with will poll messages from jms queue using jms transport protocol.Messages are xml type with no namespace.


I am using xquery to addnamespace with that input xml.

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 = "http://tempuri.org/Resources/XQueries/addNamespace/";
declare function addNamespaceToXML($noNamespaceXML as element(*),$namespaceURI as xs:string)as element(*)
{
 element {fn:expanded-QName($namespaceURI,fn:local-name($noNamespaceXML))}
{
      $noNamespaceXML/@*,
      for $node in $noNamespaceXML/node()
      return
      if (exists($node/node())) then 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 ;
addNamespaceToXML($noNamespaceXML, $namespaceURI)

Proxy message flow I am using replace activity to replace the existing input body with this xquery and adding log after that to display the change body.


To Test this,I am putting input xml in jms queue.

<?xml version="1.0" encoding="UTF-8"?>
<empDetails>
<emp>
<empID>1111</empID>
<empName>Souvik</empName>
<empaddr>2020 GOVERNMENT AVE,NORTH DAKOTA</empaddr>
<sal>10000</sal>
</emp>
</empDetails>



Here is output with namespace alias xmlns:emp="http://www.example.org/EmpDetails".


After adding namespace to request message I am renaming the namespace from "http://www.example.org/EmpDetails" to "http://www.souviksoa.com/EmpDetails"
I am using rename activity here.I xpath using .//* to rename all child elements under body.


Here is the next output


Now I am removing namespace using xquery or xslt.


xslt transformation for remove namespace


xquery transformation for remove namespace

xquery version "1.0" encoding "Cp1252";
(:: pragma  parameter="$anyType1" type="xs:anyType" ::)
(:: pragma  type="xs:anyType" ::)

declare namespace xf = "http://tempuri.org/Cancel_Order/Remove_Namespace/";

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

declare variable $e as element() external;
xf:strip-namespace($e)


Final output after removing the namespace.



5 comments:

  1. Hi I have some query,can you please provide your mail id ,so i can able to contact with you.

    Thanks

    ReplyDelete
  2. for weblogic 12c use script below:
    ---------------------------------------------------------------
    xquery version "1.0" encoding "Cp1252";
    (:: pragma parameter="$anyType1" type="xs:anyType" ::)
    (:: pragma type="xs:anyType" ::)

    module namespace xf="http://tempuri.org/Cancel_Order/RemoveNamespace";

    declare function xf:strip-namespace($e as element())as element()
    {element { fn:QName("",fn:local-name($e)) }
    { for $child in $e/(@*,node())
    return
    if ($child instance of element())
    then xf:strip-namespace($child)
    else $child
    }
    };

    ReplyDelete
  3. This article was helpful.
    Thank you.

    ReplyDelete
  4. can u provide adding namespace for 12c OSB

    ReplyDelete
  5. And if want to remove the namespace from a single element, don't need browse all the XML because I know the position of the element with the unused namepsace.

    Regards

    ReplyDelete