XML 到 Json 映射 - 索引上的数据编织脚本过滤

XML to Json Mapping -Data weave script filtration on indexing

我必须在我的代码中实现小逻辑 - 这里我收到一个 XML 请求,它必须被转换成 JSON。在转换之前,我必须实现小逻辑,我的输入 XML 是:

<details>
  <mydetail>
    <Firstdate>2017</Firstdate>
    <futuredate>1</futuredate>
  </mydetail>
  <mydetail>
    <Firstdate>2017</Firstdate>
    <futuredate>2</futuredate>
  </mydetail>
  <mydetail>
    <Firstdate>2017</Firstdate>
    <futuredate>3</futuredate>
  </mydetail>
</details>

我的预期 JSON 是:

[
  {
    "Currentdate": "2017",
    "Nextdate: null

  },
  {
   "Currentdate": "2017",
    "Nextdate "1"
  },
 {
    "Currentdate": "2017",
    "Nextdate "2"
  }
]

逻辑是:
作为响应,如果它是 JSON 中的第一个 Nextdate,那么它将始终是 null,否则 Nextdate 将映射到 futuredate 的先前值。

我写了下面的数据编织脚本,但它不起作用。

payload.details.*mydetail map {
    Currentdate: $.Firstdate,
    Nextdate: null when $$==0 otherwise $.Seconddate [$$-1]  ,
}

但是这个脚本不起作用,我没有得到预期的结果。
出现此错误的原因可能是什么?

使用以下脚本获得所需的输出

%dw 1.0
%output application/json
---
payload.details.*mydetail map  {
    Currentdate: $.Firstdate,
    Nextdate: null when $$ == 0 otherwise payload.details.*mydetail.futuredate[$$ - 1]  
}

您的脚本在映射 Nextdate 时有额外的 ,,这导致了错误。