如何在xslt 3.0中迭代json数组

watbbzwu  于 2023-03-20  发布在  其他
关注(0)|答案(1)|浏览(135)

我不得不在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>
beq87vna

beq87vna1#

我认为您可能需要以下内容:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:mf="http://www.example.com/mf"
  exclude-result-prefixes="#all"
  expand-text="yes">
  
  <xsl:output method="json" indent="yes"/>

  <xsl:function name="mf:apply-templates" as="item()*">
    <xsl:param name="items" as="item()*"/>
    <xsl:apply-templates select="$items"/>
  </xsl:function>

  <xsl:template match="." name="xsl:initial-template">
    <xsl:sequence select="array { mf:apply-templates(?members?*) }"/>
  </xsl:template>
  
  <xsl:template match=".[. instance of map(xs:string, xs:string)]">
    <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:template>
  
</xsl:stylesheet>

但是结果有一个由两个不同Map组成的数组,而不是输出所显示的两个相同的Map;因此输出为

[
   {
      "displayName": "Molecule Man",
      "attributes": {
         "testing": [
            {
               "value": "Dan Jukes"
            }
         ],
         "test": [
            {
               "value": "29"
            }
         ]
      },
      "resourceType": "Test",
      "type": {
         "name": "Member"
      }
   },
   {
      "displayName": "Iron Man",
      "attributes": {
         "testing": [
            {
               "value": "Martin"
            }
         ],
         "test": [
            {
               "value": "30"
            }
         ]
      },
      "resourceType": "Test",
      "type": {
         "name": "Member"
      }
   }
]

相关问题