Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
546 views
in Technique[技术] by (71.8m points)

azure - Liquid template to get XML node and child nodes xml as it is to json format

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"
 }
 ]
}
question from:https://stackoverflow.com/questions/66061335/liquid-template-to-get-xml-node-and-child-nodes-xml-as-it-is-to-json-format

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...