title: XSLT 2.0 : picking up XML node by matching one child node and comparing other
body: <p>I have input XML like below:</p>
  <pre><code>&lt;parent&gt;
      &lt;payment&gt;
          &lt;id&gt;123456&lt;/id&gt;
          &lt;type&gt;CANCELLED&lt;/type&gt;
          &lt;date&gt;2020-12-03&lt;/date&gt;
          &lt;amount&gt;100&lt;/amount&gt;
      &lt;/payment&gt;
      &lt;payment&gt;
          &lt;id&gt;234567&lt;/id&gt;
          &lt;type&gt;FORCE&lt;/type&gt;
          &lt;date&gt;2020-12-01&lt;/date&gt;
          &lt;amount&gt;200&lt;/amount&gt;
      &lt;/payment&gt;
      &lt;payment&gt;
          &lt;id&gt;345678&lt;/id&gt;
          &lt;type&gt;CANCELLED&lt;/type&gt;
          &lt;date&gt;2020-12-01&lt;/date&gt;
          &lt;amount&gt;300&lt;/amount&gt;
      &lt;/payment&gt;
      &lt;payment&gt;
          &lt;id&gt;456789&lt;/id&gt;
          &lt;type&gt;FORCE&lt;/type&gt;
          &lt;date&gt;2020-12-01&lt;/date&gt;
          &lt;amount&gt;400&lt;/amount&gt;
      &lt;/payment&gt;
      &lt;payment&gt;
          &lt;id&gt;456788&lt;/id&gt;
          &lt;type&gt;CANCELLED&lt;/type&gt;
          &lt;date&gt;2020-12-01&lt;/date&gt;
          &lt;amount&gt;500&lt;/amount&gt;
      &lt;/payment&gt;
  &lt;/parent&gt;
  </code></pre>
  <p>Now, we want nodes where type = 'CANCELLED' and tricky condition is within these cancelled payments, if date is equal then we need to pick up node with highest id. SO, in above sample input, only 1st and 5th node should be selected. date for first node doesn't match with any other node, so it is selected. While date for 5th node matches with 3rd node but 5th is selected as it has higher id.</p>
  <p>To achieve this, I tried to write below XSLT,</p>
  <pre><code>&lt;xsl:stylesheet version=&quot;2.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
      &lt;xsl:output method=&quot;xml&quot; version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; indent=&quot;yes&quot;/&gt;
      &lt;xsl:strip-space elements=&quot;*&quot;/&gt;
      &lt;xsl:variable name=&quot;input&quot; select=&quot;parent&quot;/&gt;
      &lt;xsl:template match=&quot;/&quot;&gt;
          &lt;xsl:for-each select=&quot;parent/payment[type='CANCELLED']&quot;&gt;
              &lt;xsl:choose&gt;
                  &lt;xsl:when test=&quot;date = $input/payment[type='CANCELLED']/date&quot;&gt;
                      &lt;xsl:message&gt;
                          &lt;xsl:value-of select=&quot;amount&quot;/&gt;
                      &lt;/xsl:message&gt;
                  &lt;/xsl:when&gt;
                  &lt;xsl:otherwise&gt;
                      &lt;xsl:value-of select=&quot;amount&quot;/&gt;
                  &lt;/xsl:otherwise&gt;
              &lt;/xsl:choose&gt;
          &lt;/xsl:for-each&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  <p>For-each is required as in actual example we are extracting some values, so we need to tweak for-each loop condition or write if condition inside it. I tried to declare variable input with whole input xml and compare node which is selected within for-each iteration with input node but then it matches with self as well. Am I going in right direction? Please guide.</p>
  <p>Thanks in advance.</p>