Layer7 API Management

Expand all | Collapse all

How to read XML Attribute value from XML message in API Gateway?

Jump to Best Answer
  • 1.  How to read XML Attribute value from XML message in API Gateway?

    Posted 10-12-2016 06:15 AM

    How to read XML Attribute value from XML message in API Gateway? Want to read value of uniqueRequestId in below XML:

     

             <dit:DitRequest uniqueRequestId="XXXXXXXXXX-XXXX">

                                       <req:PAN>*********</req:PAN>

               </dit:DitRequest>

     

    Thanks,

    Siddharth



  • 2.  Re: How to read XML Attribute value from XML message in API Gateway?
    Best Answer

    Posted 10-12-2016 10:07 AM

    Hello Siddharth,

     

    You can accomplish this via 'XPATH'. We have a few assertions in our Assertion Palette that should be able to meet your needs. ( Evaluate Request Xpath, Evaluate Response Xpath)

     

    If you are wanting to extract data from the Request context, you will need to use the first one mentioned above.

    If you are wanting to extract data from the Response context or from a variable in policy, you will need to use the 'Evaluate Response Xpath'.

     

    Here's a simple policy that does this.

     

     

    I store my xml in a variable in this case ( i could target the Request or the Response directly as well ).

     

    i then use the 'Evaluate Response Xpath' assertion to target my Message Variable called 'meMessage'.

     

    This is what the Assertion properties look like... I made sure i set my 'Source' and Prefix.

     

    for building the path, you can use the 'Sample Messages' at the bottom of the assertion to help, but it will only get you 90% of the way there in your case... you have to know how to get the rest of the 10% if this isn't an easy xpath.. in your case, to target an attribute, you'll need the '@'. 

     

     

    the assertion sets a few variables after it runs, you can view what variables an assertion sets by hovering you mouse over it.. like below

     

     

    we are interested in the <prefix>.result variable, so in our Return Template, we echo this back and this guy returns the attribute that matched our xpath.

     

     

    hope this helped.



  • 3.  Re: How to read XML Attribute value from XML message in API Gateway?

    Posted 10-12-2016 10:13 AM

    Hi SIDDHARTHJAISWAL,

        You need to use Evaluate Request XPath Assertion to extract the attribute vale using xpath. I have provided the xpath query for your XML below -

     

    //*[local-name()='DitRequest']/@uniqueRequestId

     

     

    I have ignored namespaces in the above xpath expression. You can find more details on the assertion here

     

     

    Thanks,

    Abhishek



  • 4.  Re: How to read XML Attribute value from XML message in API Gateway?

    Posted 10-12-2016 11:08 PM

    xpath assertion is one resolution, alternate resolution is Evaluate Regular Expression assertion.

     

     

     

     

     

    Request body and results,



  • 5.  Re: How to read XML Attribute value from XML message in API Gateway?

    Posted 10-17-2016 10:27 AM

    Hi All ,

    I have same question , but how can I extract the value between <req:PAN>  and  </req:PAN> ?

    Thanks .

    Jerry Lin



  • 6.  Re: How to read XML Attribute value from XML message in API Gateway?

    Posted 10-17-2016 06:43 PM

    Hi Jerry,

    For xpath, 

    /dit:DitRequest/req:PAN

     

    For regex,

    <req:PAN>([^<]*)</req:PAN>

     

    Regards,

    Mark