How to parse XML request data using XPath notations in JavaScript

Hi there!

I'm looking to leverage only the JavaScript Policy available in Egde for extracting data through XPath notations from my XML request but somehow, I'm not able to achieve the desired result with the below sample XML request and XPath notation using JavaScript Policy.

What am I doing wrong? I appreciate your timely responses. Many thanks.

Sample XML Request:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <QueryInfoRequest xmlns="http://abc/defgh/xml" xmlns:ns2="http://www.abcde.com/schemas/XML/Error/ErrorSchema.xsd">
         <header>
            <senderID>1111</senderID>
            <channelID>GGDDHH</channelID>
            <tranDateTime>2016-12-11T19:58:03.728Z</tranDateTime>
            <userID>hydml</userID>
         </header>
         <body>
            <request>
               <partnerID>11</partnerID>
               <partnerTransactionID>27c1ed58-9ad5-4065-9ecf-77b6c4ec516c</partnerTransactionID>
               <queryInfo>
                  <action>QueryProducts</action>
                  <ICCID>0000000000001452000000</ICCID>
                  <marketZip>5601789541</marketZip>
               </queryInfo>
            </request>
            <requestCount>1</requestCount>
         </body>
      </QueryInfoRequest>
   </soap:Body>
</soap:Envelope>

And my XPath expression to extract value of partnerID element from above the XML:

 //*[local-name='partnerID']/text()
0 8 3,504
8 REPLIES 8

Hi @Mathanprasath k - Why not use the Extract Variable policy which is out of the box in Edge ? Its much easier and would be faster. More info here.

Thanks much @Sai Saran Vaidyanathan for your response.I was more inclined towards using ExtractVariables Policy, as it's optimized for faster execution but I wanted to try to achieve the same in JavaScript Policy as well as out of my curiousity

Thanks for the question. You showed the xpath expression. and you say you arre using it from within JavaScript. Can you show all the JavaScript code you are using?

Also, I agree with the other commenter. Rather than using xpath from within JavaScript, why not use ExtractVariables? It is designed for this purpose. Have you considered it?

Finally, it's possible you are using E4X. If that's the case, check this answer.

Thanks much @Dino for your response. Yes, I'm more inclined towards using ExtractVariables Policy but wanted to achieve the same in JavaScript as well out of my interest. The request XML based SOAP message is being sent by client so I assume, it's available as variable form in context.targetRequest.content. and where I want your help is on how to do XPath(with the above specified XPath Notation) using JavaScript. I'm using the statement

var extract=context.targetRequest.body.asXML.partnerID;

to extract partnerID from above XML request but not working.

Hi, the syntax you are using is E4X, not xpath.

Check the link I provided in my previous comment. That will give you an example.

But one hint: there is no asterisk * path element you can use as a shortcut. The reference must walk the hierarchy. So you need something like:

var soap = new Namespace('http://schemas.xmlsoap.org/soap/envelope/');
var ns1 = new Namespace('http://abc/defgh/xml');
var p = context.targetRequest.content.asXML.soap::Envelope.soap::Body.ns1::QueryInfoResponse.ns1::body.ns1::request.ns1::partnerID;

Thanks for the swift response @Dino . I appreciate it really.Will help me a lot.

@Dino . I use the below code in JavaScript Policy but nothing gets printed in the trace. Also I see semicolon is missing error in JavaScript for the variable declaration p.

var soap = new Namespace('http://schemas.xmlsoap.org/soap/envelope/');
var ns1= new Namespace('http://www.tibco.com/schemas/XML/Error/ErrorSchema.xsd');
var p=context.targetRequest.body.asXML.soap::Envelope.soap::Body.ns1::QueryInfoRequest.ns1::body.ns1::request.ns1::partnerID;
print(p);

Any idea ?

First, I think your ns1 definition is wrong. The string you showed (ending in ErrorSchema.xsd) is not the namespace that was used in the XML document you shared above. I suspect you have missed something.

Also, try replacing "body" with "content" in the above.

If that does not help, ... you will have to diagnose it. Examine the value of each of the steps in the hierarchy.

you know...., diagnose it.