title: Normalize space issue with html tags
body: <p>Here's one for you XSLT gurus :-)</p>
  
  <p>I have to deal with XML output from a Java program I cannot control.</p>
  
  <p>In the docs outputted by this app the html tags remain as </p>
  
  <pre><code>&lt;u&gt;&lt;i&gt;&lt;b&gt;&lt;em&gt;  
  </code></pre>
  
  <p>etc,  instead of </p>
  
  <pre><code>&amp;lt;u&amp;gt;&amp;lt;i&amp;gt;&amp;lt;b&amp;gt;&amp;lt;em&amp;gt; and so on.
  </code></pre>
  
  <p>That's not a massive problem, I use XSLT to fix that, but using normalize-space to remove excess whitespace also removes spaces before these html tags.</p>
  
  <p>Example</p>
  
  <pre><code>&lt;Locator Precode="7"&gt;
  &lt;Text LanguageId="7"&gt;The next word is &lt;b&gt;bold&lt;/b&gt; and is correctly spaced 
  around the html tag,
  but the sentence has extra whitespace and 
  line breaks&lt;/Text&gt;
  &lt;/Locator&gt;
  </code></pre>
  
  <p>If I run the XSLT script we use to remove extra white space, of which this is the relevant part</p>
  
  <pre><code>&lt;xsl:template match="text(.)"&gt;
  &lt;xsl:value-of select="normalize-space()"/&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>In the resulting output the xslt has correctly removed the extra whitespace and the line breaks, but it has also removed the space before the  tag resulting in this output :-</p>
  
  <pre><code>The next word isboldand is correctly spaced around the html tag, but the sentence has extra whitespace and line breaks.
  </code></pre>
  
  <p>The spacing before and after the word "bold" has been stripped as well.</p>
  
  <p>Anyone have any ideas how to prevent this from happening? Pretty well at my wits end so any help will be greatly appreciated!</p>
  
  <p>:-)</p>
  
  <p>Hi again,</p>
  
  <p>Yes of course, here's the full stylesheet. We have to deal with the html tags and spacing in one pass</p>
  
  <pre><code>    &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  &lt;xsl:output method="xml" indent="yes" omit-xml-declaration="no" encoding="UTF-8"/&gt;
  &lt;xsl:strip-space elements="*" /&gt;  
  
  
  &lt;xsl:template match="@*|node()"&gt;
   &lt;xsl:copy&gt; 
    &lt;xsl:apply-templates select="@*|node()"/&gt;
   &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  
  
  &lt;xsl:template match="Text//*"&gt;
    &lt;xsl:value-of select="concat('&amp;lt;',name(),'&amp;gt;')" /&gt;
    &lt;xsl:apply-templates /&gt;
    &lt;xsl:value-of select="concat('&amp;lt;/',name(),'&amp;gt;')" /&gt;
  &lt;/xsl:template&gt;
  &lt;xsl:template match="text()"&gt;
      &lt;xsl:value-of select="normalize-space(.)"/&gt;
  &lt;/xsl:template&gt;
  
  
  &lt;xsl:template match="Instruction//*"&gt;
    &lt;xsl:value-of select="concat('&amp;lt;',name(),'&amp;gt;')" /&gt;
    &lt;xsl:apply-templates /&gt;
    &lt;xsl:value-of select="concat('&amp;lt;/',name(),'&amp;gt;')" /&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="Title//*"&gt;
    &lt;xsl:value-of select="concat('&amp;lt;',name(),'&amp;gt;')" /&gt;
    &lt;xsl:apply-templates /&gt;
    &lt;xsl:value-of select="concat('&amp;lt;/',name(),'&amp;gt;')" /&gt;
  &lt;/xsl:template&gt;
  
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>