0

I am trying Liquid template to convert XML to Json with some transformation. I have sample XML as shown below

Sample XML:

<Employees>
 <Employee>
  <name>abc</name>
  <summary>
   <Age>15</Age>
   <tag1>dd</tag1>
   <tag2>dd</tag2>
   <tag2>dd</tag2>
  </summary>
 </Employee>
</Employees>

My Liquid template

{
"Root": 
    [
     {% for employees in content.Employees %}
    {
        "Name": "{{employees.name}}",
        "Summary": "summary is {{employees.summary}}",
  "Age": "{{employees.summary.Age}}"
    },
    {% endfor %}
    ]
}

I got the Output as below

{
"Root": [
 {
  "Name": "abc",
  "Summary": "summary is ",
  "Age": "15"
 }
 ]
}

For Summary json node I want to display complete summary xml node and child nodes as it is(xml format), but now I am receiving empty. I tried searching to achieve this in liquid template and didn't get any option to achieve this. If not possible then what alternatives can be used in Azure logic apps.

Expected output:

 {
"Root": [
 {
  "Name": "abc",
  "Summary": "summary is <summary><Age>15</Age><tag1>dd</tag1><tag2>dd</tag2><tag2>dd</tag2></summary>",
  "Age": "15"
 }
 ]
}
techresearch
  • 119
  • 3
  • 14

1 Answers1

0

XSLT 3 can transform your XML to JSON with

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
    version="3.0">
    
  <xsl:strip-space elements="*"/>

  <xsl:output method="json" indent="yes"/>

  <xsl:template match="/Employees">
    <xsl:sequence
      select="map { 'Root' : 
                  array { 
                      Employee ! map { 'Name' : data(name), 'Summary' : 'Summary is ' || serialize(summary), 'Age' : number(summary/Age) }
                  }
              }"/>
  </xsl:template>
  
</xsl:stylesheet>

Result:

 {
  "Root": [
     {
      "Summary":"Summary is <summary><Age>15<\/Age><tag1>dd<\/tag1><tag2>dd<\/tag2><tag2>dd<\/tag2><\/summary>",
      "Name":"abc",
      "Age":15
     }
   ]
 }

https://xsltfiddle.liberty-development.net/6q1SDkM/1

I think you can use XSLT 3 by now, based on Saxon 9 .NET, in Azure apps.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • This works as expected. But when i run in logic app the namespace gets added to the summary like . One more thing i wanted is check a condition like Employee.tag2=somevalue then only do the logic of map – techresearch Feb 05 '21 at 17:11
  • @techresearch, I can't tell why you get those namespaces, unless they are declared somewhere up the tree e.g. on the `Employees` element. As for the second issue about checking a condition, inside of XSLT you can obviously easily use `Employee[tag2 = 'somevalue'] ! map { .. }` instead of the `Employee ! map { .. }` shown above, to populate the JSON array only from `Employee` elements meeting that condition. – Martin Honnen Feb 05 '21 at 17:46
  • Nice!! This xml conditioning working as expected. As you mentioned In my original XML i do have namespace up in the tree so it comes up.If i remove that namespace it wont come up in summary. So is there anyway i can handle this removal of namespace from output summary in XSLT itself. – techresearch Feb 05 '21 at 18:05
  • @techresearch, you can push the summary element through a mode that uses `copy-namespaces="no"`. If you need help on that better raise it as a separate question. – Martin Honnen Feb 05 '21 at 18:07
  • I have created new question for this https://stackoverflow.com/questions/66098112/remove-namespace-from-serialized-xml-in-xslt – techresearch Feb 08 '21 at 08:28