Layer7 API Management

 View Only
Expand all | Collapse all

Add/Remove XML element assertion

  • 1.  Add/Remove XML element assertion

    Posted Sep 16, 2015 05:43 AM

    Hi,

     

    Is it possible to get an example policy for the xml add/remove element assertion, because the documentation for it is very unclear for me. For example, I created a policy like this:

     

    addremovexml.PNG

    What I expect from this policy is that it adds the <d></d> element under <c></c>, but instead the gateway is giving an error stating  "Unable to insert element because no existing element was found". In the return template response I print out the responseXpath.element and it is as I expect it to be, namely <c></c>


    Thanks in advance



  • 2.  Re: Add/Remove XML element assertion

    Posted Oct 29, 2015 05:37 PM

    The issue here is that the "Existing Element" variable must contain a value of type Element, and the "New Element" variable must contain a value of either type Element or String.

     

    This snippet works if you:

    • Change the second Set Variable assertion's type from Message to String; or, change the "New Element" expression from "addXML" to "addXML.mainpart"
    • Change the "Existing Element" variable name from "responseXpath.element" (which is of type String) to "responseXpath.elements[0]" (which is of type Element).


  • 3.  Re: Add/Remove XML element assertion

    Posted Jul 08, 2016 02:07 PM

    I have updated the topic "Add or Remove XML Element(s) Assertion" in the Gateway wiki with the sample snippet from YGeerts, updated with the corrections from our subject matter expert Mike Lyons (lyomi03).



  • 4.  Re: Add/Remove XML element assertion

    Posted Sep 22, 2016 04:07 PM

    I am trying to add element exactly like as but even though I am getting error.

     

    Error message in logs-

    2016-09-22T15:05:24.944-0500 WARNING 199880 com.l7tech.server.message: Message was not processed: Error in Assertion Processing (601)

    2016-09-22T15:05:29.369-0500 INFO    179848 com.l7tech.server.admin.AsyncAdminMethodsImpl: Asynchronous job JobId<java.lang.Long>:bfef2656a53186ea8cf2387b48013b7b2d8381c5083458a7f01e5288ffbe8ac1 threw an exception: Argument 0 for @NotNull parameter of com/l7tech/objectmodel/Goid.toString must not be null

     

    Error message on Postman-

    <faultstring>Policy Falsified</faultstring>



  • 5.  Re: Add/Remove XML element assertion

    Broadcom Employee
    Posted Sep 22, 2016 06:33 PM

    Brijendra,

     

    Please provide the version of the gateway also this policy snippet as a file to speed up the test. I'm also going to remove the other community post so we will continue to work through this one.

     

    Sincerely,

     

    Stephen Hughes

    Director, CA Support



  • 6.  Re: Add/Remove XML element assertion

    Broadcom Employee
    Posted Sep 22, 2016 06:45 PM
      |   view attached

    Brijendra,

     

    The problem is in the add element assertion as it needs to be responseXpath.elements not element. I've attached a policy snippet from a version 9.1 gateway that does do what has been outlined.

     

    Sincerely,

     

    Stephen Hughes

    Director, CA Support

    Attachment(s)

    zip
    add_element.xml.zip   907 B 1 version


  • 7.  Re: Add/Remove XML element assertion

    Posted Sep 30, 2016 01:42 PM

    There was a mistake in the policy snippet illustration in the "Add or Remove XML Element(s) Assertion" topic in the API Gateway documentation. It has now been corrected to show "responseXpath.elements[0]" instead of "responseXpath.element[0]":

     

    Add or Remove XML Element(s) Assertion - CA API Gateway - 9.1 - CA Technologies Documentation 



  • 8.  RE: Re: Add/Remove XML element assertion

    Posted May 20, 2020 11:11 AM
    Hey all,

    Thanks for the example. Got it working here too. Just wondering though. When i try to add namespaces to the xml element the add remove element assertion complains about not being aable to add the element to the xml because it isn't a valid element to be added.

    Wanted to add a new post but can't see how tot start a new thread (maybe because i'm a newbie). Does anyone have an idea about the above question?



  • 9.  RE: Re: Add/Remove XML element assertion

    Broadcom Employee
    Posted May 20, 2020 01:43 PM
    Can you paste the policy you have that is not working here? I don't have any context for your question.

    ------------------------------
    Jay MacDonald - Adoption Architect - Broadcom API Management (Layer 7)
    ------------------------------



  • 10.  RE: Re: Add/Remove XML element assertion

    Posted Jun 05, 2020 06:31 AM
      |   view attached
    Hey Jay,

    Thanks for the reply. I've created two examples. One that is working and one that isn't working. De message I get is that L7 is complaining that the new element that it needs to add is not a well formed xml fragment. I think it is cause its possible to add namespaces to elements (when they're declared ofcourse). Am I doing something wrong?

    Cheers

    Attachment(s)



  • 11.  RE: Re: Add/Remove XML element assertion
    Best Answer

    Broadcom Employee
    Posted Jun 05, 2020 03:26 PM
    You are on the right track. In order to insert an XML fragment, the fragment must be well formed on its own. If no namespace prefix is used, as it is in your working policy, then the XML is well formed and it can be inserted. If a namespace prefix is used but the namespace is not declared, as it is in your not-working policy, then the XML is not well formed. So when you define ${addxml} it MUST include a namespace declaration. Paste the following in to replace your Set Context Variable for ${addxml} to see what I mean. The resulting XML after the insert will have the namespace re-declared in the element, but that's no harm, no foul.

    <?xml version="1.0" encoding="UTF-8"?>
    <wsp:Policy xmlns:L7p="http://www.layer7tech.com/ws/policy" xmlns:wsp="http://schemas.xmlsoap.org/ws/2002/12/policy">
      <wsp:All wsp:Usage="Required">
        <L7p:SetVariable>
          <L7p:AssertionComment assertionComment="included">
            <L7p:Properties mapValue="included">
              <L7p:entry>
                <L7p:key stringValue="RIGHT.COMMENT"/>
                <L7p:value stringValue="Element definieren en vullen met de juiste waarde"/>
              </L7p:entry>
            </L7p:Properties>
          </L7p:AssertionComment>
          <L7p:Base64Expression stringValue="PHRuczpPcGVuYmFyZXJ1aW10ZW5hYW0geG1sbnM6dG5zPSJodHRwOi8vd3d3LnZpY3JlYS5ubC9OZXVyb25HZWdldmVuc21hZ2F6aWpuL1NvYXAvUEM2X0JBR05ML0FkcmVzQnlQYzZfYmFnbmwvUGM2X2JhZ25sX1NlcnZpY2UvR2V0UGM2X2JhZ25sX0RhdGFUeXBlcyI+b3V0cHV0PC90bnM6T3BlbmJhcmVydWltdGVuYWFtPg=="/>
          <L7p:VariableToSet stringValue="addxml"/>
        </L7p:SetVariable>
      </wsp:All>
    </wsp:Policy>

    ------------------------------
    Jay MacDonald - Adoption Architect - Broadcom API Management (Layer 7)
    ------------------------------



  • 12.  RE: Re: Add/Remove XML element assertion

    Posted Jun 08, 2020 04:37 PM
    Good stuff Jay! Thanks for the answer and help :).


  • 13.  RE: Re: Add/Remove XML element assertion

    Posted Jun 19, 2020 07:31 AM
    ​I am trying to use Remove but it still doesn't work with the example Jay shared above,can you please share example  on how to use remove


  • 14.  RE: Re: Add/Remove XML element assertion

    Broadcom Employee
    Posted Jun 19, 2020 07:42 AM
      |   view attached
    Akshat,

    Attached is a very basic sample to remove an XML element.

    You can POST this body to the endpoint once you import the attached policy and it will remove the PRICE element.

    <?xml version="1.0" encoding="UTF-8"?>
    <bookstore>
    <book>
    <title lang="en">Layer7 API Management</title>
    <author>Broadcom</author>
    <year>2018</year>
    <price>30.00</price>
    </book>
    <book>
    <title lang="en">Docker</title>
    <author>John Doe</author>
    <year>2020</year>
    <price>29.99</price>
    </book>
    </bookstore>

    Attachment(s)

    xml
    remove_xml.xml   1 KB 1 version


  • 15.  RE: Re: Add/Remove XML element assertion

    Posted Jun 19, 2020 11:08 AM
    Thanks it was helpful​