Given the following XML, what Xpath 2.0 query will get me the maximum date for each contact? My goals ito find contacts who haven't been touched in 30 days.
<contacts>
<contact>
<name>james</name>
<touch method='email' date='2002-02-04'>a</touch>
<touch method='meeting' date='2010-02-04'>b</touch>
</contact>
<contact>
<name>bob</name>
<touch method='phone' date='2001-02-04'>y</touch>
<touch method='email' date='2009-02-04'>d</touch>
</contact>
<contact>
<name>cindy</name>
<touch method='email' date='2012-02-04'>v</touch>
<touch method='phone' date='2012-02-04'>h</touch>
</contact>
<contact>
<name>john</name>
</contact>
</contacts>
max((//@date/xs:dateTime(.))) will get me a single maximum date but I'm trying to get three dates.
Easier version.
You generally do not need to for-in-return for sequences of nodes, as / does the same.
/contacts/contact/max(.//@date/xs:dateTime(.))
and to find the contact before a limit:
/contacts/contact[max(.//@date/xs:dateTime(.)) < '2013-05-04']
Use for as you want to get the maximum value for each contact
for $x in /contacts/contact return max($x//@date/xs:dateTime(.))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With