title: XSLT Grouping Specific Siblings
body: <p>I'm working in XSLT 1.0 right now, and I'm having trouble grouping correctly.My xml cannot be changed and it looks like:</p>
  
  <pre><code>&lt;Analysis&gt;
    &lt;comment&gt;Test 1&lt;/comment&gt;
    &lt;Action&gt;
      &lt;Result&gt;Passed&lt;/Result&gt;
    &lt;/Action&gt;
    &lt;Action&gt;
      &lt;Result&gt;Passed&lt;/Result&gt;
    &lt;/Action&gt;
    &lt;Action&gt;
      &lt;Result&gt;Passed&lt;/Result&gt;
    &lt;/Action&gt;
    &lt;comment&gt;Start 2nd Phase&lt;/comment&gt;
    &lt;Action&gt;
      &lt;Result&gt;Failed&lt;/Result&gt;
    &lt;/Action&gt;
    &lt;Action&gt;
      &lt;Result&gt;Passed&lt;/Result&gt;
    &lt;/Action&gt;
    &lt;comment&gt;Test 2&lt;/comment&gt;
    &lt;Action&gt;
      &lt;Result&gt;Failed&lt;/Result&gt;
    &lt;/Action&gt;
    &lt;Action&gt;
      &lt;Result&gt;Failed&lt;/Result&gt;
    &lt;/Action&gt;
    &lt;Action&gt;
      &lt;Result&gt;Passed&lt;/Result&gt;
    &lt;/Action&gt;
    &lt;comment&gt;Start 2nd Phase&lt;/comment&gt;
    &lt;Action&gt;
      &lt;Result&gt;Failed&lt;/Result&gt;
    &lt;/Action&gt;
    &lt;comment&gt;Start 3rd Phase&lt;/comment&gt;
    &lt;Action&gt;
      &lt;Result&gt;Failed&lt;/Result&gt;
    &lt;/Action&gt;
    &lt;comment&gt;Test 3&lt;/comment&gt;
    &lt;Action&gt;
      &lt;Result&gt;Passed&lt;/Result&gt;
    &lt;/Action&gt;
    &lt;Action&gt;
      &lt;Result&gt;Passed&lt;/Result&gt;
    &lt;/Action&gt;
    &lt;Action&gt;
      &lt;Result&gt;Passed&lt;/Result&gt;
    &lt;/Action&gt;
    &lt;Action&gt;
      &lt;Result&gt;Passed&lt;/Result&gt;
    &lt;/Action&gt;
    &lt;Action&gt;
      &lt;Result&gt;Passed&lt;/Result&gt;
    &lt;/Action&gt;
    &lt;comment&gt;Test 4&lt;/comment&gt;
    &lt;Action&gt;
      &lt;Result&gt;Failed&lt;/Result&gt;
    &lt;/Action&gt;
    &lt;Action&gt;
      &lt;Result&gt;Failed&lt;/Result&gt;
    &lt;/Action&gt;
    &lt;Action&gt;
      &lt;Result&gt;Passed&lt;/Result&gt;
    &lt;/Action&gt;
    &lt;comment&gt;Start 2nd Phase&lt;/comment&gt;
    &lt;Action&gt;
      &lt;Result&gt;Failed&lt;/Result&gt;
    &lt;/Action&gt;
    &lt;Action&gt;
      &lt;Result&gt;Passed&lt;/Result&gt;
    &lt;/Action&gt;
  &lt;/Analysis&gt;
  </code></pre>
  
  <p>The code I currently have will print a Failed if it finds a failed underneath the Test, but it only does it once. However, right now it does it underneath each comment, not each comment that has a "Test" in it.</p>
  
  <pre><code>&lt;xsl:key name="actions" match="Action" use="generate-id(preceding-sibling::comment[1])" /&gt;
  
  &lt;xsl:template match="/Analysis"&gt;
      &lt;table&gt;
          &lt;xsl:for-each select="comment"&gt;
              &lt;tr&gt;
                  &lt;td&gt;
                      &lt;xsl:if test="starts-with(text(),'Test')"&gt;
                          &lt;xsl:value-of select="."/&gt;
                      &lt;/xsl:if&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;   
              &lt;xsl:if test="key('actions', generate-id())[Result='Failed']"&gt;
                  &lt;tr&gt;
                      &lt;td&gt;Failed&lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/xsl:if&gt;
          &lt;/xsl:for-each&gt;
      &lt;/table&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>Is there a way to make "preceding-sibling::comment[1]" work only for comments that start with "Test" or contain "Test"? There are a different number of non-important comments that are interspersed with the important information. Like with the example above, I want to see an output that would be like </p>
  
  <pre><code>Test 1
  Failed
  Test 2
  Failed
  Test 3
  Test 4
  Failed
  </code></pre>
  
  <p>However, with the code I currently have I get </p>
  
  <pre><code>Test 1  
  Failed 
  Test 2 
  Failed 
  Failed 
  Failed 
  Test 3 
  Test 4 
  Failed 
  Failed 
  </code></pre>
  
  <p>Any help would be appreciated.</p>