I am using below XSLT to re-arrange/collect various nodes in an XML as per dates they are valid on.

Here is the XSLT:

```
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
    <xsl:output method="xml" encoding="UTF-8" indent="yes" />

    <xsl:template match="/">
        <CompoundEmployee>
        <xsl:variable name="var_person" select="//person/*[not(name()='personal_information') and not(name()='phone_information') and not(name()='email_information') and not(name()='employment_information')]"></xsl:variable>
        <xsl:for-each select="//StartDate">
         <xsl:variable name="i" select="position()"/>
         <xsl:variable name="newDate" select="replace(//StartDate[$i],'-','')"/>
          <Record>
              <xsl:copy-of select="//StartDate[$i]"/>
              <xsl:copy-of select="$var_person"/>
              <xsl:copy-of select="//CompoundEmployee/person/personal_information[replace(start_date,'-','') &lt;= $newDate and $newDate &lt;= replace(end_date,'-','')]"/>
          <xsl:copy-of select="//CompoundEmployee/person/employment_information/job_information[replace(start_date,'-','') &lt;= $newDate and $newDate &lt;= replace(end_date,'-','')]"/>
         <xsl:copy-of select="//CompoundEmployee/person/employment_information[(replace(start_date,'-','') &lt;= $newDate and ($newDate &lt;= replace(end_date,'-','') or not(end_date)))]"/>
          </Record>
        </xsl:for-each>
        </CompoundEmployee>
    </xsl:template>
</xsl:stylesheet>

```

This is working fine as long as there is only one CompoundEmployee node. But I could get multiple CompoundEmployee nodes within the root queryCompoundEmployeeResponse. Here is the input XML:

```
    <?xml version="1.0" encoding="UTF-8"?>
<queryCompoundEmployeeResponse>
<CompoundEmployee>
    <id>176</id>
    <person>
        <action>NO CHANGE</action>
        <created_by>CONV_ADMIN</created_by>
        <logon_user_id>1234567</logon_user_id>
        <logon_user_is_active>true</logon_user_is_active>
        <person_id>176</person_id>
        <person_id_external>1234567</person_id_external>
        <personal_information>
            <end_date>9999-12-31</end_date>
            <first_name>yutaka</first_name>
            <first_name_previous>Robbin</first_name_previous>
            <first_name_alt1>Robbin</first_name_alt1>
            <start_date>2021-06-03</start_date>
        </personal_information>
        <personal_information>
            <end_date>2021-06-02</end_date>
            <first_name>wataru</first_name>
            <first_name_previous>Robbin</first_name_previous>
            <first_name_alt1>Robbin</first_name_alt1>
            <start_date>2017-12-06</start_date>
        </personal_information>
        <employment_information>
            <employment_id>136</employment_id>
            <start_date>2017-12-06</start_date>
            <user_id>10005005</user_id>
            <job_information>
                <action>NO CHANGE</action>
                <end_date>9999-12-31</end_date>
                <entry_into_group>2017-12-06</entry_into_group>
                <event>5</event>
                <event_reason>DATACONV</event_reason>
                <excl_executive_sector>false</excl_executive_sector>
                <fte>1.0</fte>
                <hazard>false</hazard>
                <job_code>1000039</job_code>
                <location>10000069</location>
                <manager_employment_id>265</manager_employment_id>
                <manager_id>10005069</manager_id>
                <manager_person_id>305</manager_person_id>
                <manager_person_id_external>10005069</manager_person_id_external>
                <start_date>2019-03-02</start_date>
            </job_information>
        </employment_information>
    </person>
    <StartDates>
    <StartDate>2021-06-03</StartDate>
    <StartDate>2017-12-06</StartDate>
    <StartDate>2017-12-06</StartDate>
    <StartDate>2019-03-02</StartDate>
    </StartDates>
</CompoundEmployee>
<CompoundEmployee>
    <id>176</id>
    <person>
        <action>NO CHANGE</action>
        <created_by>CONV_ADMIN</created_by>
        <logon_user_id>1234567</logon_user_id>
        <logon_user_is_active>true</logon_user_is_active>
        <person_id>176</person_id>
        <person_id_external>1234567</person_id_external>
        <personal_information>
            <end_date>9999-12-31</end_date>
            <first_name>yutaka</first_name>
            <first_name_previous>Robbin</first_name_previous>
            <first_name_alt1>Robbin</first_name_alt1>
            <start_date>2021-06-03</start_date>
        </personal_information>
        <personal_information>
            <end_date>2021-06-02</end_date>
            <first_name>wataru</first_name>
            <first_name_previous>Robbin</first_name_previous>
            <first_name_alt1>Robbin</first_name_alt1>
            <start_date>2017-12-06</start_date>
        </personal_information>
        <employment_information>
            <employment_id>136</employment_id>
            <start_date>2017-12-06</start_date>
            <user_id>10005005</user_id>
            <job_information>
                <action>NO CHANGE</action>
                <end_date>9999-12-31</end_date>
                <entry_into_group>2017-12-06</entry_into_group>
                <event>5</event>
                <event_reason>DATACONV</event_reason>
                <excl_executive_sector>false</excl_executive_sector>
                <fte>1.0</fte>
                <hazard>false</hazard>
                <job_code>1000039</job_code>
                <location>10000069</location>
                <manager_employment_id>265</manager_employment_id>
                <manager_id>10005069</manager_id>
                <manager_person_id>305</manager_person_id>
                <manager_person_id_external>10005069</manager_person_id_external>
                <start_date>2019-03-02</start_date>
            </job_information>
        </employment_information>
    </person>
    <StartDates>
    <StartDate>2021-06-01</StartDate>
    <StartDate>2017-12-05</StartDate>
    <StartDate>2017-12-07</StartDate>
    <StartDate>2019-03-08</StartDate>
    </StartDates>
</CompoundEmployee>
</queryCompoundEmployeeResponse>

```

