Search code examples
xmlazure-logic-appsjsonconvert

Can I append or manipulate XML in Azure Logic App?


I'm using a Logic App to Convert a JSON file to XML.

After the conversion I want to manipulate some of the XML elements to include certain text. Please see below code examples. Is this possible to achieve inside the logic app? Or do I need to manipulate the JSON before converting?

Pre conversion:

{
    "IDRecord": [
        {
            "EmployeeLastName": "Doe",
            "EmployeeFirstName": "John",
            "EmployeeUserid": "JD",
            "SomeField": "Test"
        }
    ]
}

After conversion (Root element ID is created by converting function):

<ID>
    <IDRecord>
        <EmployeeLastName>Doe</EmployeeLastName>
        <EmployeeFirstName>John</EmployeeFirstName>
        <EmployeeUserid>JD</EmployeeUserid>
        <SomeField>Test</SomeField>
    </IDRecord>
</ID>

What I want to achieve:

<ID xmlns="someUrl">
    <IDRecord xmlns="">
        <EmployeeLastName>Doe</EmployeeLastName>
        <EmployeeFirstName>John</EmployeeFirstName>
        <EmployeeUserid>JD</EmployeeUserid>
        <SomeField>Test</SomeField>
    </IDRecord>
</ID>

Any help is appreciated! Thanks


Solution

  • You can use XSLT maps for XML transformation in Azure Logic Apps.

    IMHO, there is no need to specify an empty namespace for the <IDRecord> element.

    Input XML

    <ID>
        <IDRecord>
            <EmployeeLastName>Doe</EmployeeLastName>
            <EmployeeFirstName>John</EmployeeFirstName>
            <EmployeeUserid>JD</EmployeeUserid>
            <SomeField>Test</SomeField>
        </IDRecord>
    </ID>
    

    XSLT 2.0

    <?xml version="1.0"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes"/>
        <xsl:strip-space elements="*"/>
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="ID">
            <ID xmlns="someUrl">
                <xsl:apply-templates/>
            </ID>
        </xsl:template>
    
        <xsl:template match="IDRecord">
            <xsl:copy>
                <xsl:attribute name="xmlns" namespace=""/>
                <xsl:apply-templates/>
            </xsl:copy>
        </xsl:template>
    </xsl:stylesheet>
    

    Output XML

    <ID xmlns="someUrl">
      <IDRecord xmlns="">
        <EmployeeLastName>Doe</EmployeeLastName>
        <EmployeeFirstName>John</EmployeeFirstName>
        <EmployeeUserid>JD</EmployeeUserid>
        <SomeField>Test</SomeField>
      </IDRecord>
    </ID>