XSLT 3.0 - 在 XSLT 3.0 xml-to-json() 中出现错误 "duplicate key value"

XSLT 3.0 - Getting Error "duplicate key value" in XSLT 3.0 xml-to-json()

我正在尝试使用 XSLT 3.0 将给定的 json 数据从一种形式转换为另一种形式。我正在使用 XSLT 3.0 提供的 json-to-xml 和 xml-to-json 函数来转换数据 from.to json to/from xml .

我有以下 json 数据。

{
 "serviceCode":"ATOM",
 "action":"SCHEDULE",
"customerId":864,
"instance":"DWHPRD",
"serviceParameters":[
  {
     "parameterName":"team",
     "parameterValue":"EBS"
  }
],
"arguments":[
  {
     "argumentKey":"rfc",
     "argumentValue":"3-BW9R3UA"
  },
  {
     "argumentKey":"sid",
     "argumentValue":"DWHPRD"
  },
  {
     "argumentKey":"schedule_at",
     "argumentValue":"2023-07-02 15:10:00"
  },
  {
     "argumentKey":"update_rfc",
     "argumentValue":false
  },
  {
     "argumentKey":"dynamic_args",
     "argumentValue":[
        {
           "argumentKey":"task_name",
           "argumentValue":"Exa CPU Bursting Task"
        },
        {
           "argumentKey":"arg_name",
           "argumentValue":"$CPU Count",
           "parent":{
              "argumentKey":"task_name",
              "argumentValue":"Exa CPU Bursting Task"
           }
        },
        {
           "argumentKey":"arg_value",
           "argumentValue":"2",
           "parent":{
              "argumentKey":"task_name",
              "argumentValue":"Exa CPU Bursting Task"
           }
        },
        {
           "argumentKey":"task_name",
           "argumentValue":"Exa CPU Bursting DB Task"
        },
        {
           "argumentKey":"arg_name",
           "argumentValue":"$Target DB CPU Count",
           "parent":{
              "argumentKey":"task_name",
              "argumentValue":"Exa CPU Bursting DB Task"
           }
        },
        {
           "argumentKey":"arg_value",
           "argumentValue":"3",
           "parent":{
              "argumentKey":"task_name",
              "argumentValue":"Exa CPU Bursting DB Task"
           }
        }
     ]
    }
   ]
  }

我正在尝试使用 XSLT 3.0 将其转换为以下形式

{
"rfc":"3-BW9R3UA",
"sid":"DWHPRD",
"job_id":972,
"schedule_at":"2023-07-02 15:10:00",
"update_rfc":false,
"dynamic_args":[
  {
     "task_name":"Exa CPU Bursting Task",
     "arg_name":"$CPU Count",
     "arg_value":"2"
  },
  {
     "task_name":"Exa CPU Bursting DB Task",
     "arg_name":"$Target DB CPU Count",
     "arg_value":"3"
   }
  ]
}

我想出了以下 XSLT(不完整,因为我在级别 1 时遇到错误)

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
 version="3.0"
 xmlns="http://www.w3.org/2005/xpath-functions"
 xpath-default-namespace="http://www.w3.org/2005/xpath-functions"
 expand-text="yes">
 <xsl:param name="jsonText"/>

 <xsl:output method="text"/>

 <xsl:template name="init">
 <xsl:variable name="input-as-xml" select="json-to-xml($jsonText)"/>
 <xsl:variable name="transformed-xml">

 <map>
 <xsl:for-each select="$input-as-xml//array[@key='arguments']//map">
 <string key="{string[@key='argumentKey']}"> <xsl:value-of 
select="string[@key='argumentValue']"/></string>
 </xsl:for-each>
</map>
</xsl:variable>
 <xsl:value-of select="xml-to-json($transformed-xml)"/>
 </xsl:template>
</xsl:stylesheet>

任何人都可以帮助我如何获得所需的输出json。

您似乎想跳过一些内容,但对嵌套数组内容进行了分组:

