0

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

DrejarN
  • 13
  • 3
  • You can use XSLT maps for XML transformation in Azure Logic Apps: https://learn.microsoft.com/en-us/azure/logic-apps/logic-apps-enterprise-integration-maps?tabs=consumption – Yitzhak Khabinsky Sep 24 '21 at 14:58

1 Answers1

0

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>
Yitzhak Khabinsky
  • 18,471
  • 2
  • 15
  • 21
  • Couldnt upvote ur comment, but thanks. Works perfectly. It stops working when the file contains too many lines (50k). Guessing that's a limitation in the XSLT transform – DrejarN Sep 26 '21 at 13:37
  • Good to hear that the proposed solution is working for you. Please mark it as answered. You simply need to mark an answer as correct (the green check image). Click the green outlined checkmark to the left of the answer that solved your problem. This marks the answer as "accepted" – Yitzhak Khabinsky Sep 26 '21 at 15:03
  • Microsoft is using free Saxonica Home Edition (HE) engine behind the scenes. They couldn't afford a paid Professional Edition (PE). That's the unfortunate reason for the XML size limitation. The Saxonica PE edition doesn't have any XML size limitation. – Yitzhak Khabinsky Sep 26 '21 at 15:19