首页 > 解决方案 > 用于获取 XML 节点和子节点 xml 的液体模板,因为它是 json 格式

问题描述

我正在尝试 Liquid 模板通过一些转换将 XML 转换为 Json。我有如下所示的示例 XML

示例 XML:

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

我的液体模板

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

我得到如下输出

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

对于摘要 json 节点,我想按原样显示完整的摘要 xml 节点和子节点(xml 格式),但现在我收到的是空的。我尝试在液体模板中搜索以实现这一目标,但没有任何选择来实现这一目标。如果不可能,那么可以在 Azure 逻辑应用中使用哪些替代方案。

预期输出:

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

标签: azurexsltmapsazure-logic-appsliquid-template

解决方案


XSLT 3 可以将您的 XML 转换为 JSON

<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>

结果:

 {
  "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

我认为您现在可以在 Azure 应用程序中使用基于 Saxon 9 .NET 的 XSLT 3。


推荐阅读