API Connect

 View Only
  • 1.  To search/find Data using GatewayScript function

    Posted Tue March 15, 2022 12:42 PM
    Hello Everyone,

    Can anyone suggest us which function we can use in gatewayscript to search/find particular data in message body

    ------------------------------
    Akshay Sawant
    ------------------------------


  • 2.  RE: To search/find Data using GatewayScript function

    IBM Champion
    Posted Wed March 16, 2022 07:28 AM
    Hi @Akshay Sawant,
    That would really depend on the data/object type you are doing the search on. Please provide more details or an example search you want to perform. ​


  • 3.  RE: To search/find Data using GatewayScript function

    Posted Thu March 17, 2022 05:19 AM
    Hello Romil,

    How to read XML response in array format ( v10). We want read data and compare timestamp. To get latest timestamp's data(ie xyz123).

    For eg.
    <Response>
    <url>xyz;http://www.xyz.com;xyz123;Wed Mar 22 16:22:22 IST 2022 </url>
    <url>abc;http://www.abc.com;abc123;Mon Mar 21 16:22:22 IST 2022 </url>
    <url>pqr;http://www/pqr.com;pqr123;Mon Mar 20 16:22:22 IST 2022 </url>
    <Response>

    ------------------------------
    Jyoti Yadav
    ------------------------------



  • 4.  RE: To search/find Data using GatewayScript function

    Posted Thu March 17, 2022 10:53 AM
    Hello Romil,

    We want to read XML response in array format. To search and compare data (timestamp) from XML response.
    For eg.
    <Reponse>
    <url><xyz_123;https://www.xyz.com;123;xyz_123;xxx;mar 23 2022 12:22:22></url>
    <url><abc_345https://www.abc.com;345;abc_345;aaa;mar 24 2022 12:22:22></url>
    <url><pqr_123;https://www.pqr.com;123;pqr_123;ppp;mar 25 2022 12:22:22></url>
    </Response>

    We want to read data  in <url> tag and compare timestamp and get latest timestamp values(ie. xxx).

    ------------------------------
    Jyoti Yadav
    ------------------------------



  • 5.  RE: To search/find Data using GatewayScript function

    Posted Thu March 17, 2022 11:39 AM

    Hi Jyoti,

    For XML that is relatively simple, and I'm assuming your XML isn't just a string but a parsed DOM object,  you can use the

    let urlNodelist = xmlResponseDom.getElementsByTagName('url').

    This is effectively the same as an XPath of //url, and the response is a nodelist.  You can iterate over each nodelist item and get the actual value using 

    let url = urlNodelist.item(i).textContent;

    The XML above isn't well formed due to the < and > characters in the url value, so I'm assuming your url element must have some type of timestamp child element?? if so you would need for each urlNodelist item to then do a getElementsByTagName('timestamp') function on it.  DataPower GatewayScript supports the read/get functions of the DOM API, see https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html for more details.

    Finally, if your XML is more complex, especially with namespaces, or just very large where a //url Xpath like function would not be very preformant, you could use the transform module which you'll note is asynchronous, so you'll need to continue your processing of the result in the asynchronous function as shown below.

      var transform = require('transform');
    
      ... 
    
      var options = 
      {
        // expression: '/soap:Envelope/soap:Body/api:PingRequest/api:RequestHeader',
        expression: '/tns:claimResponseMessage/tns:claimResponseBody/tns:response/tns:claimPkgData/tns:statusReasonData',
        xmldom: xmlDOM,  // your XML here
        namespace: { 'tns' : 'http://www.yourco.com/ns/',
                     'soap' : 'http://schemas.xmlsoap.org/soap/envelope/',
                     'api' : 'http://www.yourco.com/xmlschema/api'
                   }
      };
    
      transform.xpath(options, function(xpathError, xmlNodeList) {
        // the write to output at the completion of the switch should complete first, 
        // this async callback's writes should write to output last
        if (xpathError) {
          // error occurred when compiling / executing the XPath operation
          var errMessage = 'Error on XPath operation: ' + xpathError ;
          console.error(errMessage);
          // any other error processing your may have, or just treat it as an empty nodelist??
        } else {
          // process the returned nodelist
        }
      });

    Regards,
    Steve



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------



  • 6.  RE: To search/find Data using GatewayScript function

    Posted Fri March 18, 2022 02:13 AM
    Hello Steve,
     We tried to display within Response tag which is include 3 URL tag in below sample xml response of our API but we are getting xmlResponseDom is not defined error using below condition
    let urlNodelist = xmlResponseDom.getElementsByTagName('url')

    we want to implement below 3 condition in our gatewayscript

    1 Below is response of our API & we want to display only URL data which is include in Response tag.

    2 below URL we are getting object id & it's compare to latest timestamp so it's display first

    3 after object id we are getting Endorsement, ARreceipt & policy so we want only Endorsement & policy object ID, we not need of ARreceipt object id

    We want to implement above 3 condition can you guide us how we can implement this 3 condition in gatewayscript  for below xml data?

    <?xml version="1.0" encoding="UTF-8"?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
        <soapenv:Header xmlns:urn="urn:in.abc.xml:corporate.edm.entity.atomic.DocumentSearch"/>
        <soapenv:Body xmlns:urn="urn:in.abc.xml:corporate.edm.entity.atomic.DocumentSearch">
            <urn:DocSearchResponse>
                <Response>
                    <URL>Endo_000000000034070;http://rs29u2c8vap101.ccc.in/cbcWebservice/documentViewer.jsp?objectId=3143077;Endorsement;Wed Mar 09 16:27:02 IST 2022</URL>
                    <URL>Receipt_8030392;http://rs29u2c8vap101.bbb.in/cdeWebservice/documentViewer.jsp?objectId=3141154;ARReceipt;Mon Mar 07 22:26:48 IST 2022</URL>
                    <URL>Receipt_8001499;http://rs29u2c8vap101.aaa.in/efgWebservice/documentViewer.jsp?objectId=2585731;Policy;Wed Jul 14 14:39:17 IST 2021</URL>
                </Response>
            </urn:DocSearchResponse>
        </soapenv:Body>
    </soapenv:Envelope>


    ------------------------------
    Akshay Sawant
    ------------------------------



  • 7.  RE: To search/find Data using GatewayScript function

    Posted Mon March 21, 2022 12:00 PM
    Edited by Steve Linn Mon March 21, 2022 12:03 PM

    Hi Akshay,

    In the original XML in the above thread, the element was lower case

    <url>xyz;http://www.xyz.com;xyz123;Wed Mar 22 16:22:22 IST 2022 </url>
     
    but in your current XML, the element is upper case

    <URL>Endo_000000000034070;http://rs29u2c8vap101.ccc.in/cbcWebservice/documentViewer.jsp?objectId=3143077;Endorsement;Wed Mar 09 16:27:02 IST 2022</URL>

    the getElementsByTagName is case sensitive (as is XML).  Please try 

    let urlNodelist = xmlResponseDom.getElementsByTagName('URL')

    now xmlResponseDom needs to be an XML document.  How are you getting the XML in your code?

    If it is a string or buffer it will need to be parsed.  Within your code you could do that parsing yourself.

    let xmlResponseDom = XML.parse(xmlstring);

    of course you would use whatever variable you have the XML string.

    I'm trying to guess however what you have based upon your comments (code would be better of course) but since you say

    > Below is response of our API & we want to display only URL data which is include in Response tag.

    I'm thinking perhaps you did an invoke version 2.0.0 policy.  The invoke policy does not parse the response it receives, and places the response payload as a Buffer into message.body by default unless you override that to place it in some other response variable, for example, mymessage.body.  You can add a parse policy after the invoke policy.  If you specify "Use content type" it will use the content-type response header to determine the type of parsing it will use, and assuming the backend returned "application/xml" or some other XML type, then that will create your DOM which would be stored in the context, for example, in message.body.  You can also configure the parse policy to detect the context type based upon the payload, where a < to start out the body would most probably drive the XML parse code.

    If your code is then doing something like 

    let xmlResponseDom = context.get('message.body');

    the message.body should be XML.  If you are not doing a parse policy, then you'll get a Buffer which doesn't know anything about the DOM API function, but as noted above you can convert that Buffer to a string and then parse it.

    Hope this helps,
    Steve



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------