title: Using XSLT to calculate start times of a video playlist
body: <p>I have the following XML representing a list of videos (Titles) and a playlist using these videos (Clock) :</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;myXML&gt;
  &lt;Titles&gt;
      &lt;Title&gt;
          &lt;name&gt;Video1&lt;/name&gt;
          &lt;duration&gt;200&lt;/duration&gt;
      &lt;/Title&gt;
      &lt;Title&gt;
          &lt;name&gt;Video2&lt;/name&gt;
          &lt;duration&gt;300&lt;/duration&gt;
      &lt;/Title&gt;
      &lt;Title&gt;
          &lt;name&gt;Video3&lt;/name&gt;
          &lt;duration&gt;100&lt;/duration&gt;
      &lt;/Title&gt;
  &lt;/Titles&gt;
  &lt;Clock&gt;
      &lt;Video&gt;
          &lt;name&gt;Video3&lt;/name&gt;
      &lt;/Video&gt;
      &lt;Video&gt;
          &lt;name&gt;Video1&lt;/name&gt;
      &lt;/Video&gt;
      &lt;Video&gt;
          &lt;name&gt;Video3&lt;/name&gt;
      &lt;/Video&gt;
      &lt;Video&gt;
          &lt;name&gt;Video2&lt;/name&gt;
      &lt;/Video&gt;
  &lt;/Clock&gt;
  &lt;/myXML&gt;
  </code></pre>
  <p>I want to use XSLT to create a new XML playlist with the same order and the <strong>start time</strong> of each video (assuming the first video in Clock starts at time 0). As you can see, a video can be played several times in the playlist (here Video3 is played twice).</p>
  <p>I tried this :</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
  &lt;xsl:template match=&quot;/&quot;&gt;
  
  &lt;Playlist&gt;
  &lt;xsl:for-each select=&quot;myXML/Clock/Video&quot;&gt;
  &lt;Video&gt;
      &lt;name&gt;&lt;xsl:value-of select=&quot;name&quot;/&gt;&lt;/name&gt;
      &lt;start_time&gt;&lt;xsl:value-of select=&quot;sum(/myXML/Titles/Title[name=current()/preceding-sibling::*/name]/duration)&quot; /&gt;&lt;/start_time&gt;
  &lt;/Video&gt;
  &lt;/xsl:for-each&gt;
  &lt;/Playlist&gt;
  
  &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  <p>The idea would be, for each Video in Clock, to calculate the sum of durations of the preceding videos in Clock, which would be the start time of the Video, using the join operation &quot;[name=current()/preceding-sibling::*/name]&quot;. But it does not work because if a video is played twice in Clocks (or multiple times), it is only counted once in the sum.
  The XML output with this solution is :</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;Playlist&gt;
  &lt;Video&gt;
      &lt;name&gt;Video3&lt;/name&gt;
      &lt;start_time&gt;0&lt;/start_time&gt;
  &lt;/Video&gt;
  &lt;Video&gt;
      &lt;name&gt;Video1&lt;/name&gt;
      &lt;start_time&gt;100&lt;/start_time&gt;&lt;/Video&gt;
  &lt;Video&gt;
      &lt;name&gt;Video3&lt;/name&gt;
      &lt;start_time&gt;300&lt;/start_time&gt;&lt;/Video&gt;
  &lt;Video&gt;
      &lt;name&gt;Video2&lt;/name&gt;
      &lt;start_time&gt;300&lt;/start_time&gt;&lt;/Video&gt;
  &lt;/Playlist&gt;
  </code></pre>
  <p>while Video2's start_time should be 400.</p>
  <p>I tried to manipulate XSLT but did not find a satisfying solution. I feel stuck because of the lack of mutable variables in XSLT. Do you have any idea how to achieve this ? Any help is welcome, thanks.</p>