簡體   English   中英

Dataweave XML-XML轉換“無法將a:數組強制轉換為:string。”

[英]Dataweave XML-XML Transformation “Cannot coerce a :array to a :string.”

我正在使用Dataweave將有效負載中的XML重新格式化為程序所期望的格式:

有效載荷

<reservation>
    <id>1</id>
    <begin_time>12:00 PM</begin_time>
    <end_time>1:00 PM</begin_time>
    <other_field>Misc. Data</other_field>
</reservation>
.
.
.

預期產出。

<reservation>
    <id>1</id>
    <begin_time>12:00 PM</begin_time>
    <schedule>
        <begin_time>12:00 PM</begin_time>
        <end_time>1:00 PM</end_time>
    </schedule>
</reservation>
.
.
.

數據編碼

%dw 1.0
%output application/xml
%namespace ns0 http://www.collegenet.com/r25
---
using (r = (flatten payload.ns0#reservations.*ns0#reservation))

    Reservation: r groupBy $.ns0#reservation_id pluck {
        id : $.ns0#reservation_id ,
        StartTime: $.ns0#reservation_start_dt,
        Schedule : {
            ReservationStartDate : $.ns0#reservation_start_dt,
            PreEventDate : $.ns0#pre_event_dt,
            EventStartDate : $.ns0#event_start_dt,
            PostEventDate : $.ns0#post_event_dt,
            ReservationEndDate : $.ns0#reservation_end_dt
        }   
    }

每當我嘗試這段代碼時,我都會收到錯誤:

“執行時出現異常:保留:r map {^無法將a:數組強制轉換為:object。”

當我嘗試完全相同的代碼但轉換為JSON而不是XML時,轉換工作完美。 如果我刪除了地圖,代碼可以工作,但它會加入同一標題下所有預訂的所有ID。 我不明白為什么Anypoint將xml解釋為數組而不是對象。

您需要注意的第一件事是,無論何時使用map進行迭代,都必須使用$訪問當前元素數據,使用$$訪問索引。 這里r.ns0#reservation_id試圖從:array強制轉換為:object。 嘗試將代碼更改為

%dw 1.0
%output text/xml
%namespace ns0 http://www.mynamespace.com/ns0
---
using (r = (flatten payload.ns0#reservations.*ns0#reservation))

Reservation: r map {
    id : $.ns0#reservation_id,
    StartTime: $.ns0#reservation_start_dt,
    Schedule : {
        ReservationStartDate : $.ns0#reservation_start_dt,
        ReservationEndDate : $.ns0#reservation_end_dt
    }   
}

還觀察到輸入XML具有與代碼中使用的名稱不同的標記名稱。 為了更具可讀性,您可以將lambda與map一起使用

%dw 1.0
%output text/xml
%namespace ns0 http://www.mynamespace.com/ns0
---
using (r = (flatten payload.ns0#reservations.*ns0#reservation))

Reservation: r map ((reservation, reservationIndex) -> {
    id : reservation.ns0#reservation_id,
    StartTime: reservation.ns0#reservation_start_dt,
    Schedule : {
        ReservationStartDate : reservation.ns0#reservation_start_dt,
        ReservationEndDate : reservation.ns0#reservation_end_dt
    }   
})

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM