title: XSLT: outputting element values contingent on values of preceding and following siblings
body: <p>I’m working with some XML with a flat structure, in which items are implicitly identified by means of repeated groups of elements, without being properly nested in a single parent element for each item (which is the cause of headaches). I need to transform it, using XSLT, into a version of the same thing, in which certain of these groups are output with different text, depending on the value of one of the elements in the group of sibling elements. So, if the input looks like this:</p>
  
  <pre><code>&lt;items&gt;
  
  &lt;item_title&gt;Item 1&lt;/item_title&gt;
  &lt;text1&gt;Item 1 Text 1&lt;/text1&gt;
  &lt;text2&gt;Item 1 Text 2&lt;/text2&gt;
  &lt;text3&gt;Item 1 Text 3&lt;/text3&gt;
  &lt;text4&gt;Item 1 Text 4&lt;/text4&gt;
  &lt;text5&gt;Item 1 Text 5&lt;/text5&gt;
  
  &lt;item_title&gt;Item 2&lt;/item_title&gt;
  &lt;text1&gt;Item 2 Text 1&lt;/text1&gt;
  &lt;flag&gt;Suppress text&lt;/flag&gt;
  &lt;text4&gt;Item 2 Text 4&lt;/text4&gt;
  &lt;text5&gt;Item 2 Text 5&lt;/text5&gt;
  
  &lt;item_title&gt;Item 3&lt;/item_title&gt;
  &lt;text1&gt;Item 3 Text 1&lt;/text1&gt;
  &lt;text3&gt;Item 3 Text 3&lt;/text3&gt;
  &lt;text4&gt;Item 3 Text 4&lt;/text4&gt;
  
  &lt;item_title&gt;Item 4&lt;/item_title&gt;
  &lt;text1&gt;Item 4 Text 1&lt;/text1&gt;
  &lt;text4&gt;Item 4 Text 4&lt;/text4&gt;
  &lt;text5&gt;Item 4 Text 5&lt;/text5&gt;
  
  &lt;item_title&gt;Item 5&lt;/item_title&gt;
  &lt;text2&gt;Item 5 Text 2&lt;/text2&gt;
  &lt;text3&gt;Item 5 Text 3&lt;/text3&gt;
  &lt;flag&gt;Suppress text&lt;/flag&gt;
  &lt;text5&gt;Item 5 Text 5&lt;/text5&gt;
  
  &lt;/items&gt;
  </code></pre>
  
  <p>I want to output something like this:</p>
  
  <pre><code>&lt;items&gt;
  
  &lt;item_title&gt;Item 1&lt;/item_title&gt;
  &lt;text1&gt;Item 1 Text 1&lt;/text1&gt;
  &lt;text2&gt;Item 1 Text 2&lt;/text2&gt;
  &lt;text3&gt;Item 1 Text 3&lt;/text3&gt;
  &lt;text4&gt;Item 1 Text 4&lt;/text4&gt;
  &lt;text5&gt;Item 1 Text 5&lt;/text5&gt;
  
  &lt;item_title&gt;Item 2&lt;/item_title&gt;
  &lt;text1&gt;The text of this item has been suppressed.&lt;/text1&gt;
  
  &lt;item_title&gt;Item 3&lt;/item_title&gt;
  &lt;text1&gt;Item 3 Text 1&lt;/text1&gt;
  &lt;text3&gt;Item 3 Text 3&lt;/text3&gt;
  &lt;text4&gt;Item 3 Text 4&lt;/text4&gt;
  
  &lt;item_title&gt;Item 4&lt;/item_title&gt;
  &lt;text1&gt;Item 4 Text 1&lt;/text1&gt;
  &lt;text4&gt;Item 4 Text 4&lt;/text4&gt;
  &lt;text5&gt;Item 4 Text 5&lt;/text5&gt;
  
  &lt;item_title&gt;Item 5&lt;/item_title&gt;
  &lt;text1&gt;The text of this item has been suppressed.&lt;/text1&gt;
  
  &lt;/items&gt;
  </code></pre>
  
  <p>I’ve been puzzling over how (and indeed, whether) it might be possible to write an XPath expression that identifies, for each /item_title, whether it has a following-sibling::flag before (in document order) its next following-sibling::item_title, and then for each text element, I could embed this XPath expression in an XPath expression that identifies whether the element’s closest preceding-sibling::item_title meets this criterion (in which case the element would not get output). But I haven’t been able to work out how.</p>
  
  <p>Is this possible through an approach like the one I mention, or through some alternative approach?</p>
  
  <p>This is a problem caused by the fact that I’m dealing with rubbish XML, but that’s what I’ve got.</p>
  
  <p>Thank you for any advice.</p>