title: xpath or xslt (1.0) to find max number of rows in a grid with blocks of arbitrary length
body: <p><strong>Context and ultimate objective</strong></p>
  <p>Consider the below XML which should create the grid in the image. Each col element represents a cell (whether empty or containing a region) with a width and length. For a given block, the starting row (latitude) is known, the ending one not.
  Note there is no <code>&lt;row latitude=&quot;6&quot;/&gt;</code> because that row is already used up as part of the <em>Desert States</em> and <em>Deep South</em> blocks. Similarly, <code>&lt;col timezone=&quot;PDT&quot;/&gt;</code> is missing for row 3 because that cell is already taken up by <em>North West</em>.</p>
  <p>I need to know how many rows I need to make the final grid. In this example, I would need 10 rows.</p>
  <p><strong>Question</strong></p>
  <p>My current approach is to work out the timezone that has to highest sum of length.</p>
  <pre><code>sum(//col[@timezone='EDT']/@length)
  </code></pre>
  <p>The problem with the above xpath is that the timezone is hardcoded here (and in the real application is actually an axis with a very large set of possible values).
  I've tried keys and muenchian grouping but to no avail.</p>
  <p>What xpath 1.0 or xslt 1.0 can I use?</p>
  <p><strong>XML</strong></p>
  <pre><code>&lt;rows&gt;
      &lt;row latitude=&quot;1&quot;&gt;
          &lt;cols&gt;
              &lt;col timezone=&quot;PDT&quot;  width=&quot;1&quot; length=&quot;1&quot;&gt;Canada&lt;/col&gt;
              &lt;col timezone=&quot;CDT&quot;  width=&quot;1&quot; length=&quot;1&quot;&gt;Canada&lt;/col&gt;
              &lt;col timezone=&quot;EDT&quot;  width=&quot;1&quot; length=&quot;1&quot;&gt;Canada&lt;/col&gt;
          &lt;/cols&gt;
      &lt;/row&gt;
      &lt;row latitude=&quot;2&quot;&gt;
          &lt;cols&gt;
              &lt;col timezone=&quot;PDT&quot; width=&quot;1&quot; length=&quot;2&quot;&gt;North West&lt;/col&gt;
              &lt;col timezone=&quot;CDT&quot; width=&quot;1&quot; length=&quot;1&quot;&gt;&lt;/col&gt;
              &lt;col timezone=&quot;EDT&quot; width=&quot;1&quot; length=&quot;1&quot;&gt;&lt;/col&gt;
          &lt;/cols&gt;
      &lt;/row&gt;
      &lt;row latitude=&quot;3&quot;&gt;
          &lt;cols&gt;
              &lt;col timezone=&quot;CDT&quot;  width=&quot;1&quot; length=&quot;1&quot;&gt;&lt;/col&gt;
              &lt;col timezone=&quot;EDT&quot;  width=&quot;1&quot; length=&quot;2&quot;&gt;NY/NJ&lt;/col&gt;
          &lt;/cols&gt;
      &lt;/row&gt;
      &lt;row latitude=&quot;4&quot;&gt;
          &lt;cols&gt;
              &lt;col timezone=&quot;PDT&quot; width=&quot;1&quot; length=&quot;3&quot;&gt;Desert States&lt;/col&gt;
              &lt;col timezone=&quot;CDT&quot; width=&quot;1&quot; length=&quot;1&quot;&gt;&lt;/col&gt;
          &lt;/cols&gt;
      &lt;/row&gt;
      &lt;row latitude=&quot;5&quot;&gt;
          &lt;cols&gt;
              &lt;col timezone=&quot;CDT&quot;  width=&quot;2&quot; length=&quot;6&quot;&gt;Deep South / Bahamas&lt;/col&gt;
              &lt;col timezone=&quot;EDT&quot;  width=&quot;2&quot; length=&quot;6&quot;&gt;Deep South / Bahamas&lt;/col&gt;
          &lt;/cols&gt;
      &lt;/row&gt;
      &lt;row latitude=&quot;7&quot;&gt;
          &lt;cols&gt;
              &lt;col timezone=&quot;PDT&quot; width=&quot;1&quot; length=&quot;2&quot;&gt;California&lt;/col&gt;
          &lt;/cols&gt;
      &lt;/row&gt;
  &lt;/rows&gt;
  </code></pre>
  <p><a href="https://i.stack.imgur.com/7NT6b.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7NT6b.png" alt="grid" /></a></p>