Passing CDATA in XQuery using OSB

In the following post I have tried to explain how we can pass CDATA tag in xquery transformation in OSB.
There is often a need to pass special character in XML request.


Consider an example, an customer order processing proxy.

Lets create an simple xsd that generate below request and response xml:3
Note:- Haven’t  concentrated more on naming convention
Request element:-
<tns:Rq xmlns:tns="http://www.example.org/OrderRq" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/OrderRq CustOrder.xsd ">

  <tns:orderRq>
    <tns:CustID>tns:CustID</tns:CustID>
    <tns:CustName>tns:CustName</tns:CustName>
    <tns:Brand>tns:Brand</tns:Brand>
  </tns:orderRq>

</tns:Rq>

Response element(Which is actual third party request):

<tns:TP_Rq xmlns:tns="http://www.example.org/OrderRq" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/OrderRq CustOrder.xsd ">

  <tns:CustomerName>tns:CustomerName</tns:CustomerName>
  <tns:Brand>tns:Brand</tns:Brand>

</tns:TP_Rq>

Created simple xquery:


(:: pragma bea:global-element-parameter parameter="$rq1" element="ns0:Rq" location="../Schemas/CustOrder.xsd" ::)
(:: pragma bea:global-element-return element="ns0:TP_Rq" location="../Schemas/CustOrder.xsd" ::)

declare namespace ns0 = "http://www.example.org/OrderRq";
declare namespace xf = "http://tempuri.org/Customer_Details/Resources/Xqueries/CustomerOrder/";

declare function xf:CustomerOrder($rq1 as element(ns0:Rq))
    as element(ns0:TP_Rq) {
        <ns0:TP_Rq>
            <ns0:CustomerName>{ data($rq1/ns0:orderRq[1]/ns0:CustName) }</ns0:CustomerName>
            <ns0:Brand>{
               fn-bea:serialize(  fn:concat('<![CDATA[',data($rq1/ns0:orderRq[1]/ns0:Brand),']]>'))
             }</ns0:Brand>
        </ns0:TP_Rq>
};

declare variable $rq1 as element(ns0:Rq) external;
xf:CustomerOrder($rq1)

Create a simple proxy service that uses above xquery. In my case I create WSDL and tested through web service client.


Testing the Proxy Service:-
1. Request
    SetBrand("Bank & INDIA")
2. Transformed Response
    <![CDATA[Bank & INDIA]]>
*********************************************************************

You can use fn-bea:serialize() if you want string as an output instead of xml.

Defination as per Oracle Doc: 
You can use the fn-bea:serialize() function if you need to represent an XML document as a string instead of as an XML element. For example, you may want to exchange an XML document through an EJB interface and the EJB method takes String as argument. The function has the following signature:
fn-bea:serialize($input as item()) as xs:string

Do post your review or issues, will try my best to solve.




Popular posts from this blog

JAVA embedding in Oracle SOA 12c

Passing/Receiving HTTP header in SOA BPEL

Integrating Weblogic with IBM MQ over JMS