다음을 통해 공유


Logic Apps : Create JSON or XML using XSLT through Function App

Introduction

Recently,I have delivered a set of Logic Apps to my client which accepts SOAP messages from Salesforce and delivers into Azure DB. I have observed that we use Integration Account for transforming XMLs in Logic Apps using XSLT that costs alone 1000 USD monthly extra.

Enterprise Integration Pack

To transform a XML document into XML or json,  we need "EnterpriSe Integration Pack" to be associated with Logic Apps like here

Cost

Alternate approach

In order to save money, I have created a Azure Functions or Function App which transforms our XML into XML or Json in .net which accepts 3 parameters as below.

Code

My Function App would look like below,

using System.Net;
using System;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.Linq;
using System.Text;
using Newtonsoft.Json;
 
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
 
    dynamic body = await req.Content.ReadAsStringAsync();
    var json = JsonConvert.DeserializeObject<InputData>(body as string);
     
    XmlDocument inputDocument = new  XmlDocument();
    inputDocument.LoadXml(json.inputString);
    XslCompiledTransform compiledTransform = new  XslCompiledTransform(true);
    XmlReader reader = XmlReader.Create(new System.IO.StringReader(json.xslt));
    compiledTransform.Load(reader);
    MemoryStream ms = new  MemoryStream();
 
    UTF8Encoding encode = new  System.Text.UTF8Encoding();
    XmlTextWriter output = new  XmlTextWriter(ms, encode);
    XmlResolver resolver = new  XmlUrlResolver();
 
    compiledTransform.Transform(inputDocument, null, output, resolver);
 
     
    byte[] outArray = ms.ToArray();    
    string outstring = encode.GetString(outArray);
    return req.CreateResponse(HttpStatusCode.OK, outstring);
}
 
 
public class InputData
{
    public string inputString { get; set; }
    public string xslt { get; set; }
    public string arguments { get; set; }
}

To make it run in Azure functions we need to add a file, call it "Project.json" and content should include the below,

Usage

To this Function App, we need to pass 3 parameters but as of now I have not used the arguments parameter.

  1. Input String 
  2. Transform XSLT
  3. Arguments (not used)

The first parameter is the input XML for transforming, second parameter is the XSLT itself without double quotes in it. Those are passed as string inside the function App.

Testing

In the below sample, I have generated XML using XSLT.

See Also

Another important place to find a huge amount of Logic Apps related articles is the TechNet Wiki itself.