0

I have a logic to filter out students who joined before 30 days. I have the date of joining and lastdate in an xml element. I have to subtract the dates from these two fields using data weave.

<School>
  <joindate>2015-10-18T00:00:00.000-08:00</joindate>
  <lastdate>2016-01-18</lastdate>
</School>
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
Satheesh Kumar
  • 797
  • 7
  • 31

2 Answers2

2

There are a number of date and time functions available with XPath/XSLT 2.0 and greater. It appears that DataWeave supports up to XSLT 3.0

The following expression will address all of the School elements where the difference in days between lastdate and joindate is less than 30.

//School[days-from-duration(xs:date(lastdate) - xs:date(xs:dateTime(joindate))) lt 30]
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
  • No, it will return a Date. Subtracting a Date from it will return a Duration. You can get the number of days with days-from-duration() – Mads Hansen Jan 18 '16 at 14:28
  • Hi Mads Hansen, i tried above syntax in dataweave but it's not supporting the [ braces can you please enlighten me how to do this am struck at this and strugling to solve. Can you say how can we implement the above code in data weave – Satheesh Kumar Jan 19 '16 at 13:01
  • The square braces are a predicate filter (similar to a where clause in SQL) that filter the items to the left of the braces, only returning those for which the predicate statement returns true. I don't have any experience with Data Weave, and am not sure how/where you are trying to apply it, so I don't think I can help you figure out how to apply it. Could you post a little more code showing how you are trying to select/filter the School elements? – Mads Hansen Jan 19 '16 at 15:13
  • Does dataweave support XSLT ? The link u gave describes XSLT but doesnt actually give any relation between dataweave and XSLT. Please elaborate or share links if u have any. – tortoise May 17 '16 at 09:44
2

Give it a try with the DataWeave Date Time operations:

https://docs.mulesoft.com/mule-user-guide/v/3.7/dataweave-reference-documentation#adding-a-period-of-time

get the values from your XML and store them in variables in DataWeave, cast them as :date and subtract them in the script.

this is an example that gives you an object, i think you are able to fix it from there?

%dw 1.0
%output application/java
%var join = payload.School.joindate as :date
%var last = payload.School.lastdate as :date
---
period: join - last
JoostD
  • 734
  • 6
  • 13
  • Hi JoostD Thanks for the help but am getting the output as P-25Y-11M-16D how can I convert this to days – Satheesh Kumar Jan 18 '16 at 13:05
  • the ouput payload contains an object of type period, you wont have all the days available as a number in there. have a look at this post for more info on that maybe that helps you: http://stackoverflow.com/questions/30833582/how-to-calculate-the-number-of-days-in-a-period – JoostD Jan 19 '16 at 16:38