<?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="http://www.w3.org/2005/xpath-functions"
  xpath-default-namespace="http://www.w3.org/2005/xpath-functions"
  exclude-result-prefixes="#all"
  expand-text="yes">
  
  <xsl:param name="json" as="xs:string" expand-text="no">{
 "serviceCode":"ATOM",
 "action":"SCHEDULE",
"customerId":864,
"instance":"DWHPRD",
"serviceParameters":[
  {
     "parameterName":"team",
     "parameterValue":"EBS"
  }
],
"arguments":[
  {
     "argumentKey":"rfc",
     "argumentValue":"3-BW9R3UA"
  },
  {
     "argumentKey":"sid",
     "argumentValue":"DWHPRD"
  },
  {
     "argumentKey":"schedule_at",
     "argumentValue":"2023-07-02 15:10:00"
  },
  {
     "argumentKey":"update_rfc",
     "argumentValue":false
  },
  {
     "argumentKey":"dynamic_args",
     "argumentValue":[
        {
           "argumentKey":"task_name",
           "argumentValue":"Exa CPU Bursting Task"
        },
        {
           "argumentKey":"arg_name",
           "argumentValue":"$CPU Count",
           "parent":{
              "argumentKey":"task_name",
              "argumentValue":"Exa CPU Bursting Task"
           }
        },
        {
           "argumentKey":"arg_value",
           "argumentValue":"2",
           "parent":{
              "argumentKey":"task_name",
              "argumentValue":"Exa CPU Bursting Task"
           }
        },
        {
           "argumentKey":"task_name",
           "argumentValue":"Exa CPU Bursting DB Task"
        },
        {
           "argumentKey":"arg_name",
           "argumentValue":"$Target DB CPU Count",
           "parent":{
              "argumentKey":"task_name",
              "argumentValue":"Exa CPU Bursting DB Task"
           }
        },
        {
           "argumentKey":"arg_value",
           "argumentValue":"3",
           "parent":{
              "argumentKey":"task_name",
              "argumentValue":"Exa CPU Bursting DB Task"
           }
        }
     ]
    }
   ]
  }</xsl:param>

  <xsl:output method="text"/>
  
  <xsl:mode on-no-match="shallow-skip"/>

  <xsl:template match="/" name="xsl:initial-template">
    <xsl:variable name="json-xml" select="json-to-xml($json)"/>
    <xsl:variable name="transformed-json-xml">
      <map>
        <xsl:apply-templates select="$json-xml//array[@key = 'arguments']/map"/>
      </map>
    </xsl:variable>
    <xsl:value-of select="xml-to-json($transformed-json-xml, map { 'indent' : true() })"/>
  </xsl:template>
  
  <xsl:template match="map[string[@key = 'argumentKey'] and string[@key = 'argumentValue']]">
    <string key="{string[@key = 'argumentKey']}">{string[@key = 'argumentValue']}</string>
  </xsl:template>
  
  <xsl:template match="map[string[@key = 'argumentKey'] and string[@key = 'argumentValue']][string[@key = 'argumentValue'] castable as xs:double]">
    <number key="{string[@key = 'argumentKey']}">{string[@key = 'argumentValue']}</number>
  </xsl:template>
  
  <xsl:template match="map[string[@key = 'argumentKey'] and boolean[@key = 'argumentValue']]">
    <boolean key="{string[@key = 'argumentKey']}">{boolean[@key = 'argumentValue']}</boolean>
  </xsl:template>
  
  <xsl:template match="map[string[@key = 'argumentKey'] and array[@key = 'argumentValue']]">
    <array key="{string[@key = 'argumentKey']}">
      <xsl:for-each-group select="array/map" group-starting-with="map[not(map[@key = 'parent'])]">
        <map>
          <xsl:apply-templates select="current-group()"/>
        </map>
      </xsl:for-each-group>
    </array>
  </xsl:template>

</xsl:stylesheet>

我不明白 "job_id":972 的来源以及输出中的数字何时应该是数字或字符串。