title: XSLT Munchen method and child element of prior XML tag
body: <p>I've been attempting to group data to reduce the output, however when I use Munchen I get close to what I want, but with an expected "bug" in the output. I've tried to come up with a solution, but I have run out of ideas!</p>
  
  <p>Please recognize that the data set here is a <strong>very</strong> simplified look at the actual data in order to be able to post this question. There are about 40 more tags at various levels in the real data so perhaps I'm losing the forest for the trees.</p>
  
  <p>The data contains a set of names and start/end times. Currently if "Jeff" is working from 6-9a his name appears for each half hour of time (6 lines of data). The request is to list "Jeff" as appearing from 6-9a on a single line. I'm using "Jeff" as a test before applying it to the rest of the sheet so I can see what other issues might arise; therefore the test on his name will eventually be removed.</p>
  
  <pre><code>&lt;schedules&gt;
  &lt;ES_schedules name="Jeff"&gt;
      &lt;tx_starttime&gt;
          &lt;ESP_TIMEDURATION hours="6" minutes="00" seconds="00" durationinseconds="21600"/&gt;
      &lt;/tx_starttime&gt;
      &lt;tx_endtime&gt;
          &lt;ESP_TIMEDURATION hours="6" minutes="30" seconds="00"/&gt;
      &lt;/tx_endtime&gt;
      &lt;tx_txdate&gt;
          &lt;ESP_DATE year="2016" dateindays="42063"/&gt;
      &lt;/tx_txdate&gt;
  &lt;/ES_schedules&gt;
  &lt;ES_schedules name="Jeff"&gt;
      &lt;tx_starttime&gt;
          &lt;ESP_TIMEDURATION hours="6" minutes="30" seconds="00" durationinseconds="23400"/&gt;
      &lt;/tx_starttime&gt;
      &lt;tx_endtime&gt;
          &lt;ESP_TIMEDURATION hours="7" minutes="00" seconds="00"/&gt;
      &lt;/tx_endtime&gt;
      &lt;tx_txdate&gt;
          &lt;ESP_DATE year="2016" dateindays="42063"/&gt;
      &lt;/tx_txdate&gt;
  &lt;/ES_schedules&gt;
  &lt;ES_schedules name="Jeff"&gt;
      &lt;tx_starttime&gt;
          &lt;ESP_TIMEDURATION hours="7" minutes="00" seconds="00" durationinseconds="25200"/&gt;
      &lt;/tx_starttime&gt;
      &lt;tx_endtime&gt;
          &lt;ESP_TIMEDURATION hours="7" minutes="30" seconds="00"/&gt;
      &lt;/tx_endtime&gt;
      &lt;tx_txdate&gt;
          &lt;ESP_DATE year="2016" dateindays="42063"/&gt;
      &lt;/tx_txdate&gt;
  &lt;/ES_schedules&gt;
  &lt;ES_schedules name="Jeff"&gt;
      &lt;tx_starttime&gt;
          &lt;ESP_TIMEDURATION hours="7" minutes="30" seconds="00" durationinseconds="27000"/&gt;
      &lt;/tx_starttime&gt;
      &lt;tx_endtime&gt;
          &lt;ESP_TIMEDURATION hours="8" minutes="00" seconds="00"/&gt;
      &lt;/tx_endtime&gt;
      &lt;tx_txdate&gt;
          &lt;ESP_DATE year="2016" dateindays="42063"/&gt;
      &lt;/tx_txdate&gt;
  &lt;/ES_schedules&gt;
  &lt;ES_schedules name="Jeff"&gt;
      &lt;tx_starttime&gt;
          &lt;ESP_TIMEDURATION hours="8" minutes="00" seconds="00" durationinseconds="28800"/&gt;
      &lt;/tx_starttime&gt;
      &lt;tx_endtime&gt;
          &lt;ESP_TIMEDURATION hours="8" minutes="30" seconds="00"/&gt;
      &lt;/tx_endtime&gt;
      &lt;tx_txdate&gt;
          &lt;ESP_DATE year="2016" dateindays="42063"/&gt;
      &lt;/tx_txdate&gt;
  &lt;/ES_schedules&gt;
  &lt;ES_schedules name="Jeff"&gt;
      &lt;tx_starttime&gt;
          &lt;ESP_TIMEDURATION hours="8" minutes="30" seconds="00" durationinseconds="30600"/&gt;
      &lt;/tx_starttime&gt;
      &lt;tx_endtime&gt;
          &lt;ESP_TIMEDURATION hours="9" minutes="00" seconds="00"/&gt;
      &lt;/tx_endtime&gt;
      &lt;tx_txdate&gt;
          &lt;ESP_DATE year="2016" dateindays="42063"/&gt;
      &lt;/tx_txdate&gt;
  &lt;/ES_schedules&gt;
  &lt;ES_schedules name="Rich"&gt;
      &lt;tx_starttime&gt;
          &lt;ESP_TIMEDURATION hours="9" minutes="00" seconds="00" durationinseconds="32400"/&gt;
      &lt;/tx_starttime&gt;
      &lt;tx_endtime&gt;
          &lt;ESP_TIMEDURATION hours="12" minutes="00" seconds="00"/&gt;
      &lt;/tx_endtime&gt;
      &lt;tx_txdate&gt;
          &lt;ESP_DATE year="2016" dateindays="42063"/&gt;
      &lt;/tx_txdate&gt;
  &lt;/ES_schedules&gt;
  &lt;ES_schedules name="Jeff"&gt;
      &lt;tx_starttime&gt;
          &lt;ESP_TIMEDURATION hours="12" minutes="00" seconds="00" durationinseconds="43200"/&gt;
      &lt;/tx_starttime&gt;
      &lt;tx_endtime&gt;
          &lt;ESP_TIMEDURATION hours="12" minutes="30" seconds="00"/&gt;
      &lt;/tx_endtime&gt;
      &lt;tx_txdate&gt;
          &lt;ESP_DATE year="2016" dateindays="42063"/&gt;
      &lt;/tx_txdate&gt;
  &lt;/ES_schedules&gt;
  &lt;ES_schedules name="Jeff"&gt;
      &lt;tx_starttime&gt;
          &lt;ESP_TIMEDURATION hours="12" minutes="30" seconds="00" durationinseconds="45000"/&gt;
      &lt;/tx_starttime&gt;
      &lt;tx_endtime&gt;
          &lt;ESP_TIMEDURATION hours="13" minutes="00" seconds="00"/&gt;
      &lt;/tx_endtime&gt;
      &lt;tx_txdate&gt;
          &lt;ESP_DATE year="2016" dateindays="42063"/&gt;
      &lt;/tx_txdate&gt;
  &lt;/ES_schedules&gt;
  &lt;ES_schedules name="Dan"&gt;
      &lt;tx_starttime&gt;
          &lt;ESP_TIMEDURATION hours="13" minutes="00" seconds="00" durationinseconds="46800"/&gt;
      &lt;/tx_starttime&gt;
      &lt;tx_endtime&gt;
          &lt;ESP_TIMEDURATION hours="16" minutes="00" seconds="00"/&gt;
      &lt;/tx_endtime&gt;
      &lt;tx_txdate&gt;
          &lt;ESP_DATE year="2016" dateindays="42063"/&gt;
      &lt;/tx_txdate&gt;
  &lt;/ES_schedules&gt;
  &lt;ES_schedules name="Shane"&gt;
      &lt;tx_starttime&gt;
          &lt;ESP_TIMEDURATION hours="16" minutes="00" seconds="00" durationinseconds="57600"/&gt;
      &lt;/tx_starttime&gt;
      &lt;tx_endtime&gt;
          &lt;ESP_TIMEDURATION hours="16" minutes="30" seconds="00"/&gt;
      &lt;/tx_endtime&gt;
      &lt;tx_txdate&gt;
          &lt;ESP_DATE year="2016" dateindays="42063"/&gt;
      &lt;/tx_txdate&gt;
  &lt;/ES_schedules&gt;
  &lt;ES_schedules name="Sean"&gt;
      &lt;tx_starttime&gt;
          &lt;ESP_TIMEDURATION hours="16" minutes="30" seconds="00" durationinseconds="59400"/&gt;
      &lt;/tx_starttime&gt;
      &lt;tx_endtime&gt;
          &lt;ESP_TIMEDURATION hours="17" minutes="00" seconds="00"/&gt;
      &lt;/tx_endtime&gt;
      &lt;tx_txdate&gt;
          &lt;ESP_DATE year="2016" dateindays="42063"/&gt;
      &lt;/tx_txdate&gt;
  &lt;/ES_schedules&gt;
  &lt;ES_schedules name="Joe"&gt;
      &lt;tx_starttime&gt;
          &lt;ESP_TIMEDURATION hours="17" minutes="00" seconds="00" durationinseconds="61200"/&gt;
      &lt;/tx_starttime&gt;
      &lt;tx_endtime&gt;
          &lt;ESP_TIMEDURATION hours="17" minutes="30" seconds="00"/&gt;
      &lt;/tx_endtime&gt;
      &lt;tx_txdate&gt;
          &lt;ESP_DATE year="2016" dateindays="42063"/&gt;
      &lt;/tx_txdate&gt;
  &lt;/ES_schedules&gt;
  &lt;ES_schedules name="Mark"&gt;
      &lt;tx_starttime&gt;
          &lt;ESP_TIMEDURATION hours="17" minutes="30" seconds="00" durationinseconds="63000"/&gt;
      &lt;/tx_starttime&gt;
      &lt;tx_endtime&gt;
          &lt;ESP_TIMEDURATION hours="18" minutes="00" seconds="00"/&gt;
      &lt;/tx_endtime&gt;
      &lt;tx_txdate&gt;
          &lt;ESP_DATE year="2016" dateindays="42063"/&gt;
      &lt;/tx_txdate&gt;
  &lt;/ES_schedules&gt;
  &lt;ES_schedules name="Kendra"&gt;
      &lt;tx_starttime&gt;
          &lt;ESP_TIMEDURATION hours="18" minutes="00" seconds="00" durationinseconds="64800"/&gt;
      &lt;/tx_starttime&gt;
      &lt;tx_endtime&gt;
          &lt;ESP_TIMEDURATION hours="20" minutes="00" seconds="00"/&gt;
      &lt;/tx_endtime&gt;
      &lt;tx_txdate&gt;
          &lt;ESP_DATE year="2016" dateindays="42063"/&gt;
      &lt;/tx_txdate&gt;
  &lt;/ES_schedules&gt;
  &lt;ES_schedules name="Mark"&gt;
      &lt;tx_starttime&gt;
          &lt;ESP_TIMEDURATION hours="20" minutes="00" seconds="00" durationinseconds="72000"/&gt;
      &lt;/tx_starttime&gt;
      &lt;tx_endtime&gt;
          &lt;ESP_TIMEDURATION hours="20" minutes="30" seconds="00"/&gt;
      &lt;/tx_endtime&gt;
      &lt;tx_txdate&gt;
          &lt;ESP_DATE year="2016" dateindays="42063"/&gt;
      &lt;/tx_txdate&gt;
  &lt;/ES_schedules&gt;
  &lt;ES_schedules name="Crystal"&gt;
      &lt;tx_starttime&gt;
          &lt;ESP_TIMEDURATION hours="20" minutes="30" seconds="00" durationinseconds="73800"/&gt;
      &lt;/tx_starttime&gt;
      &lt;tx_endtime&gt;
          &lt;ESP_TIMEDURATION hours="22" minutes="30" seconds="00"/&gt;
      &lt;/tx_endtime&gt;
      &lt;tx_txdate&gt;
          &lt;ESP_DATE year="2016" dateindays="42063"/&gt;
      &lt;/tx_txdate&gt;
  &lt;/ES_schedules&gt;
  &lt;ES_schedules name="Matthew"&gt;
      &lt;tx_starttime&gt;
          &lt;ESP_TIMEDURATION hours="22" minutes="30" seconds="00" durationinseconds="81000"/&gt;
      &lt;/tx_starttime&gt;
      &lt;tx_endtime&gt;
          &lt;ESP_TIMEDURATION hours="23" minutes="00" seconds="00"/&gt;
      &lt;/tx_endtime&gt;
      &lt;tx_txdate&gt;
          &lt;ESP_DATE year="2016" dateindays="42063"/&gt;
      &lt;/tx_txdate&gt;
  &lt;/ES_schedules&gt;
  &lt;ES_schedules name="Georgia"&gt;
      &lt;tx_starttime&gt;
          &lt;ESP_TIMEDURATION hours="23" minutes="00" seconds="00" durationinseconds="82800"/&gt;
      &lt;/tx_starttime&gt;
      &lt;tx_endtime&gt;
          &lt;ESP_TIMEDURATION hours="1" minutes="00" seconds="00"/&gt;
      &lt;/tx_endtime&gt;
      &lt;tx_txdate&gt;
          &lt;ESP_DATE year="2016" dateindays="42063"/&gt;
      &lt;/tx_txdate&gt;
  &lt;/ES_schedules&gt;
  &lt;ES_schedules name="Ben"&gt;
      &lt;tx_starttime&gt;
          &lt;ESP_TIMEDURATION hours="1" minutes="00" seconds="00" durationinseconds="90000"/&gt;
      &lt;/tx_starttime&gt;
      &lt;tx_endtime&gt;
          &lt;ESP_TIMEDURATION hours="3" minutes="00" seconds="00"/&gt;
      &lt;/tx_endtime&gt;
      &lt;tx_txdate&gt;
          &lt;ESP_DATE year="2016" dateindays="42063"/&gt;
      &lt;/tx_txdate&gt;
  &lt;/ES_schedules&gt;
  &lt;ES_schedules name="Ben"&gt;
      &lt;tx_starttime&gt;
          &lt;ESP_TIMEDURATION hours="3" minutes="00" seconds="00" durationinseconds="97200"/&gt;
      &lt;/tx_starttime&gt;
      &lt;tx_endtime&gt;
          &lt;ESP_TIMEDURATION hours="5" minutes="00" seconds="00"/&gt;
      &lt;/tx_endtime&gt;
      &lt;tx_txdate&gt;
          &lt;ESP_DATE year="2016" dateindays="42063"/&gt;
      &lt;/tx_txdate&gt;
  &lt;/ES_schedules&gt;
  &lt;ES_schedules name="McGregor"&gt;
      &lt;tx_starttime&gt;
          &lt;ESP_TIMEDURATION hours="5" minutes="00" seconds="00" durationinseconds="104400"/&gt;
      &lt;/tx_starttime&gt;
      &lt;tx_endtime&gt;
          &lt;ESP_TIMEDURATION hours="6" minutes="00" seconds="00"/&gt;
      &lt;/tx_endtime&gt;
      &lt;tx_txdate&gt;
          &lt;ESP_DATE year="2016" dateindays="42063"/&gt;
      &lt;/tx_txdate&gt;
  &lt;/ES_schedules&gt;
  </code></pre>
  
  <p></p>
  
  <p>When I run this via the Munchean method below my output groups "Jeff" incorrectly. Since he works 6-9a and then 12-1p my output shows Jeff working from 6a-1p.</p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  &lt;xsl:output method="text"/&gt;
  
  &lt;xsl:key name="groups" match="schedules/ES_schedules" use="concat(tx_date//@dateindays, '_',@name)"/&gt;
  
  &lt;xsl:template match="/"&gt;
      &lt;xsl:for-each select="schedules/ES_schedules[generate-id() = generate-id(key('groups',concat(tx_date//@dateindays, '_',@name))[1])]"&gt;
          &lt;xsl:sort select="tx_txdate/ESP_DATE/@dateindays" data-type="number" order="ascending"/&gt;
          &lt;xsl:sort select="tx_starttime/ESP_TIMEDURATION/@durationinseconds" data-type="number" order="ascending"/&gt;
          &lt;xsl:variable name="thisDay" select="tx_txdate/ESP_DATE/@dateindays"/&gt;
          &lt;xsl:variable name="thisGroup" select="@name"/&gt;
  
          &lt;xsl:value-of select="@name"/&gt;
          &lt;xsl:text&gt; &lt;/xsl:text&gt;
          &lt;xsl:choose&gt;
              &lt;xsl:when test="@name = 'Jeff'"&gt;
                  &lt;xsl:for-each select="/schedules/ES_schedules[tx_txdate/ESP_DATE/@dateindays = $thisDay and @name = 'Jeff']"&gt;
                      &lt;xsl:sort data-type="number" select="tx_starttime/ESP_TIMEDURATION/@durationinseconds" order="ascending"/&gt;
                      &lt;xsl:if test="position() = 1"&gt;
                          &lt;xsl:apply-templates select="tx_starttime/ESP_TIMEDURATION"/&gt;
                      &lt;/xsl:if&gt;
                  &lt;/xsl:for-each&gt;
              &lt;/xsl:when&gt;
              &lt;xsl:otherwise&gt;
                  &lt;xsl:apply-templates select="tx_starttime/ESP_TIMEDURATION"/&gt;
              &lt;/xsl:otherwise&gt;
          &lt;/xsl:choose&gt;
          &lt;xsl:text&gt; - &lt;/xsl:text&gt;
          &lt;xsl:choose&gt;
              &lt;xsl:when test="@name = 'Jeff'"&gt;
                  &lt;xsl:for-each select="/schedules/ES_schedules[tx_txdate/ESP_DATE/@dateindays = $thisDay and @name = $thisGroup]"&gt;
                      &lt;xsl:sort data-type="number" select="tx_starttime/ESP_TIMEDURATION/@durationinseconds" order="descending"/&gt;
                      &lt;xsl:if test="position() = 1"&gt;
                          &lt;xsl:apply-templates select="tx_endtime/ESP_TIMEDURATION"/&gt;
                      &lt;/xsl:if&gt;
                  &lt;/xsl:for-each&gt;
              &lt;/xsl:when&gt;
              &lt;xsl:otherwise&gt;
                  &lt;xsl:apply-templates select="tx_endtime/ESP_TIMEDURATION"/&gt;
              &lt;/xsl:otherwise&gt;
          &lt;/xsl:choose&gt;
          &lt;xsl:text&gt;&amp;#xD;&lt;/xsl:text&gt;
      &lt;/xsl:for-each&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="ESP_TIMEDURATION"&gt;
      &lt;!--2011-09-01T00:00:00.000--&gt;
      &lt;xsl:value-of select="format-number(@hours,'00')"/&gt;
      &lt;xsl:text&gt;:&lt;/xsl:text&gt;
      &lt;xsl:value-of select="format-number(@minutes,'00')"/&gt;
      &lt;xsl:text&gt;:&lt;/xsl:text&gt;
      &lt;xsl:value-of select="format-number(@seconds,'00')"/&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p></p>
  
  <blockquote>
    <p>Jeff 06:00:00 - 13:00:00 Rich 09:00:00 - 12:00:00 Dan 13:00:00 -
    16:00:00 Shane 16:00:00 - 16:30:00 Sean 16:30:00 - 17:00:00 Joe
    17:00:00 - 17:30:00 Mark 17:30:00 - 18:00:00 Kendra 18:00:00 -
    20:00:00 Crystal 20:30:00 - 22:30:00 Matthew 22:30:00 - 23:00:00
    Georgia 23:00:00 - 01:00:00 Ben 01:00:00 - 03:00:00 McGregor 05:00:00
    - 06:00:00</p>
  </blockquote>
  
  <p>I know that my problem is that the key is based on the date and name, but I'm not sure how to get XSLT to recognize that I want to group only if the prior time period has the same name.</p>