Logic App Xpath Output

Noah Van Loen 61 Reputation points
2020-09-12T22:23:26.137+00:00

I am retrieving an XML response from an HTTP request, and would like to parse that into local Logic App variables. I'm using the Xpath() expression here:

xpath(xml(body('HTTP_ACTION_NAME')),'//sessionid')

I am getting the correct field value, however the output is in the format:

[
    {
        "$content-type": "application/xml;charset=utf-8",
        "$content": "FIELD_VALUE"
    }
]

Two questions:

  1. Is this the correct type of response that I should be expecting, or am I not forming the xpath command correctly?
  2. Is there an easy way to parse the response that I'm getting to extract the "FIELD_VALUE"?
Azure Logic Apps
Azure Logic Apps
An Azure service that automates the access and use of data across clouds without writing code.
3,542 questions
{count} votes

Accepted answer
  1. Pramod Valavala 20,656 Reputation points Microsoft Employee Moderator
    2020-09-14T05:04:06.32+00:00

    The xpath that you are using returns the XML node and not the value exactly. Also, since it returns all matches across the document, it returns an array instead.

    This expression should work instead - xpath(xml(variables('xml_data')), '//sessionid/text()')[0]

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Noah Van Loen 61 Reputation points
    2020-09-13T16:17:02.57+00:00

    According to Example 1 https://learn.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#xpath

    I should be able to get the following to run, although I am getting the error:
    InvalidTemplate. Unable to process template language expressions in action 'Compose_3' inputs: 'The template language function 'xml' parameter is not valid. The provided value cannot be converted to XML: 'Data at the root level is invalid. Line 1, position 1.'.

    Compose_4 {
    "inputs": "\"<?xml version=\"1.0\"?> <produce> <item> <name>Gala</name> <type>apple</type> <count>20</count> </item> <item> <name>Honeycrisp</name> <type>apple</type> <count>10</count> </item> </produce>\"",
    }

            "Compose_3": {  
                "inputs": "@xpath(xml(outputs('Compose_4')),'/produce/item/name')",  
                "runAfter": {  
                    "Compose_4": [  
                        "Succeeded"  
                    ]  
                },  
            },  
    

    BTW this is obvs not the answer, but I was unable to paste and save this into another Comment...?


  2. Noah Van Loen 61 Reputation points
    2020-09-14T22:32:34.337+00:00

    Thank you @Pramod Valavala - here's what I'm getting now (snipped for brevity):

    "Initialize_variable": {
    "inputs": {
    "variables": [
    {
    "name": "XML_Output",
    "type": "string",
    "value": "\"<?xml version=\"1.0\"?> <produce> <item> <name>Gala</name> <type>apple</type> <count>20</count> </item> <item> <name>Honeycrisp</name> <type>apple</type> <count>10</count> </item> </produce>\""
    }
    ]
    }

    "Get_Name_Value": {
    "inputs": "@xpath (xml(variables('XML_Output')),'/produce/item/name/text()')[0]",
    "type": "Compose"
    },

    error:
    InvalidTemplate. Unable to process template language expressions in action 'Get_Name_Value' inputs at line '1' and column '2863': 'The template language function 'xml' parameter is not valid. The provided value cannot be converted to XML: 'Data at the root level is invalid. Line 1, position 1.'. Please see https://aka.ms/logicexpressions#xml for usage details.'.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.