
Questions: 19,736 //
Answers: 43,524 //
Contributing Members: 14,735
I am using following dw code to change months and years in "*" :- %dw 1.0 %output application/xml
test : { (flowVars.sample.temp mapObject { ($$) : $ unless $$ ~= "years" otherwise "" , ($$) : $ unless $$ ~= "months" otherwise ""
}) }
but it is giving me :-
<diff>-4</diff>
<diff>-4</diff>
<years>*</years>
<years>0</years>
<months>10</months>
<months>*</months>
<days>1202400.0</days>
<days>1202400.0</days>
where as i want something like this:-
<diff>-4</diff>
<years>*</years>
<months>*</months>
<days>1202400.0</days>
input xml is:-
<diff>-4</diff>
<years>0</years>
<months>0</months>
<days>1202400.0</days>
Jul 17, 2017 at 09:24 AM, phulme answered with:
Sorry, copy paste errors from OP . For different values you nest the 'when .. otherwise ..' clauses. I have switched to as :string as well.
test : {
(flowVars.sample.temp mapObject {
($$) : '#' when ($$ as :string) == "months"
otherwise ('*' when ($$ as :string) == "years" otherwise $)
}) }
@Phil Hulme : Is there any way i can update the attribute value as well. e.g in one xml below i want to update the "id" attribute only from "abc" to "*".
<element1 id="abc">abc-time change</element1>
<element2>cdf</element2>
<element3>xyz</element3>
</check>
Try this;
%dw 1.0
%output application/xml skipNullOn = "attributes"
---
element1 @(id: '*' when payload.element1.@id == 'abc' otherwise payload.element1.@id) :payload.element1
it assumes input is just the element1 line so needs to go into your overall weave at the appropriate place. NOTE: skipNullOn = "attributes" removes any attributes in the output if they have a null value to deal with the case of id not being present in the input.
It is giving below xml which is wrong:_
<element1 id="*">abc-time change</element1>
<element1 id="*">abc-time change</element1>
<element1 id="*">abc-time change</element1>
i want other elements to be as is and only want to change the "id" value. I am using following dataweave script:- (payload is in flowVar) %dw 1.0 %output application/xml
test : { (flowVars.change.check mapObject (payload2,index2) -> { (index2) : element1 @(id: '*' when flowVars.change.check.element1.@id == 'abc' otherwise flowVars.change.check.element1.@id) :flowVars.change.check.element1 })
}
@Phil Hulme : The "id" attribute can be present with any xml element. Currently i have 3 xml elements out of which only one has "id" attribute. Number of xml elements are also not fixed. i can have any number of elements with in root tag.
That is possible but starts to get a bit messy.
This input;
<check>
<element1 id='abc'>abc-time change</element1>
<element2 id='123'>e1</element2>
<element3>e2</element3>
<otherField>some value</otherField>
</check>
through this transform;
%dw 1.0
%output application/xml skipNullOn = "attributes"
---
check:(payload.check mapObject {
($$ as :string) @(id: '*' when payload.check."$($$ as :string)".@id == 'abc' otherwise payload.check."$($$ as :string)".@id) :$
})
gives this output;
<check>
<element1 id="*">abc-time change</element1>
<element2 id="123">e1</element2>
<element3>e2</element3>
<otherField>some value</otherField>
</check>
Jul 17, 2017 at 08:42 AM, phulme answered with:
Where you have repeated for the months you have created a second element. Hence you get two lines in the output for each line in the input. one set with year value replaced and one set with month value replaced. You need to combine the "year" and "month" conditions like this.
test : {
(flowVars.sample.temp mapObject {
($$) : $ unless ($$ ~= "years" and $$ ~= "months") otherwise "*"
}) }
how about if i need to assign years to "*" but months to "#"?
moreover the solution provided for "*" is also not working. Pls find the o/p given:-
<diff>-4</diff>
<years>0</years>
<months>0</months>
<days>1202400.0</days>
so i need to use :- test : { (flowVars.sample.temp mapObject { ($$) : $ unless ($$ ~= "years" or $$ ~= "months") otherwise ""
}) }*
what if i need to map these two elements to two different values such as years to * but months to # ?
Jul 21, 2017 at 06:17 AM, aradhanamohanty123 answered with:
Add this piece of code: test : { (flowVars.sample.temp mapObject { ($$) : '#' when ($$ as :string) == "months" otherwise ('*' when ($$ as :string) == "years" otherwise $)
}) }
Jul 21, 2017 at 10:44 AM, manvitha answered with:
Add test : { (flowVars.sample.temp mapObject { ($$) : '#' when ($$ as :string) == "months" otherwise ('*' when ($$ as :string) == "years" otherwise $) }) }
Adding an additional tag only to the inbound payload without remapping all fields 2 Answers
XML content stored in a variable is not working in Data Weave 1 Answer
Dataweave Transformation of payload 6 Answers
Can Anyone Generate the Dataweave For that Soap Request(Exact same Request I want In Preview) 1 Answer
how can i use message.id in dataweave 2 Answers