我不得不在xslt中迭代json。我已经尝试了xsl:for-each。但是得到了错误 *JSON输出方法不能处理两个或多个项目的序列 。为了处理这个错误,我已经尝试了嵌套Map。但是得到了另一个错误 * 构造Map中的重复键:{资源类型}.
输入Json
{
"members": [
{
"name": "Molecule Man",
"age": "29",
"secretIdentity": "Dan Jukes"
},
{
"name": "Iron Man",
"age": "30",
"secretIdentity": "Martin"
}
]
}
输出Json
[
{
"resourceType": "Test",
"type": {
"name": "Member"
},
"displayName": "Molecule Man",
"attributes": {
"test": [
{
"value": "29"
}
],
"testing": [
{
"value": "Dan Jukes"
}
]
}
},
{
"resourceType": "Test",
"type": {
"name": "Member"
},
"displayName": "Molecule Man",
"attributes": {
"test": [
{
"value": "29"
}
],
"testing": [
{
"value": "Dan Jukes"
}
]
}
}
]
I have to take the input as Input Json and the required output will be the output json.
我已经尝试了下面的xslt代码
示例.xsl
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
xmlns:mf="http://example.com/mf"
xmlns:js="http://www.w3.org/2005/xpath-functions"
exclude-result-prefixes="js">
<xsl:param name="nodes" as="map(*)*"/>
<xsl:template name="init">
<xsl:apply-templates select="($nodes)"/>
</xsl:template>
<xsl:template match=".">
<xsl:for-each select="?members?*">
<xsl:map>
<xsl:map-entry key="'resourceType'" select="'Test'"/>
<xsl:map-entry key="'type'">
<xsl:map-entry key="'name'" select="'Member'"/>
</xsl:map-entry>
<xsl:map-entry key="'displayName'" select="?name"/>
<xsl:map-entry key="'attributes'">
<xsl:map>
<xsl:map-entry key="'test'" select="[map { 'value' : ?age }]"/>
<xsl:map-entry key="'testing'" select="[map { 'value' : ?secretIdentity }]"/>
</xsl:map>
</xsl:map-entry>
</xsl:map>
</xsl:for-each>
</xsl:template>
<xsl:output method="json" indent="yes"/>
</xsl:stylesheet>
1条答案
按热度按时间beq87vna1#
我认为您可能需要以下内容:
但是结果有一个由两个不同Map组成的数组,而不是输出所显示的两个相同的Map;因此输出为