azure - 用于获取 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"
}
]
}
解决方案
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。
推荐阅读
- javascript - 如何从匿名函数访问 Vue 实例
- r - 使 SQL 和 R 单元在 R Notebook 中始终一起运行
- python - 如何摆脱嵌套循环并继续外循环的迭代
- java - RSAPubKey 元素中的模值不匹配
- django - 错误消息“mkvirtualenv 未被识别为内部或外部命令”
- android - Android - 使用数据绑定来分配布局?
- c# - 弹性搜索(使用 NEST)在 MatchAll() 上返回 0 个结果
- r - 无法安装包 'ggpmisc' 不可用(对于 R 版本 3.5.0)
- r - 在 Windows CMD (powershell) 中使用正则表达式从 XML 中删除 CDATA
- labview - 如何通过 Labview 上的热电偶获取温度