title: XPath to get nodes with a matching child-node and highest value for other child-node
body: <p>I need help in XPath 1.0 for filtering the below XML such that I get only the bars with distinct 'id' and with the highest 'validity/date':</p>
  
  <pre><code>&lt;foo name="fooName"&gt;
      &lt;bar name="barName"&gt;
          &lt;id&gt;1111&lt;/id&gt;
          &lt;validity&gt;
              &lt;date&gt;20170920&lt;/date&gt;
          &lt;/validity&gt;
      &lt;/bar&gt;
      &lt;bar name="barName"&gt;
          &lt;id&gt;1111&lt;/id&gt;
          &lt;validity&gt;
              &lt;date&gt;20170922&lt;/date&gt;
          &lt;/validity&gt;
      &lt;/bar&gt;
      &lt;bar name="barName"&gt;
          &lt;id&gt;1111&lt;/id&gt;
          &lt;validity&gt;
              &lt;date&gt;20170921&lt;/date&gt;
          &lt;/validity&gt;
      &lt;/bar&gt;
      &lt;bar name="barName"&gt;
          &lt;id&gt;2222&lt;/id&gt;
          &lt;validity&gt;
              &lt;date&gt;20170921&lt;/date&gt;
          &lt;/validity&gt;
      &lt;/bar&gt;
      &lt;bar name="barName"&gt;
          &lt;id&gt;2222&lt;/id&gt;
          &lt;validity&gt;
              &lt;date&gt;20170923&lt;/date&gt;
          &lt;/validity&gt;
      &lt;/bar&gt;
  &lt;/foo&gt;
  </code></pre>
  
  <p>I tried a lot of options and research, but not able to figure out the exact solution.</p>
  
  <p>Expected XML after filtering should look like:</p>
  
  <pre><code>&lt;foo name="fooName"&gt;
      &lt;bar name="barName"&gt;
          &lt;id&gt;1111&lt;/id&gt;
          &lt;validity&gt;
              &lt;date&gt;20170922&lt;/date&gt;
          &lt;/validity&gt;
      &lt;/bar&gt;
      &lt;bar name="barName"&gt;
          &lt;id&gt;2222&lt;/id&gt;
          &lt;validity&gt;
              &lt;date&gt;20170923&lt;/date&gt;
          &lt;/validity&gt;
      &lt;/bar&gt;
  &lt;/foo&gt;
  </code></pre>