Error:
Error executing XSLT at line 12 : A sequence of more than one item is not allowed as the first argument of fn:replace() ("2021-06-03", "2021-06-03")

Expected XML

```
<?xml version="1.0" encoding="UTF-8"?>
<queryCompoundEmployeeResponse>
<CompoundEmployee>
   <Record>
      <StartDate>2021-06-03</StartDate>
      <action>NO CHANGE</action>
      <created_by>CONV_ADMIN</created_by>
      <logon_user_id>1234567</logon_user_id>
      <logon_user_is_active>true</logon_user_is_active>
      <person_id>176</person_id>
      <person_id_external>1234567</person_id_external>
      <personal_information>
            <end_date>9999-12-31</end_date>
            <first_name>yutaka</first_name>
            <first_name_previous>Robbin</first_name_previous>
            <first_name_alt1>Robbin</first_name_alt1>
            <start_date>2021-06-03</start_date>
        </personal_information>
      <job_information>
                <action>NO CHANGE</action>
                <end_date>9999-12-31</end_date>
                <entry_into_group>2017-12-06</entry_into_group>
                <event>5</event>
                <event_reason>DATACONV</event_reason>
                <excl_executive_sector>false</excl_executive_sector>
                <fte>1.0</fte>
                <hazard>false</hazard>
                <job_code>1000039</job_code>
                <location>10000069</location>
                <manager_employment_id>265</manager_employment_id>
                <manager_id>10005069</manager_id>
                <manager_person_id>305</manager_person_id>
                <manager_person_id_external>10005069</manager_person_id_external>
                <start_date>2019-03-02</start_date>
            </job_information>
      <employment_information>
            <employment_id>136</employment_id>
            <start_date>2017-12-06</start_date>
            <user_id>10005005</user_id>
            <job_information>
                <action>NO CHANGE</action>
                <end_date>9999-12-31</end_date>
                <entry_into_group>2017-12-06</entry_into_group>
                <event>5</event>
                <event_reason>DATACONV</event_reason>
                <excl_executive_sector>false</excl_executive_sector>
                <fte>1.0</fte>
                <hazard>false</hazard>
                <job_code>1000039</job_code>
                <location>10000069</location>
                <manager_employment_id>265</manager_employment_id>
                <manager_id>10005069</manager_id>
                <manager_person_id>305</manager_person_id>
                <manager_person_id_external>10005069</manager_person_id_external>
                <start_date>2019-03-02</start_date>
            </job_information>
        </employment_information>
   </Record>
   <Record>
      <StartDate>2017-12-06</StartDate>
      <action>NO CHANGE</action>
      <created_by>CONV_ADMIN</created_by>
      <logon_user_id>1234567</logon_user_id>
      <logon_user_is_active>true</logon_user_is_active>
      <person_id>176</person_id>
      <person_id_external>1234567</person_id_external>
      <personal_information>
            <end_date>2021-06-02</end_date>
            <first_name>wataru</first_name>
            <first_name_previous>Robbin</first_name_previous>
            <first_name_alt1>Robbin</first_name_alt1>
            <start_date>2017-12-06</start_date>
        </personal_information>
      <employment_information>
            <employment_id>136</employment_id>
            <start_date>2017-12-06</start_date>
            <user_id>10005005</user_id>
            <job_information>
                <action>NO CHANGE</action>
                <end_date>9999-12-31</end_date>
                <entry_into_group>2017-12-06</entry_into_group>
                <event>5</event>
                <event_reason>DATACONV</event_reason>
                <excl_executive_sector>false</excl_executive_sector>
                <fte>1.0</fte>
                <hazard>false</hazard>
                <job_code>1000039</job_code>
                <location>10000069</location>
                <manager_employment_id>265</manager_employment_id>
                <manager_id>10005069</manager_id>
                <manager_person_id>305</manager_person_id>
                <manager_person_id_external>10005069</manager_person_id_external>
                <start_date>2019-03-02</start_date>
            </job_information>
        </employment_information>
   </Record>
   <Record>
      <StartDate>2017-12-06</StartDate>
      <action>NO CHANGE</action>
      <created_by>CONV_ADMIN</created_by>
      <logon_user_id>1234567</logon_user_id>
      <logon_user_is_active>true</logon_user_is_active>
      <person_id>176</person_id>
      <person_id_external>1234567</person_id_external>
      <personal_information>
            <end_date>2021-06-02</end_date>
            <first_name>wataru</first_name>
            <first_name_previous>Robbin</first_name_previous>
            <first_name_alt1>Robbin</first_name_alt1>
            <start_date>2017-12-06</start_date>
        </personal_information>
      <employment_information>
            <employment_id>136</employment_id>
            <start_date>2017-12-06</start_date>
            <user_id>10005005</user_id>
            <job_information>
                <action>NO CHANGE</action>
                <end_date>9999-12-31</end_date>
                <entry_into_group>2017-12-06</entry_into_group>
                <event>5</event>
                <event_reason>DATACONV</event_reason>
                <excl_executive_sector>false</excl_executive_sector>
                <fte>1.0</fte>
                <hazard>false</hazard>
                <job_code>1000039</job_code>
                <location>10000069</location>
                <manager_employment_id>265</manager_employment_id>
                <manager_id>10005069</manager_id>
                <manager_person_id>305</manager_person_id>
                <manager_person_id_external>10005069</manager_person_id_external>
                <start_date>2019-03-02</start_date>
            </job_information>
        </employment_information>
   </Record>
   <Record>
      <StartDate>2019-03-02</StartDate>
      <action>NO CHANGE</action>
      <created_by>CONV_ADMIN</created_by>
      <logon_user_id>1234567</logon_user_id>
      <logon_user_is_active>true</logon_user_is_active>
      <person_id>176</person_id>
      <person_id_external>1234567</person_id_external>
      <personal_information>
            <end_date>2021-06-02</end_date>
            <first_name>wataru</first_name>
            <first_name_previous>Robbin</first_name_previous>
            <first_name_alt1>Robbin</first_name_alt1>
            <start_date>2017-12-06</start_date>
        </personal_information>
      <job_information>
                <action>NO CHANGE</action>
                <end_date>9999-12-31</end_date>
                <entry_into_group>2017-12-06</entry_into_group>
                <event>5</event>
                <event_reason>DATACONV</event_reason>
                <excl_executive_sector>false</excl_executive_sector>
                <fte>1.0</fte>
                <hazard>false</hazard>
                <job_code>1000039</job_code>
                <location>10000069</location>
                <manager_employment_id>265</manager_employment_id>
                <manager_id>10005069</manager_id>
                <manager_person_id>305</manager_person_id>
                <manager_person_id_external>10005069</manager_person_id_external>
                <start_date>2019-03-02</start_date>
            </job_information>
      <employment_information>
            <employment_id>136</employment_id>
            <start_date>2017-12-06</start_date>
            <user_id>10005005</user_id>
            <job_information>
                <action>NO CHANGE</action>
                <end_date>9999-12-31</end_date>
                <entry_into_group>2017-12-06</entry_into_group>
                <event>5</event>
                <event_reason>DATACONV</event_reason>
                <excl_executive_sector>false</excl_executive_sector>
                <fte>1.0</fte>
                <hazard>false</hazard>
                <job_code>1000039</job_code>
                <location>10000069</location>
                <manager_employment_id>265</manager_employment_id>
                <manager_id>10005069</manager_id>
                <manager_person_id>305</manager_person_id>
                <manager_person_id_external>10005069</manager_person_id_external>
                <start_date>2019-03-02</start_date>
            </job_information>
        </employment_information>
   </Record>
</CompoundEmployee>
<CompoundEmployee>
   <Record>
      <StartDate>2021-06-01</StartDate>
      <action>NO CHANGE</action>
      <created_by>CONV_ADMIN</created_by>
      <logon_user_id>1234567</logon_user_id>
      <logon_user_is_active>true</logon_user_is_active>
      <person_id>176</person_id>
      <person_id_external>1234567</person_id_external>
      <personal_information>
            <end_date>9999-12-31</end_date>
            <first_name>yutaka</first_name>
            <first_name_previous>Robbin</first_name_previous>
            <first_name_alt1>Robbin</first_name_alt1>
            <start_date>2021-06-01</start_date>
        </personal_information>
      <personal_information>
            <end_date>2021-06-02</end_date>
            <first_name>wataru</first_name>
            <first_name_previous>Robbin</first_name_previous>
            <first_name_alt1>Robbin</first_name_alt1>
            <start_date>2017-12-05</start_date>
        </personal_information>
      <job_information>
                <action>NO CHANGE</action>
                <end_date>9999-12-31</end_date>
                <entry_into_group>2017-12-06</entry_into_group>
                <event>5</event>
                <event_reason>DATACONV</event_reason>
                <excl_executive_sector>false</excl_executive_sector>
                <fte>1.0</fte>
                <hazard>false</hazard>
                <job_code>1000039</job_code>
                <location>10000069</location>
                <manager_employment_id>265</manager_employment_id>
                <manager_id>10005069</manager_id>
                <manager_person_id>305</manager_person_id>
                <manager_person_id_external>10005069</manager_person_id_external>
                <start_date>2019-03-08</start_date>
            </job_information>
      <employment_information>
            <employment_id>136</employment_id>
            <start_date>2017-12-07</start_date>
            <user_id>10005005</user_id>
            <job_information>
                <action>NO CHANGE</action>
                <end_date>9999-12-31</end_date>
                <entry_into_group>2017-12-06</entry_into_group>
                <event>5</event>
                <event_reason>DATACONV</event_reason>
                <excl_executive_sector>false</excl_executive_sector>
                <fte>1.0</fte>
                <hazard>false</hazard>
                <job_code>1000039</job_code>
                <location>10000069</location>
                <manager_employment_id>265</manager_employment_id>
                <manager_id>10005069</manager_id>
                <manager_person_id>305</manager_person_id>
                <manager_person_id_external>10005069</manager_person_id_external>
                <start_date>2019-03-08</start_date>
            </job_information>
        </employment_information>
   </Record>
   <Record>
      <StartDate>2017-12-05</StartDate>
      <action>NO CHANGE</action>
      <created_by>CONV_ADMIN</created_by>
      <logon_user_id>1234567</logon_user_id>
      <logon_user_is_active>true</logon_user_is_active>
      <person_id>176</person_id>
      <person_id_external>1234567</person_id_external>
      <personal_information>
            <end_date>2021-06-02</end_date>
            <first_name>wataru</first_name>
            <first_name_previous>Robbin</first_name_previous>
            <first_name_alt1>Robbin</first_name_alt1>
            <start_date>2017-12-05</start_date>
        </personal_information>
   </Record>
   <Record>
      <StartDate>2017-12-07</StartDate>
      <action>NO CHANGE</action>
      <created_by>CONV_ADMIN</created_by>
      <logon_user_id>1234567</logon_user_id>
      <logon_user_is_active>true</logon_user_is_active>
      <person_id>176</person_id>
      <person_id_external>1234567</person_id_external>
      <personal_information>
            <end_date>2021-06-02</end_date>
            <first_name>wataru</first_name>
            <first_name_previous>Robbin</first_name_previous>
            <first_name_alt1>Robbin</first_name_alt1>
            <start_date>2017-12-05</start_date>
        </personal_information>
      <employment_information>
            <employment_id>136</employment_id>
            <start_date>2017-12-07</start_date>
            <user_id>10005005</user_id>
            <job_information>
                <action>NO CHANGE</action>
                <end_date>9999-12-31</end_date>
                <entry_into_group>2017-12-06</entry_into_group>
                <event>5</event>
                <event_reason>DATACONV</event_reason>
                <excl_executive_sector>false</excl_executive_sector>
                <fte>1.0</fte>
                <hazard>false</hazard>
                <job_code>1000039</job_code>
                <location>10000069</location>
                <manager_employment_id>265</manager_employment_id>
                <manager_id>10005069</manager_id>
                <manager_person_id>305</manager_person_id>
                <manager_person_id_external>10005069</manager_person_id_external>
                <start_date>2019-03-08</start_date>
            </job_information>
        </employment_information>
   </Record>
   <Record>
      <StartDate>2019-03-08</StartDate>
      <action>NO CHANGE</action>
      <created_by>CONV_ADMIN</created_by>
      <logon_user_id>1234567</logon_user_id>
      <logon_user_is_active>true</logon_user_is_active>
      <person_id>176</person_id>
      <person_id_external>1234567</person_id_external>
      <personal_information>
            <end_date>2021-06-02</end_date>
            <first_name>wataru</first_name>
            <first_name_previous>Robbin</first_name_previous>
            <first_name_alt1>Robbin</first_name_alt1>
            <start_date>2017-12-05</start_date>
        </personal_information>
      <job_information>
                <action>NO CHANGE</action>
                <end_date>9999-12-31</end_date>
                <entry_into_group>2017-12-06</entry_into_group>
                <event>5</event>
                <event_reason>DATACONV</event_reason>
                <excl_executive_sector>false</excl_executive_sector>
                <fte>1.0</fte>
                <hazard>false</hazard>
                <job_code>1000039</job_code>
                <location>10000069</location>
                <manager_employment_id>265</manager_employment_id>
                <manager_id>10005069</manager_id>
                <manager_person_id>305</manager_person_id>
                <manager_person_id_external>10005069</manager_person_id_external>
                <start_date>2019-03-08</start_date>
            </job_information>
      <employment_information>
            <employment_id>136</employment_id>
            <start_date>2017-12-07</start_date>
            <user_id>10005005</user_id>
            <job_information>
                <action>NO CHANGE</action>
                <end_date>9999-12-31</end_date>
                <entry_into_group>2017-12-06</entry_into_group>
                <event>5</event>
                <event_reason>DATACONV</event_reason>
                <excl_executive_sector>false</excl_executive_sector>
                <fte>1.0</fte>
                <hazard>false</hazard>
                <job_code>1000039</job_code>
                <location>10000069</location>
                <manager_employment_id>265</manager_employment_id>
                <manager_id>10005069</manager_id>
                <manager_person_id>305</manager_person_id>
                <manager_person_id_external>10005069</manager_person_id_external>
                <start_date>2019-03-08</start_date>
            </job_information>
        </employment_information>
   </Record>
</CompoundEmployee>

```

I need to get this working even with more than one CompoundEmployee nodes. Also, please let me know if there is a better way to do this. I have created this by going through some blogs. Thanks!