title: How do I structure poorly structured content based off of following/preceding-sibling text() node predicates?
body: <p>Based on the source XML below, I want to capture the text nodes and <code>&lt;emphasis&gt;</code> elements that do not contain <code>&lt;emphasis bold="yes"&gt;</code>NOTE#:<code>&lt;/emphasis&gt;</code>, with their corresponding <code>&lt;emphasis bold="yes"&gt;</code>NOTE#:<code>&lt;/emphasis&gt;</code> element.</p>
  
  <p>Source XML:</p>
  
  <pre><code>&lt;section&gt;
    &lt;para&gt;
      &lt;emphasis bold="yes"&gt;NOTE1:&lt;/emphasis&gt; This is the text of the first note 1 &lt;emphasis bold="yes"&gt;that should only be in the &lt;emphasis italic="yes"&gt;first&lt;/emphasis&gt; subsection occurance of note one.&lt;/emphasis&gt;. This is the second sentence of the first note one. &lt;emphasis italic="yes"&gt;Here is some other text&lt;/emphasis&gt; that can appear. &lt;emphasis bold="yes"&gt;Marvin Gaye is an excellent musician1.&lt;/emphasis&gt; Play it for your girlfriend1 &lt;emphasis italic="yes"&gt;now1.&lt;/emphasis&gt;.
      &lt;emphasis bold="yes"&gt;NOTE2:&lt;/emphasis&gt; This is the text of  the first note two2.1 &lt;emphasis italic="yes"&gt;The Isley Brothers are also good.2.1&lt;/emphasis&gt;
      &lt;emphasis bold="yes"&gt;NOTE1:&lt;/emphasis&gt; This is the text of the second note one.1.2 &lt;emphasis italic="yes"&gt;My girlfriend loves them1.2&lt;/emphasis&gt;
      &lt;emphasis bold="yes"&gt;NOTE3:&lt;/emphasis&gt; This is the text of the first note three3.1.
      &lt;emphasis bold="yes"&gt;NOTE1:&lt;/emphasis&gt; This is the text of the third note one.1.3&lt;emphasis italic="yes"&gt;She is going to make me dinner tonight1.3&lt;/emphasis&gt;
      &lt;emphasis bold="yes"&gt;NOTE3:&lt;/emphasis&gt; This is the text of the second note three.3.2&lt;emphasis italic="yes"&gt;Steak and potatos3.2&lt;/emphasis&gt;
      &lt;emphasis bold="yes"&gt;NOTE2:&lt;/emphasis&gt; This is the text of the second note two.2.2&lt;emphasis italic="yes"&gt;And then some wine2.2&lt;/emphasis&gt;
    &lt;/para&gt;
  &lt;/section&gt;
  </code></pre>
  
  <p>Current XSLT:</p>
  
  <pre><code>&lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  
    &lt;xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/&gt;
  
    &lt;xsl:template match="*|@*|text()"&gt;
      &lt;xsl:copy&gt;
        &lt;xsl:apply-templates select="*|@*|text()"/&gt;
      &lt;/xsl:copy&gt;
    &lt;/xsl:template&gt;
  
    &lt;xsl:template match="section"&gt;
      &lt;root&gt;
        &lt;xsl:apply-templates select="*|@*|text()"/&gt;
      &lt;/root&gt;
    &lt;/xsl:template&gt;
  
    &lt;xsl:template match="para/emphasis[preceding-sibling::emphasis[@bold='yes' and text()='NOTE1:']]"/&gt;
    &lt;xsl:template match="para/emphasis[preceding-sibling::emphasis[@bold='yes' and text()='NOTE2:']]"/&gt;
    &lt;xsl:template match="para/emphasis[preceding-sibling::emphasis[@bold='yes' and text()='NOTE3:']]"/&gt;
    &lt;xsl:template match="para/text()[preceding-sibling::emphasis[@bold='yes' and text()='NOTE1:']]"/&gt;
    &lt;xsl:template match="para/text()[preceding-sibling::emphasis[@bold='yes' and text()='NOTE2:']]"/&gt;
    &lt;xsl:template match="para/text()[preceding-sibling::emphasis[@bold='yes' and text()='NOTE3:']]"/&gt;
  
    &lt;xsl:template match="para/emphasis[@bold='yes' and text()='NOTE1:' and not(position()=last())]"&gt;
      &lt;subsection&gt;
        &lt;para&gt;
          &lt;emphasis bold="yes"&gt;NOTE1:&lt;/emphasis&gt;
          &lt;xsl:copy-of
            select="following-sibling::text()[not(preceding-sibling::emphasis[@bold='yes' and text()='NOTE2:'])  and not(preceding-sibling::emphasis[@bold='yes' and text()='NOTE3:'])] |following-sibling::emphasis[not(contains(string(), 'NOTE1:'))  and  not(contains(string(), 'NOTE2:'))  and not(contains(string(), 'NOTE3:'))]"
          /&gt;
        &lt;/para&gt;
      &lt;/subsection&gt;
    &lt;/xsl:template&gt;
  
    &lt;xsl:template match="para/emphasis[position()=last() and position() &gt; 1  and @bold='yes' and text()='NOTE1:']"&gt;
      &lt;subsection&gt;
        &lt;para&gt;
          &lt;emphasis bold="yes"&gt;NOTE1:&lt;/emphasis&gt;
          &lt;xsl:copy-of
            select="following-sibling::text()[position() = 1]|following-sibling::emphasis[not(contains(string(), 'NOTE1:'))  and  not(contains(string(), 'NOTE2:'))  and not(contains(string(), 'NOTE3:'))]"
          /&gt;
        &lt;/para&gt;
      &lt;/subsection&gt;
    &lt;/xsl:template&gt;
  
    &lt;xsl:template match="para/emphasis[@bold='yes' and text()='NOTE2:' and not(position()=last())]"&gt;
      &lt;subsection&gt;
        &lt;para&gt;
          &lt;emphasis bold="yes"&gt;NOTE2:&lt;/emphasis&gt;
          &lt;xsl:copy-of
            select="following-sibling::text()[not(preceding-sibling::emphasis[@bold='yes' and text()='NOTE1:'])  and not(preceding-sibling::emphasis[@bold='yes' and text()='NOTE3:'])] |following-sibling::emphasis[not(contains(string(), 'NOTE1:'))  and  not(contains(string(), 'NOTE2:'))  and not(contains(string(), 'NOTE3:'))]"
          /&gt;
        &lt;/para&gt;
      &lt;/subsection&gt;
    &lt;/xsl:template&gt;
  
    &lt;xsl:template match="para/emphasis[position()=last() and position() &gt; 1 and @bold='yes' and text()='NOTE2:']"&gt;
      &lt;note&gt;
        &lt;para&gt;
          &lt;emphasis bold="yes"&gt;NOTE2:&lt;/emphasis&gt;
          &lt;xsl:copy-of
            select="following-sibling::text()[position() = 1]|following-sibling::emphasis[not(contains(string(), 'NOTE1:'))  and  not(contains(string(), 'NOTE2:')) and not(contains(string(), 'NOTE3:'))  ]"
          /&gt;
        &lt;/para&gt;
      &lt;/note&gt;
    &lt;/xsl:template&gt;
  
    &lt;xsl:template match="para/emphasis[@bold='yes' and text()='NOTE3:' and not(position()=last())]"&gt;
      &lt;note&gt;
        &lt;para&gt;
          &lt;emphasis bold="yes"&gt;NOTE3:&lt;/emphasis&gt;
          &lt;xsl:copy-of
            select="following-sibling::text()[not(preceding-sibling::emphasis[@bold='yes' and text()='NOTE2:'])  and not(preceding-sibling::emphasis[@bold='yes' and text()='NOTE1:'])] | following-sibling::emphasis[not(contains(string(), 'NOTE1:'))  and  not(contains(string(), 'NOTE2:'))  and not(contains(string(), 'NOTE3:'))]"
          /&gt;
        &lt;/para&gt;
      &lt;/note&gt;
    &lt;/xsl:template&gt;
  
    &lt;xsl:template match="para/emphasis[position()=last() and position() &gt; 1 and @bold='yes' and text()='NOTE3:']"&gt;
      &lt;note&gt;
        &lt;para&gt;
          &lt;emphasis bold="yes"&gt;NOTE3:&lt;/emphasis&gt;
          &lt;xsl:copy-of select="following-sibling::text()[position() = 1]|following-sibling::emphasis[not(contains(string(), 'NOTE1:'))  and  not(contains(string(), 'NOTE2:'))  and not(contains(string(), 'NOTE3:'))  ]"/&gt;
        &lt;/para&gt;
      &lt;/note&gt;
    &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>Current output:</p>
  
  <pre><code>&lt;root&gt;
    &lt;para&gt;
      &lt;subsection&gt;
        &lt;para&gt;
          &lt;emphasis bold="yes"&gt;NOTE1:&lt;/emphasis&gt; This is the text of the first note 1 &lt;emphasis bold="yes"&gt;that should only be in the &lt;emphasis italic="yes"&gt;first&lt;/emphasis&gt; subsection occurance of note one.&lt;/emphasis&gt;. This is the second sentence of the first note one. &lt;emphasis italic="yes"&gt;Here is some other text&lt;/emphasis&gt; that can appear. &lt;emphasis bold="yes"&gt;Marvin Gaye is an excellent musician1.&lt;/emphasis&gt; Play it for your girlfriend1 &lt;emphasis italic="yes"&gt;now1.&lt;/emphasis&gt;.
          &lt;emphasis italic="yes"&gt;The Isley Brothers are also good.2.1&lt;/emphasis&gt;
          &lt;emphasis italic="yes"&gt;My girlfriend loves them1.2&lt;/emphasis&gt;
          &lt;emphasis italic="yes"&gt;She is going to make me dinner tonight1.3&lt;/emphasis&gt;
          &lt;emphasis italic="yes"&gt;Steak and potatos3.2&lt;/emphasis&gt;
          &lt;emphasis italic="yes"&gt;And then some wine2.2&lt;/emphasis&gt;
        &lt;/para&gt;
      &lt;/subsection&gt;
      &lt;subsection&gt;
        &lt;para&gt;
          &lt;emphasis bold="yes"&gt;NOTE2:&lt;/emphasis&gt;
          &lt;emphasis italic="yes"&gt;The Isley Brothers are also good.2.1&lt;/emphasis&gt;
          &lt;emphasis italic="yes"&gt;My girlfriend loves them1.2&lt;/emphasis&gt;
          &lt;emphasis italic="yes"&gt;She is going to make me dinner tonight1.3&lt;/emphasis&gt;
          &lt;emphasis italic="yes"&gt;Steak and potatos3.2&lt;/emphasis&gt;
          &lt;emphasis italic="yes"&gt;And then some wine2.2&lt;/emphasis&gt;
        &lt;/para&gt;
      &lt;/subsection&gt;
      &lt;subsection&gt;
        &lt;para&gt;
          &lt;emphasis bold="yes"&gt;NOTE1:&lt;/emphasis&gt;
          &lt;emphasis italic="yes"&gt;My girlfriend loves them1.2&lt;/emphasis&gt;
          &lt;emphasis italic="yes"&gt;She is going to make me dinner tonight1.3&lt;/emphasis&gt;
          &lt;emphasis italic="yes"&gt;Steak and potatos3.2&lt;/emphasis&gt;
          &lt;emphasis italic="yes"&gt;And then some wine2.2&lt;/emphasis&gt;
        &lt;/para&gt;
      &lt;/subsection&gt;
      &lt;note&gt;
        &lt;para&gt;
          &lt;emphasis bold="yes"&gt;NOTE3:&lt;/emphasis&gt;
          &lt;emphasis italic="yes"&gt;She is going to make me dinner tonight1.3&lt;/emphasis&gt;
          &lt;emphasis italic="yes"&gt;Steak and potatos3.2&lt;/emphasis&gt;
          &lt;emphasis italic="yes"&gt;And then some wine2.2&lt;/emphasis&gt;
        &lt;/para&gt;
      &lt;/note&gt;
      &lt;subsection&gt;
        &lt;para&gt;
          &lt;emphasis bold="yes"&gt;NOTE1:&lt;/emphasis&gt;
          &lt;emphasis italic="yes"&gt;She is going to make me dinner tonight1.3&lt;/emphasis&gt;
          &lt;emphasis italic="yes"&gt;Steak and potatos3.2&lt;/emphasis&gt;
          &lt;emphasis italic="yes"&gt;And then some wine2.2&lt;/emphasis&gt;
        &lt;/para&gt;
      &lt;/subsection&gt;
      &lt;note&gt;
        &lt;para&gt;
          &lt;emphasis bold="yes"&gt;NOTE3:&lt;/emphasis&gt;
          &lt;emphasis italic="yes"&gt;Steak and potatos3.2&lt;/emphasis&gt;
          &lt;emphasis italic="yes"&gt;And then some wine2.2&lt;/emphasis&gt;
        &lt;/para&gt;
      &lt;/note&gt;
      &lt;subsection&gt;
        &lt;para&gt;
          &lt;emphasis bold="yes"&gt;NOTE2:&lt;/emphasis&gt;
          &lt;emphasis italic="yes"&gt;And then some wine2.2&lt;/emphasis&gt;
        &lt;/para&gt;
      &lt;/subsection&gt;
    &lt;/para&gt;
  &lt;/root&gt;
  
  
  Desired output:
  
  &lt;root&gt;
    &lt;para&gt;
      &lt;subsection&gt;
        &lt;para&gt;
          &lt;emphasis bold="yes"&gt;NOTE1:&lt;/emphasis&gt; This is the text of the first note 1 &lt;emphasis bold="yes"&gt;that should only be in the &lt;emphasis italic="yes"&gt;first&lt;/emphasis&gt; subsection occurance of note one.&lt;/emphasis&gt;. This is the second sentence of the first note one. &lt;emphasis italic="yes"&gt;Here is some other text&lt;/emphasis&gt; that can appear. &lt;emphasis bold="yes"&gt;Marvin Gaye is an excellent musician1.&lt;/emphasis&gt; Play it for your girlfriend1 &lt;emphasis italic="yes"&gt;now1.&lt;/emphasis&gt;.
        &lt;/para&gt;
      &lt;/subsection&gt;
      &lt;subsection&gt;
        &lt;para&gt;
          &lt;emphasis bold="yes"&gt;NOTE2:&lt;/emphasis&gt;This is the text of  the first note two2.1&lt;emphasis italic="yes"&gt;The Isley Brothers are also good.2.1&lt;/emphasis&gt;
        &lt;/para&gt;
      &lt;/subsection&gt;
      &lt;subsection&gt;
        &lt;para&gt;
          &lt;emphasis bold="yes"&gt;NOTE1:&lt;/emphasis&gt; This is the text of the second note one.1.2 &lt;emphasis italic="yes"&gt;My girlfriend loves them1.2&lt;/emphasis&gt;
        &lt;/para&gt;
      &lt;/subsection&gt;
      &lt;subsection&gt;
        &lt;para&gt;
          &lt;emphasis bold="yes"&gt;NOTE3:&lt;/emphasis&gt; This is the text of the first note three3.1.
        &lt;/para&gt;
      &lt;/subsection&gt;
      &lt;subsection&gt;
        &lt;para&gt;
          &lt;emphasis bold="yes"&gt;NOTE1:&lt;/emphasis&gt; This is the text of the third note one.1.3&lt;emphasis italic="yes"&gt;She is going to make me dinner tonight1.3&lt;/emphasis&gt;
        &lt;/para&gt;
      &lt;/subsection&gt;
      &lt;subsection&gt;
        &lt;para&gt;
          &lt;emphasis bold="yes"&gt;NOTE3:&lt;/emphasis&gt; This is the text of the second note three.3.2&lt;emphasis italic="yes"&gt;Steak and potatos3.2&lt;/emphasis&gt;
        &lt;/para&gt;
      &lt;/subsection&gt;
      &lt;subsection&gt;
        &lt;para&gt;
          &lt;emphasis bold="yes"&gt;NOTE2:&lt;/emphasis&gt; This is the text of the second note two.2.2&lt;emphasis italic="yes"&gt;And then some wine2.2&lt;/emphasis&gt;
        &lt;/para&gt;
      &lt;/subsection&gt;
    &lt;/para&gt;
  &lt;/root&gt;
  </code></pre>