MuleSoft DataWeave检查XML fild是否存在

时间:2017-01-17 22:53:13

标签: mule dataweave

我有以下mule DataWeave转换:

([]) when (payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments =="") otherwise 
{
Id: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.@id as :string,
Date: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#Date,
Time: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#Time,
Cancel: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#Cancel as :string,
VisitType: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#VisitType,
VisitTypeID: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#VisitTypeID as :string,
Duration: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#Duration as :string,
Confirm: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#Confirm as :string,
Providers: {
    Provider: {
        Id: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#Providers.ns0#Provider.@id as :string,
        Name: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#Providers.ns0#Provider.@name,
        Department: {
            Id: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#Providers.ns0#Provider.ns0#Department.@id as :string,
            Name: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#Providers.ns0#Provider.ns0#Department.@name,
            Center: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#Providers.ns0#Provider.ns0#Department.@center,
            DepartmentDirections: "" when payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#Providers.ns0#Provider.ns0#Department.ns0#DepartmentDirections == null otherwise payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#Providers.ns0#Provider.ns0#Department.ns0#DepartmentDirections
        }
    }
},
PatientInstructions: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#PatientInstructions,
Copay: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#Copay as :string
}

一切都有效,除了字段" DepartmentDirections"并不总是在XML中传递。所以现在当我添加这个字段时,我的webservice不会返回任何内容,但是当我删除它时,它可以正常工作。由于该字段可能存在,也可能不存在,我不认为检查null会有所帮助(在不工作的情况下,该字段不存在)。有没有办法在分配之前轻松检查实际字段是否存在(而不是值)?

1 个答案:

答案 0 :(得分:1)

添加标题skipNullOn="everywhere",如果输入中不存在,它将在转换时跳过字段。 %output application/json skipNullOn="everywhere" 仅适用于XML和JSON

供参考:https://docs.mulesoft.com/mule-user-guide/v/3.7/dataweave-reference-documentation