title: How to loop X times using XSLT 2,0?
body: <p>I have following requirement where I need your help to transform the XML using XSLT 2.0. </p>
  
  <ol>
  <li><code>&lt;Workers&gt;</code> node for a single worker need to be inserted as many time as total number of <code>&lt;Days&gt;</code> for a worker e.g. if total number of days for a worker is 5, <code>&lt;Workers&gt;&lt;Worker&gt;</code> need to be present 5 times holding the data for that worker. </li>
  <li><p>Each time new nodes are inserted for a worker, <code>&lt;StartDate&gt;</code> need to be incremented by a day and map it's value to new element <code>WorkerStartDate</code></p></li>
  <li><p>Each time new nodes are created, a new element <code>&lt;RecordNumber&gt;</code> needs to be inserted holding the value of that loop. </p></li>
  </ol>
  
  <p>Input XML</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Workers&gt;
      &lt;Worker&gt;
          &lt;id&gt;1234&lt;/id&gt;
          &lt;loc&gt;New York&lt;/loc&gt;
          &lt;Days&gt;5&lt;/Days&gt;
          &lt;StartDate&gt;2019-02-01&lt;/StartDate&gt;
      &lt;/Worker&gt;
  &lt;/Workers&gt;
  </code></pre>
  
  <p>Expected Output XML after XSLT transformation </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Workers&gt;
      &lt;Worker&gt;
          &lt;WorkerId&gt;1234&lt;/WorkerId&gt;
          &lt;WorkerLoc&gt;New York&lt;/WorkerLoc&gt;
          &lt;RecordNumber&gt;1&lt;/RecordNumber&gt;
          &lt;WorkerDays&gt;1&lt;/WorkerDays&gt;
          &lt;WorkerStartDate&gt;2019-02-02&lt;/WorkerStartDate&gt;
      &lt;/Worker&gt;
  &lt;/Workers&gt;
  &lt;Workers&gt;
      &lt;Worker&gt;
          &lt;WorkerId&gt;1234&lt;/WorkerId&gt;
          &lt;WorkerLoc&gt;New York&lt;/WorkerLoc&gt;
          &lt;RecordNumber&gt;2&lt;/RecordNumber&gt;
          &lt;WorkerDays&gt;1&lt;/WorkerDays&gt;
          &lt;WorkerStartDate&gt;2019-02-03&lt;/WorkerStartDate&gt;
      &lt;/Worker&gt;
  &lt;/Workers&gt;
  &lt;Workers&gt;
      &lt;Worker&gt;
          &lt;WorkerId&gt;1234&lt;/WorkerId&gt;
          &lt;WorkerLoc&gt;New York&lt;/WorkerLoc&gt;
          &lt;RecordNumber&gt;3&lt;/RecordNumber&gt;
          &lt;WorkerDays&gt;1&lt;/WorkerDays&gt;
          &lt;WorkerStartDate&gt;2019-02-04&lt;/WorkerStartDate&gt;
      &lt;/Worker&gt;
  &lt;/Workers&gt;
  &lt;Workers&gt;
      &lt;Worker&gt;
          &lt;WorkerId&gt;1234&lt;/WorkerId&gt;
          &lt;WorkerLoc&gt;New York&lt;/WorkerLoc&gt;
          &lt;RecordNumber&gt;4&lt;/RecordNumber&gt;
          &lt;WorkerDays&gt;1&lt;/WorkerDays&gt;
          &lt;WorkerStartDate&gt;2019-02-05&lt;/WorkerStartDate&gt;
      &lt;/Worker&gt;
  &lt;/Workers&gt;
  &lt;Workers&gt;
      &lt;Worker&gt;
          &lt;WorkerId&gt;1234&lt;/WorkerId&gt;
          &lt;WorkerLoc&gt;New York&lt;/WorkerLoc&gt;
          &lt;RecordNumber&gt;5&lt;/RecordNumber&gt;
          &lt;WorkerDays&gt;1&lt;/WorkerDays&gt;
          &lt;WorkerStartDate&gt;2019-02-06&lt;/WorkerStartDate&gt;
      &lt;/Worker&gt;
  &lt;/Workers&gt;
  
  </code></pre>
  
  <p>I’m able to get following output <code>&lt;RecordNumber&gt;</code> and <code>&lt;WorkerStartDate&gt;</code> are returning wrong data</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Workers&gt;
      &lt;Worker&gt;
          &lt;WorkerId&gt;1234&lt;/WorkerId&gt;
          &lt;WorkerLoc&gt;New York&lt;/WorkerLoc&gt;
          &lt;RecordNumber&gt;1&lt;/RecordNumber&gt;
          &lt;WorkerDays&gt;1&lt;/WorkerDays&gt;
          &lt;WorkerStartDate&gt;2019-02-02&lt;/WorkerStartDate&gt;
      &lt;/Worker&gt;
  &lt;/Workers&gt;
  &lt;Workers&gt;
      &lt;Worker&gt;
          &lt;WorkerId&gt;1234&lt;/WorkerId&gt;
          &lt;WorkerLoc&gt;New York&lt;/WorkerLoc&gt;
          &lt;RecordNumber&gt;1&lt;/RecordNumber&gt;
          &lt;WorkerDays&gt;1&lt;/WorkerDays&gt;
          &lt;WorkerStartDate&gt;2019-02-02&lt;/WorkerStartDate&gt;
      &lt;/Worker&gt;
  &lt;/Workers&gt;
  &lt;Workers&gt;
      &lt;Worker&gt;
          &lt;WorkerId&gt;1234&lt;/WorkerId&gt;
          &lt;WorkerLoc&gt;New York&lt;/WorkerLoc&gt;
          &lt;RecordNumber&gt;1&lt;/RecordNumber&gt;
          &lt;WorkerDays&gt;1&lt;/WorkerDays&gt;
          &lt;WorkerStartDate&gt;2019-02-02&lt;/WorkerStartDate&gt;
      &lt;/Worker&gt;
  &lt;/Workers&gt;
  &lt;Workers&gt;
      &lt;Worker&gt;
          &lt;WorkerId&gt;1234&lt;/WorkerId&gt;
          &lt;WorkerLoc&gt;New York&lt;/WorkerLoc&gt;
          &lt;RecordNumber&gt;1&lt;/RecordNumber&gt;
          &lt;WorkerDays&gt;1&lt;/WorkerDays&gt;
          &lt;WorkerStartDate&gt;2019-02-02&lt;/WorkerStartDate&gt;
      &lt;/Worker&gt;
  &lt;/Workers&gt;
  &lt;Workers&gt;
      &lt;Worker&gt;
          &lt;WorkerId&gt;1234&lt;/WorkerId&gt;
          &lt;WorkerLoc&gt;New York&lt;/WorkerLoc&gt;
          &lt;RecordNumber&gt;1&lt;/RecordNumber&gt;
          &lt;WorkerDays&gt;1&lt;/WorkerDays&gt;
          &lt;WorkerStartDate&gt;2019-02-02&lt;/WorkerStartDate&gt;
      &lt;/Worker&gt;
  &lt;/Workers&gt;
  
  </code></pre>
  
  <p>My XSLT</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:functx="http://www.functx.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="#all"&gt;
      &lt;xsl:output method="xml" omit-xml-declaration="no" indent="yes"/&gt;
  
      &lt;xsl:template match="/"&gt;
          &lt;xsl:variable name="start" select="1"/&gt;
          &lt;xsl:variable name="counter" select="Workers/Worker/Days"/&gt;
          &lt;xsl:variable name="Records" select="Workers/Worker"/&gt;
          &lt;xsl:for-each select="$start to $counter"&gt;
              &lt;xsl:apply-templates select="$Records" mode="replicate"&gt;
                  &lt;xsl:with-param name="data" select="."/&gt;
              &lt;/xsl:apply-templates&gt;
          &lt;/xsl:for-each&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match="Worker" mode="replicate"&gt;
          &lt;xsl:param name="data"/&gt;
          &lt;Workers&gt;
              &lt;Worker&gt;
                  &lt;WorkerId&gt;&lt;xsl:value-of select="id"/&gt;&lt;/WorkerId&gt;
                  &lt;WorkerLoc&gt;&lt;xsl:value-of select="loc"/&gt;&lt;/WorkerLoc&gt;
                  &lt;RecordNumber&gt;&lt;xsl:value-of select="position()"/&gt;&lt;/RecordNumber&gt;
                  &lt;WorkerDays&gt;1&lt;/WorkerDays&gt;
                  &lt;WorkerStartDate&gt;&lt;xsl:value-of select="xs:date(StartDate) + xs:dayTimeDuration('P1D')"/&gt;
                  &lt;/WorkerStartDate&gt;
              &lt;/Worker&gt;
          &lt;/Workers&gt;
      &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  
  </code></pre>
  
  <p>Current output XML has following issues: </p>
  
  <ol>
  <li><code>&lt;WorkerStartDate&gt;</code> always returned as 1 where as WorkerStartDate is expected to increment by a day based on the XML element </li>
  <li><code>&lt;RecordNumber&gt;</code> always returned as 1 where as RecordNumber is expected to increment by one such as 1,2,3..</li>
  <li>Not a big issue - <code>&lt;WorkerDays&gt;</code> needs to always return 1. Now that I hard coded this value. Not sure if there is an efficient way to print 1 as value until the loop ends. </li>
  </ol>
  
  <p>Can someone help me fix the issues that I have please?</p>