title: XSLT 2.0: Evaluate and modify element value according to current index of for-each -group
body: <p>I am trying to evaluate EndDate as next &quot;StartDate -1&quot; (a day before next start date). Given below is the input XML:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;queryCompoundEmployeeResponse&gt;
     &lt;CompoundEmployee&gt;
           &lt;id&gt;176&lt;/id&gt;
           &lt;person&gt;
                 &lt;action&gt;NO CHANGE&lt;/action&gt;
                 &lt;person_id_external&gt;10005005&lt;/person_id_external&gt;
                 &lt;personal_information&gt;
                       &lt;end_date&gt;9999-12-31&lt;/end_date&gt;
                       &lt;start_date&gt;2021-06-03&lt;/start_date&gt;
                 &lt;/personal_information&gt;
                 &lt;personal_information&gt;
                       &lt;end_date&gt;2021-06-02&lt;/end_date&gt;
                       &lt;start_date&gt;2017-12-06&lt;/start_date&gt;
                 &lt;/personal_information&gt;
                 &lt;phone_information&gt;
                       &lt;phone_type&gt;B&lt;/phone_type&gt;
                 &lt;/phone_information&gt;
                 &lt;phone_information&gt;
                       &lt;phone_number&gt;7CAED430A494B3C404&lt;/phone_number&gt;
                 &lt;/phone_information&gt;
                 &lt;email_information&gt;
                       &lt;last_modified_on&gt;2019-03-25T02:44:51.000Z&lt;/last_modified_on&gt;
                 &lt;/email_information&gt;
                 &lt;employment_information&gt;
                       &lt;start_date&gt;2017-12-06&lt;/start_date&gt;
                       &lt;user_id&gt;10005005&lt;/user_id&gt;
                       &lt;job_information&gt;
                             &lt;end_date&gt;9999-12-31&lt;/end_date&gt;
                             &lt;start_date&gt;2019-03-02&lt;/start_date&gt;
                          &lt;/job_information&gt;
                 &lt;/employment_information&gt;
           &lt;/person&gt;
           &lt;execution_timestamp&gt;2021-07-14T15:08:38.000Z&lt;/execution_timestamp&gt;
           &lt;version_id&gt;2105P0&lt;/version_id&gt;
        &lt;Start_Dates&gt;
           &lt;StartDate&gt;2017-12-06&lt;/StartDate&gt;
           &lt;StartDate&gt;2017-12-06&lt;/StartDate&gt;
           &lt;StartDate&gt;2019-03-02&lt;/StartDate&gt;
           &lt;StartDate&gt;2021-06-03&lt;/StartDate&gt;
        &lt;/Start_Dates&gt;
        &lt;End_Dates&gt;
           &lt;EndDate&gt;2021-06-02&lt;/EndDate&gt;
           &lt;EndDate&gt;NA&lt;/EndDate&gt;
           &lt;EndDate&gt;9999-12-31&lt;/EndDate&gt;
           &lt;EndDate&gt;9999-12-31&lt;/EndDate&gt;
        &lt;/End_Dates&gt;
     &lt;/CompoundEmployee&gt;
     &lt;/queryCompoundEmployeeResponse&gt;
  </code></pre>
  <p>Current Output:</p>
  <pre><code>    &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;queryCompoundEmployeeResponse xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;&gt;
     &lt;CompoundEmployee&gt;
        &lt;Person&gt;
           &lt;StartDate&gt;2017-12-06&lt;/StartDate&gt;
           &lt;EndDate&gt;2019-03-01&lt;/EndDate&gt;
        &lt;/Person&gt;
        &lt;Person&gt;
           &lt;StartDate&gt;2019-03-02&lt;/StartDate&gt;
           &lt;EndDate/&gt;
        &lt;/Person&gt;
        &lt;Person&gt;
           &lt;StartDate&gt;2021-06-03&lt;/StartDate&gt;
           &lt;EndDate&gt;9999-12-31&lt;/EndDate&gt;
        &lt;/Person&gt;
     &lt;/CompoundEmployee&gt;
  &lt;/queryCompoundEmployeeResponse&gt;
  </code></pre>
  <p>Required Output:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;queryCompoundEmployeeResponse xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;&gt;
     &lt;CompoundEmployee&gt;
        &lt;Person&gt;
           &lt;StartDate&gt;2017-12-06&lt;/StartDate&gt;
           &lt;EndDate&gt;2019-03-01&lt;/EndDate&gt;
        &lt;/Person&gt;
        &lt;Person&gt;
           &lt;StartDate&gt;2019-03-02&lt;/StartDate&gt;
           &lt;EndDate&gt;2021-06-02&lt;/EndDate&gt;
        &lt;/Person&gt;
        &lt;Person&gt;
           &lt;StartDate&gt;2021-06-03&lt;/StartDate&gt;
           &lt;EndDate&gt;9999-12-31&lt;/EndDate&gt;
        &lt;/Person&gt;
     &lt;/CompoundEmployee&gt;
  &lt;/queryCompoundEmployeeResponse&gt;
  </code></pre>
  <p>I am trying to calculate the EndDate as a day minus next StartDate for each Person node. For last Person, the EndDate should be the last EndDate in the input XML.</p>
  <p>Here is the code that I am trying to enhance:</p>
  <p>&lt;xsl:stylesheet version=&quot;2.0&quot;
  xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;&gt;
  &lt;xsl:output method=&quot;xml&quot; version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; indent=&quot;yes&quot;/&gt;</p>
  
  <pre><code>&lt;xsl:template match=&quot;/queryCompoundEmployeeResponse&quot;&gt;
      &lt;queryCompoundEmployeeResponse&gt;
          &lt;xsl:for-each select=&quot;CompoundEmployee&quot;&gt;
              &lt;xsl:copy&gt;
                  &lt;xsl:variable name=&quot;person&quot; select=&quot;person&quot; /&gt;
                  &lt;xsl:for-each-group select=&quot;Start_Dates/StartDate&quot; group-by=&quot;.&quot;&gt;
                      &lt;Person&gt;
                         &lt;xsl:copy-of select=&quot;.&quot;/&gt;
                         
                          &lt;!--Start of EndDate logic for last record--&gt;
                          &lt;xsl:variable name=&quot;nxtStartDate&quot; select=&quot;following-sibling::StartDate&quot;/&gt;
                             &lt;xsl:if test=&quot;not($nxtStartDate)&quot;&gt;
                               &lt;xsl:variable name=&quot;i&quot; select=&quot;position()&quot;/&gt;
                                  &lt;EndDate&gt;
                                      &lt;xsl:value-of select=&quot;../following-sibling::End_Dates/EndDate[last()]&quot;/&gt;   
                                  &lt;/EndDate&gt;
                             &lt;/xsl:if&gt;
                            &lt;!-- End of EndDate logic for last record--&gt;  
                            
                            &lt;!--Calculate next start date -1 --&gt;
                              &lt;xsl:if test=&quot;$nxtStartDate&quot;&gt;
                              &lt;xsl:variable name=&quot;currentDate&quot; select=&quot;Start_Dates/StartDate&quot;/&gt;
                              &lt;xsl:variable name=&quot;i&quot; select=&quot;position()&quot;/&gt;
                              &lt;EndDate&gt;
                                  &lt;xsl:apply-templates select=&quot;following-sibling::StartDate[$i+1]&quot;/&gt;
                              &lt;/EndDate&gt;
                              &lt;/xsl:if&gt;
                        &lt;!--Calculate next start date -1 --&gt;
                      
                       &lt;!-- //Some additional required code://
                       
                         &lt;xsl:copy-of select=&quot;$person/* except $person/(personal_information | phone_information | email_information | employment_information)&quot;/&gt;
                         &lt;xsl:copy-of select=&quot;$person/personal_information[start_date le current() and current() le end_date]&quot;/&gt;
                         &lt;xsl:copy-of select=&quot;$person/employment_information[start_date le current() and current() le end_date]&quot;/&gt;
                          &lt;xsl:copy-of select=&quot;$person/employment_information/job_information[start_date le current() and current() le end_date]&quot;/&gt; 
                       --&gt;
                      &lt;/Person&gt;
                  &lt;/xsl:for-each-group&gt;
              &lt;/xsl:copy&gt;
          &lt;/xsl:for-each&gt;
      &lt;/queryCompoundEmployeeResponse&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match=&quot;StartDate&quot;&gt;
   &lt;xsl:variable name=&quot;sDate&quot; select=&quot;.&quot; as=&quot;xs:date&quot;/&gt;
          &lt;xsl:copy-of select=&quot;$sDate - 1*xs:dayTimeDuration('P1D')&quot;/&gt;
  &lt;/xsl:template&gt;
      
  &lt;/xsl:stylesheet&gt;
  
  
  
   I need something like following-sibling::(.)[index+1]. It gives me the End date in first Person. But next one is still empty. 
  </code></pre>
  <p>What am I doing wrong?
  Also, how can I modify this to have separate templates for each part?</p>
  <p>Thanks in advance!</p>