title: XPath expression (in XSLT) to match siblings of node, up to a sibling fulfilling condition based on its children
body: <p>I have the following markup, a table with a mix of TRs with one or two child TDs</p>
  
  <pre><code>  &lt;table&gt;
      &lt;tr&gt;
        &lt;td&gt;
           &lt;p&gt;XXX&lt;/p&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;
          &lt;p&gt;YYYYYY&lt;/p&gt;
        &lt;/td&gt;
        &lt;td&gt;
          &lt;p&gt;YYYYYY&lt;/p&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;
          &lt;p&gt;YYYYYY&lt;/p&gt;
        &lt;/td&gt;
        &lt;td&gt;
          &lt;p&gt;YYYYYY&lt;/p&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;
          &lt;p&gt;YYYYYY&lt;/p&gt;
        &lt;/td&gt;
        &lt;td&gt;
          &lt;p&gt;YYYYYY&lt;/p&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;
          &lt;p&gt;XXX&lt;/p&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;
          &lt;p&gt;YYYYYY&lt;/p&gt;
        &lt;/td&gt;
        &lt;td&gt;
          &lt;p&gt;YYYYYY&lt;/p&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;
          &lt;p&gt;YYYYYY&lt;/p&gt;
        &lt;/td&gt;
        &lt;td&gt;
          &lt;p&gt;YYYYYY&lt;/p&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;
          &lt;p&gt;XXX&lt;/p&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;
          &lt;p&gt;YYYYYY&lt;/p&gt;
        &lt;/td&gt;
        &lt;td&gt;
          &lt;p&gt;YYYYYY&lt;/p&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;
          &lt;p&gt;YYYYYY&lt;/p&gt;
        &lt;/td&gt;
        &lt;td&gt;
          &lt;p&gt;YYYYYY&lt;/p&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
  </code></pre>
  
  <p>I am trying to use XSLT 1.0 to transform this into</p>
  
  <pre><code>&lt;AAA&gt;
    XXX
    &lt;BBB&gt;YYYYY&lt;/BBB&gt;
    &lt;BBB&gt;YYYYY&lt;/BBB&gt;
    ...
  &lt;/AAA&gt;
  &lt;AAA&gt;
    XXX
    &lt;BBB&gt;YYYYY&lt;/BBB&gt;
    &lt;BBB&gt;YYYYY&lt;/BBB&gt;
    ...
  &lt;/AAA&gt;
  ...
  </code></pre>
  
  <p>Abstracting from the fact that this is probably to the best way to approach a nested loop in XSLT, what XPath expression put into ??? below would select all TR siblings of a current TR (XXX) which have two TD children, but only until the next TR (XXX). I am differentiating XXX and YYYYY-containing nodes on the basis of how many TD children they have. 1 = XXX, 2 = YYYYY.</p>
  
  <pre><code>&lt;xsl:for-each select="//table/tr"&gt;
     &lt;xsl:if test="count(td) = '1'"&gt;
        XXX
     &lt;/xsl:if&gt;
     &lt;xsl:for-each select="???"&gt;
        all YYYYY up until the next XXX
     &lt;/xsl:for-each&gt;
  &lt;/xsl:for-each&gt;
  </code></pre>
  
  <p>I have "following-sibling::tr[child::td[following-sibling::td]" but that matches all the following  with YYYYY to the end - how do I make it select only those up until the next TR with XXX?</p>
  
  <p>Thanks!</p>