title: ampersand entities in long strings make template malfunction
body: <p>I'm using a template I found on the internet to split long strings into chunks. It seems to work fine with most text but if there is an entity in this supposedly long text input such as '&amp;' it makes the output chunk too long.</p>
  
  <p>My consumer of this data needs long description values in certain entities broken down into fixed length pieces. So this data is fine:</p>
  
  <pre><code>&lt;tag&gt;
      &lt;text&gt;This is a long string 1This is a long string 2This is a long string 3This is a long string 4&lt;/text&gt;
  &lt;/tag&gt;
  </code></pre>
  
  <p>resulting output:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;tag&gt;
      &lt;text&gt;
        &lt;text&gt;This is a long string 1&lt;/text&gt;
        &lt;text&gt;This is a long string 2&lt;/text&gt;
        &lt;text&gt;This is a long string 3&lt;/text&gt;
        &lt;text&gt;This is a long string 4&lt;/text&gt;
     &lt;/text&gt;
  &lt;/tag&gt;
  </code></pre>
  
  <p>this data is too long here in the first output string:</p>
  
  <pre><code>&lt;tag&gt;
      &lt;text&gt;&amp;amp;This is a long string 1This is a long string 2This is a long string 3This is a long string 4&lt;/text&gt;
  &lt;/tag&gt;
  </code></pre>
  
  <p>resulting output:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;tag&gt;
      &lt;text&gt;
        &lt;text&gt;&amp;amp;This is a long string &lt;/text&gt;
        &lt;text&gt;1This is a long string &lt;/text&gt;
        &lt;text&gt;2This is a long string &lt;/text&gt;
        &lt;text&gt;3This is a long string &lt;/text&gt;
        &lt;text&gt;4&lt;/text&gt;
     &lt;/text&gt;
  &lt;/tag&gt;
  </code></pre>
  
  <p>I tried changing the output to html but that didn't change the behavior - anyways the output is supposed to by xml.</p>
  
  <p>I'm actually not sure if the problem is really solvable as the XML is only the middle man and the actual source and destination ultimately are plain text database field, but I'd like to chunk the long string into short strings exactly the desired length.</p>
  
  <p>Here is the template.. here the desired size is: 23</p>
  
  <pre><code>&lt;?xml version='1.0'?&gt;
  &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:max="http://www.ibm.com/maximo" exclude-result-prefixes="max"&gt;
  &lt;xsl:output method="xml" encoding="utf-8" indent="yes"/&gt;
  &lt;xsl:variable name="pChunkSize" select="23" /&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/text()" name="chunk"&gt;
  &lt;xsl:param name="pText" select="."/&gt;
  
  &lt;xsl:if test="string-length($pText) &gt;0"&gt;
  &lt;text&gt;
  &lt;xsl:value-of select=
     "substring($pText, 1, $pChunkSize)"/&gt;
  &lt;/text&gt;
  &lt;xsl:call-template name="chunk"&gt;
  &lt;xsl:with-param name="pText"
      select="substring($pText, $pChunkSize+1)"/&gt;
  &lt;/xsl:call-template&gt;
  &lt;/xsl:if&gt;
  &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>I have seen some discussion about this and it might be intractable - since chopping &amp; in the middle (if it was towards the end of the segment) results in invalid XML.</p>