3

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.

2 Answers 2

4

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']
Sign up to request clarification or add additional context in comments.

1 Comment

Nice. For some reason I need to cast '2013-05-04T00:00:00' as a dateTime for it to work in my environment, but it works.
1

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

for $x in /contacts/contact return max($x//@date/xs:dateTime(.))

1 Comment

I need to get contacts who haven't been contacted after a certain date. Taking your xpath above, I've come up with for $x in //contact return max($x//@date/xs:dateTime(.))[. < '2013-05-04']. But then how do I get the name of the contact?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.