Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xpath - get maximum attribute for each element

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.

like image 641
GGGforce Avatar asked Mar 05 '26 21:03

GGGforce


2 Answers

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']
like image 61
BeniBela Avatar answered Mar 08 '26 13:03

BeniBela


Use for as you want to get the maximum value for each contact

for $x in /contacts/contact return max($x//@date/xs:dateTime(.))
like image 20
dirkk Avatar answered Mar 08 '26 12:03

dirkk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!