API Connect

 View Only
Expand all | Collapse all

How to check Attribute value in Switch

  • 1.  How to check Attribute value in Switch

    Posted Sun November 27, 2022 05:56 AM
    Hi All,

    How to write the condition for xml Attribute value Checking in Switch in APIConnect.

    example XML:
    <Reply>
    <Name>B2BEligibility</Name>
     <parameter text="OTPRequired">false</parameter>
    <descriptioninfo>Passed</descriptioninfo>
    </Reply>

    I want to check attribute of text value is OTPRequired or not in switch condition of APIConnect.


    ------------------------------
    kandula nagababu
    ------------------------------


  • 2.  RE: How to check Attribute value in Switch

    Posted Mon November 28, 2022 10:01 AM

    Hi Kandula,

    See https://www.ibm.com/docs/en/api-connect/10.0.1.x?topic=switch-writing-condition-scripts-datapower-api-gateway.  You'll need to use the $(xpath) function, something like

    $xpath(message.body, "/Reply/parameter/text = 'OTPRequired'")

    Best Regards,

    Steve Linn



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



  • 3.  RE: How to check Attribute value in Switch

    Posted Mon November 28, 2022 02:14 PM
    I don't think $xpath is allowed in a switch , you could map out the value and then use it later in a switch, seen an example API below

    swagger: '2.0'
    info:
      title: development
      x-ibm-name: development
      version: 1.0.0
    x-ibm-configuration:
      cors:
        enabled: true
      gateway: datapower-api-gateway
      type: rest
      phase: realized
      enforced: true
      testable: true
      assembly:
        execute:
          - map:
              version: 2.0.0
              title: map
              inputs:
                input:
                  schema:
                    description: ''
                    type: object
                    properties:
                      Reply:
                        type: object
                        properties:
                          Name:
                            type: string
                            name: Name
                          parameter:
                            type: string
                            properties:
                              text:
                                type: string
                                xml:
                                  attribute: true
                                name: '@text'
                            xml:
                              namespace: ''
                            name: parameter
                          descriptioninfo:
                            type: string
                            name: descriptioninfo
                        xml:
                          namespace: ''
                        name: Reply
                    xml:
                      namespace: ''
                    example: "<Reply>\n<Name>B2BEligibility</Name>\n\_<parameter text=\"OTPRequired\">false</parameter>\n<descriptioninfo>Passed</descriptioninfo>\n</Reply>"
                  variable: message.body
                  content: application/xml
              outputs:
                output:
                  schema:
                    type: object
                  variable: switchvariable
              actions:
                - set: output
                  from: input.Reply.parameter
          - switch:
              version: 2.0.0
              title: switch
              case:
                - condition: switchvariable = 'false'
                  execute:
                    - gatewayscript:
                        version: 2.0.0
                        title: gatewayscript
                        source: context.set("message.body","false")
                - condition: switchvariable = 'true'
                  execute:
                    - gatewayscript:
                        version: 2.0.0
                        title: gatewayscript
                        source: context.set("message.body","true")
                - otherwise:
                    - gatewayscript:
                        version: 2.0.0
                        title: gatewayscript
                        source: context.set("message.body","otherwise")
        catch: []
        finally: []
      properties:
        target-url:
          value: http://example.com/operation-name
          description: The URL of the target service
          encoded: false
      activity-log:
        enabled: true
        success-content: activity
        error-content: payload
    basePath: /development
    paths:
      /:
        get:
          responses:
            '200':
              description: success
              schema:
                type: string
          consumes: []
          produces: []
        put:
          responses:
            '200':
              description: success
              schema:
                type: string
          consumes: []
          produces: []
        post:
          responses:
            '200':
              description: success
              schema:
                type: string
          consumes: []
          produces: []
        delete:
          responses:
            '200':
              description: success
              schema:
                type: string
          consumes: []
          produces: []
        head:
          responses:
            '200':
              description: success
              schema:
                type: string
          consumes: []
          produces: []
        patch:
          responses:
            '200':
              description: success
              schema:
                type: string
          consumes: []
          produces: []
    securityDefinitions:
      clientID:
        type: apiKey
        in: header
        name: X-IBM-Client-Id
    security:
      - clientID: []
    schemes:
      - https
    consumes:
      - application/xml
    produces:
      - application/xml
    ​


    ------------------------------
    Tom van Oppens
    ------------------------------



  • 4.  RE: How to check Attribute value in Switch

    Posted Mon November 28, 2022 02:53 PM

    Kandula and Tom,

    I just tried this and get the same validation error from the manager.  When I overwrite the switch condition in the DataPower object after publishing the API with a simple JSONata with the $xpath JSONata function, the Gateway has no issue and successfully matches with an $xpath.  We're discussing this internally, but a PMR should opened to have the API validation  accept this function.

    Best Regards,

    Steve



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



  • 5.  RE: How to check Attribute value in Switch

    Posted Tue November 29, 2022 08:47 AM
    Hi Steve Linn,

    For time being I resolved this issue another way using XSLT I stored the value and checking that context variable in Switch() policy.

    Here I am facing issue with name prefix for attribute like below:
    <ns:Reply
    xmlns:ns="http://www.bob.com"
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
    <ns:Name>B2BEligibility</ns:Name>
    <ns:parameter ns:text="OTPRequired">false</ns:parameter>
    <ns:parameter ns:text="OTPinfo">false</ns:parameter>
    <ns:descriptioninfo>Passed</ns:descriptioninfo>
    </ns:Reply>

    if possible share me thoughts on this.

    ------------------------------
    kandula nagababu
    ------------------------------



  • 6.  RE: How to check Attribute value in Switch

    Posted Tue November 29, 2022 08:59 AM

    Hi Kandula,

    In xslt and in particular, an XPath, what really matters in the namespace uri.  Think of the prefix as shorthand for the uri.  Thus in your xslt you could have in your xsl:stylesheet

    xmlns:bogusname="http://www.bob.com"

    and your XPath in your xslt would be specified as follows as the prefix bogusname has the proper namespace uri

    /bogusname:Reply/bogusname:Name

    you can also use the local-name function within a predicate to ignore the prefix all together, but then that would open up the possibility of having inbound XML with that localname but a different namespace uri which really shouldn't match

    /*[local-name() = 'Reply']/*[local-name() = 'Name']

    Best Regards,

    Steve Linn



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



  • 7.  RE: How to check Attribute value in Switch

    Posted Wed November 30, 2022 05:11 AM
    Hi Steve,

    We tried multiple scenarios like wise but no one working in API Connect, here I am trying to get the value of parameter element by validating attribute of text value as shown below.

    /ns:Reply/ns:parameter[@ns:text="OTPRequired"]/text() = "false"
    ($xpath(message.body, '/ns:Reply/ns:parameter[@ns:text="OTPRequired"]/text()') = "false")

    ------------------------------
    kandula nagababu
    ------------------------------



  • 8.  RE: How to check Attribute value in Switch

    Posted Wed November 30, 2022 12:20 PM
    Hi Kandula,
    The text attribute of the ns:parameter element does not have a namespace. Simply use 
    @text​
     to denote this attribute in the XPath​.

    Best Regards,
    Steve Linn

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



  • 9.  RE: How to check Attribute value in Switch

    Posted Thu December 01, 2022 04:10 AM
    Hi Steve,

    <ns:Reply
    xmlns:ns="http://www.bob.com"
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
    <ns:Name>B2BEligibility</ns:Name>
    <ns:parameter ns:text="OTPinfo">true</ns:parameter>

    <ns:parameter ns:text="OTPRequired">false</ns:parameter>
    <ns:descriptioninfo>Passed</ns:descriptioninfo>
    </ns:Reply>

    Here we are trying to check the value of parameter element along with the attribute text value consideration.
    we are able to check the attribute name and it's value using Switch policy, but we are unable to check the parameter element value is "false" or not along with attribute value "OTPRequired" in consideration.

    Please share the exact Xpath for the above, we tried a lot but no use, in switch policy "@", "and" those not taking.



    ------------------------------
    kandula nagababu
    ------------------------------



  • 10.  RE: How to check Attribute value in Switch

    Posted Thu December 01, 2022 03:11 PM

    Hi Kandula,

    My apologies, my answer yesterday was from a view of this thread on my phone and I missed that the recent XML now has a namespace for the text attribute.  As $xpath is not currently supported in the switch JSONata you're trying this XPath in a xslt, correct?  The XPath you are using in xslt is part of an xsl element of some sort, where this XPath is within an XML attribute, ie something like

    <xsl:when test="/ns:Reply/ns:parameter[@ns:text="OTPRequired"]/text() = "false"">

    But since the attribute is itself enclosed in double quotes, have you tried enclosing the text values in single quotes?

    <xsl:when test="/ns:Reply/ns:parameter[@ns:text='OTPRequired']/text() = 'false'"/>

    The predicate would filter all of the parameter elements to just those with a ns:text attribute with the OTPRequired value, and for those parameter elements, just the value of the element (your text()) being a text value of 'false'.  So other than the double quotes it looks good.  I'd think if you had double quotes you'd be getting a compilation error, and perhaps you're getting those in the logs, but the most recently compiled stylesheet is what is being executed?

    Best Regards,

    Steve

    Regards,

    Steve



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



  • 11.  RE: How to check Attribute value in Switch

    Posted Thu December 08, 2022 06:36 AM
    Hi Steve,

    I am not doing this in XSLT previously we resolved this problem through XSLT, But we are trying to achieve using Switch Policy in APIConnect.

    ------------------------------
    kandula nagababu
    ------------------------------



  • 12.  RE: How to check Attribute value in Switch

    Posted Thu December 08, 2022 05:34 PM

    Hi Kandula,

    You have uncovered a defect in the product where the JSONata expression in your switch conditional is being validated at publish time and it does not accept (incorrectly) the $xpath function.  An APAR has been opened and will be fixed in the next fixpack as $xpath should be allowed.  In the meantime, using the XSLT to set some variable in context and having a simple JSONata using that variable (ie, the $xpath function is not used in the condition) is your only recourse. 

    Best Regards,

    Steve



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