title: Trim white-spaces at the end of lines only before a specific tag
body: <p>could you please help me?! I'm dealing with the following problem: the lines in my xml begin with the <code>&lt;lb&gt;</code> element. Some of these elements have an attribute <code>&lt;lb break="no"&gt;</code>. I need to transform the line-by-line text into a floating text and also trim whitespaces before <code>&lt;lb break="no"&gt;</code>, so that the word is written without spaces. At the same time it is necessary to leave all the whitespaces that are presented in the text, as there are many different tags that go one after another. If I'm using normalize-space() function, all of the whitespaces between the tags get lost and I need to write a test like this <code>&lt;xsl:if test="following-sibling::*[1][self::tei:span]"&gt;
              &lt;xsl:text&gt; &lt;/xsl:text&gt;</code> for every case.</p>
  
  <p>How can I solve this?</p>
  
  <p>I've found such a <a href="https://stackoverflow.com/questions/753086/how-to-remove-spaces-from-the-end-of-text-in-an-element-xsl">solution</a> 
   <code>&lt;xsl:template match="text()"&gt;
      "&lt;xsl:sequence select="replace(., '\s+$', '', 'm')"/&gt;"
    &lt;/xsl:template&gt;</code>
  but I don't know how to use such a code in my case.</p>
  
  <p>Here is the sample text:</p>
  
  <pre><code>&lt;p&gt;
  &lt;lb n="3"/&gt;Ich ergreife diese Gelegenheit eine Bitte an Sie zu rich 
  &lt;lb n="4" break="no"/&gt;ten,&lt;span type="inter" xml:id="GR55024-inter2"&gt;&amp;#32;&lt;/span&gt; zu &lt;span type="inter" xml:id="GR55024-inter3"&gt;die&lt;/span&gt; Sie mir&lt;span type="inter" xml:id="GR55024-inter4"&gt;&amp;#32;&lt;/span&gt; die Aufmunterung durch das güti
  &lt;lb n="5" break="no"/&gt;ge Versprechen gaben, &lt;span type="inter" xml:id="GR55024-inter5"&gt;im Fall ich Bücher von der Je 
  &lt;lb n="6" break="no"/&gt;naischen Bibliothek&lt;ptr type="app" target="#GR55024-seite1-les1"/&gt; nöthig haben sollte,&lt;/span&gt; mir dieselben 
  &lt;lb n="7"/&gt;gefälligst zu verschaffen. Ich &lt;span type="inter" xml:id="GR55024-inter6"&gt;bedarf&lt;/span&gt; jetzt zur Recen
  &lt;lb n="8" break="no"/&gt;sion &lt;span type="inter" xml:id="GR55024-inter7"&gt;des Buches&lt;/span&gt; über &lt;span type="inter" xml:id="GR55024-inter8"&gt;die&lt;/span&gt; Verwandschaft der &lt;span type="inter" xml:id="GR55024-inter9"&gt;griechischen 
  &lt;lb n="9"/&gt;und deutschen&lt;/span&gt; Sprache &lt;span type="inter" xml:id="GR55024-inter10"&gt;das &lt;hi rend="unterstrichen"&gt;Glossarium von&lt;/hi&gt;&lt;/span&gt; &lt;hi rend="unterstrichen"&gt;&lt;persName key=""&gt;Hesychius&lt;/persName&gt;&lt;/hi&gt;, 
  &lt;lb n="10"/&gt;&lt;span type="inter" xml:id="GR55024-inter11"&gt;edit. Alberti &amp;#x26; Ruhnkenii, und&lt;/span&gt; &lt;title type="werk" key=""&gt;&lt;persName key=""&gt;&lt;hi rend="unterstrichen"&gt;Corinthius&lt;/hi&gt;&lt;/persName&gt;&lt;/title&gt;
  &lt;lb n="11"/&gt;de dialectis, in der holländ. Ausgabe von &lt;hi rend="unterstrichen"&gt;&lt;persName key=""&gt;Koen&lt;/persName&gt;&lt;/hi&gt;, &lt;hi rend="unterstrichen"&gt;8&lt;hi rend="unterstrichen"&gt;°&lt;/hi&gt;&lt;/hi&gt; und 
  &lt;/p&gt; 
  </code></pre>
  
  <p>Thanks a lot!</p>
  
  <p>I'm using the oXygen XML Editor and its XSLT-Debugger with Saxon 9.</p>
  
  <p>Xslt template (not working)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns="http://www.w3.org/1999/xhtml"
  xpath-default-namespace="http://www.tei-c.org/ns/1.0"
  version="2.0"&gt;
  
  &lt;xsl:output method="xhtml" indent="yes"/&gt;
  &lt;xsl:preserve-space elements="text"/&gt;
  
  &lt;xsl:template match="/"&gt;
      &lt;html&gt;
          &lt;head&gt;
              &lt;title&gt;
                  Title
              &lt;/title&gt;
          &lt;/head&gt;
          &lt;body&gt;
              &lt;div&gt;
              &lt;xsl:apply-templates select="//div[@ana='ausfertigung']"&gt;
              &lt;/xsl:apply-templates&gt;
              &lt;/div&gt;
          &lt;/body&gt;
      &lt;/html&gt;
  &lt;/xsl:template&gt;
  
  
  &lt;xsl:template match="p"&gt;
      &lt;p&gt;
          &lt;xsl:apply-templates/&gt;
      &lt;/p&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="p/text()"&gt;
      &lt;xsl:text&gt; &lt;/xsl:text&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:variable name="special-handling" select="text()
  [following-sibling::*[1][self::lb[@break='no']]]"/&gt;
  
  &lt;xsl:template match="text()[following-sibling::*[1][self::lb[@break='no']]]"&gt;
      &lt;xsl:if test="$special-handling/ends-with(text(), '\n')"&gt;&lt;/xsl:if&gt;
  &lt;xsl:value-of select="$special-handling/substring-before(text()[following-sibling::*[1]
  [self::lb[@break='no']]],'\n')"&gt;&lt;/xsl:value-of&gt;
  &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>The preliminary code that works found <a href="http://nofoe.blogspot.de/2010_10_01_archive.html" rel="nofollow noreferrer">here</a></p>
  
  <pre><code>&lt;xsl:template match="para/text()"&gt;
      &lt;xsl:call-template name="selectWithoutBreaks"/&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>Utilizes these templates:</p>
  
  <pre><code>&lt;xsl:template name="selectWithoutBreaks" &gt;
  &lt;xsl:variable name="linebreak"&gt;
      &lt;xsl:text&gt;
  &lt;/xsl:text&gt;
  &lt;/xsl:variable&gt;
  
  &lt;xsl:call-template name="replace-string"&gt;
      &lt;xsl:with-param name="text" select="."/&gt;
      &lt;xsl:with-param name="replace" select="$linebreak" /&gt;
      &lt;xsl:with-param name="with" select="''"/&gt;
  &lt;/xsl:call-template&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template name="replace-string"&gt;
  &lt;xsl:param name="text"/&gt;
  &lt;xsl:param name="replace"/&gt;
  &lt;xsl:param name="with"/&gt;
  &lt;xsl:choose&gt;
      &lt;xsl:when test="contains($text,$replace)"&gt;
          &lt;xsl:value-of select="substring-before($text,$replace)"/&gt;
          &lt;xsl:value-of select="$with"/&gt;
          &lt;xsl:call-template name="replace-string"&gt;
              &lt;xsl:with-param name="text" select="substring-after($text,$replace)"/&gt;
              &lt;xsl:with-param name="replace" select="$replace"/&gt;
              &lt;xsl:with-param name="with" select="$with"/&gt;
          &lt;/xsl:call-template&gt;
      &lt;/xsl:when&gt;
      &lt;xsl:otherwise&gt;
          &lt;xsl:value-of select="$text"/&gt;
      &lt;/xsl:otherwise&gt;
  &lt;/xsl:choose&gt;
  &lt;/xsl:template&gt;
  </code></pre>