【发布时间】:2019-09-26 09:37:46
【问题描述】:
我想将 JSON 数据转换为 xml。出于这个原因,我使用函数 json-to-xml。
JSON
{
"example": {
"items": [
{
"_links": {
"self": {
"href": "xxx"
}
},
"test": "123",
"nummer": "456",
"parent": null,
"category": [
"test"
],
"select": true,
"values": {
"abc": [
{
"test": null,
"data": 2
}
]
},
"cpu": {
"1": {
"cpus": []
},
"3": {
"cpus": []
},
"4": {
"cpus": [
"cpu1"]
},
"5": {
"cpus": [
"cpu6"
]
}
}
},
{
"_links": {
"self": {
"href": "xxx"
}
},
"test": "789",
"nummer": "4110",
"parent": null,
"category": [
"test"
],
"select": true,
"values": {
"abc": [
{
"test": null,
"data": 2
}
]
},
"cpu": {
"1": {
"cpus": []
},
"3": {
"cpus": []
},
"4": {
"cpus": [
"cpu3"
]
},
"5": {
"cpus": [
"cpu4",
"cpu5"
]
}
}
}
]
}
}
我使用的 xslt:
<xsl:param name="input" select="'url json'"/>
<xsl:template name="initial">
<xsl:apply-templates select="json-to-xml(unparsed-text($input))" mode="copy"/>
</xsl:template>
<xsl:template match="node() | @*" mode="copy">
<xsl:copy>
<xsl:apply-templates select="node() | @*" mode="copy"/>
</xsl:copy>
</xsl:template>
结果是这样的:
<map xmlns="http://www.w3.org/2005/xpath-functions">
<map key="example">
<array key="items">
<map>
<map key="_links">
<map key="self">
<string key="href">xxx</string>
</map>
</map>
<string key="test">123</string>
<string key="nummer">456</string>
<null key="parent"/>
<array key="category">
<string>test</string>
</array>
<boolean key="select">true</boolean>
<map key="values">
<array key="abc">
<map>
<null key="test"/>
<number key="data">2</number>
</map>
</array>
</map>
<map key="cpu">
<map key="1">
<array key="cpus"/>
</map>
<map key="3">
<array key="cpus"/>
</map>
<map key="4">
<array key="cpus">
<string>cpu1</string>
</array>
</map>
<map key="5">
<array key="cpus">
<string>cpu6</string>
</array>
</map>
</map>
</map>
<map>
<map key="_links">
<map key="self">
<string key="href">xxx</string>
</map>
</map>
<string key="test">789</string>
<string key="nummer">4110</string>
<null key="parent"/>
<array key="category">
<string>test</string>
</array>
<boolean key="select">true</boolean>
<map key="values">
<array key="abc">
<map>
<null key="test"/>
<number key="data">2</number>
</map>
</array>
</map>
<map key="cpu">
<map key="1">
<array key="cpus"/>
</map>
<map key="3">
<array key="cpus"/>
</map>
<map key="4">
<array key="cpus">
<string>cpu3</string>
</array>
</map>
<map key="5">
<array key="cpus">
<string>cpu4</string>
<string>cpu5</string>
</array>
</map>
</map>
</map>
</array>
</map>
</map>
这就是我想要的。
<?xml version="1.0" encoding="UTF-8"?>
<example>
<items>
<_links>
<self>
<href>xxx</href>
</self>
</_links>
<test>123</test>
<nummer>456</nummer>
<parent>null</parent>
<category>test</category>
<select>true</select>
<values>
<abc>
<test>null</test>
<data>2</data>
</abc>
</values>
<cpu>
<_1></_1>
<_3></_3>
<_4>
<cpus>cpu1</cpus>
</_4>
<_5>
<cpus>cpu6</cpus>
</_5>
</cpu>
</items>
<items>
<_links>
<self>
<href>xxx</href>
</self>
</_links>
<test>789</test>
<nummer>4110</nummer>
<parent>null</parent>
<category>test</category>
<select>true</select>
<values>
<abc>
<test>null</test>
<data>2</data>
</abc>
</values>
<cpu>
<_1></_1>
<_3></_3>
<_4>
<cpus>cpu3</cpus>
</_4>
<_5>
<cpus>cpu4</cpus>
<cpus>cpu5</cpus>
</_5>
</cpu>
</items>
</example>
我想删除键属性。另外元素“items”应该是xml结构的一部分。 有没有人有这个问题的解决方案?
提前致谢!
【问题讨论】:
-
能分享一下代码sn-p和json文件吗?
-
@RaviKulkarni 是的,当然,它完成了