<p>I am performing a find and replace on the line feed character (<code>&amp;#10;</code>) and replacing it with the paragraph close and paragraph open tags using the following code:</p>

<pre><code>&lt;xsl:template match="/STORIES/STORY"&gt;   
    &lt;component&gt;
        &lt;xsl:if test="boolean(ARTICLEBODY)"&gt;
            &lt;p&gt;
                &lt;xsl:call-template name="replace-text"&gt;
                        &lt;xsl:with-param name="text" select="ARTICLEBODY"  /&gt;
                        &lt;xsl:with-param name="replace" select="'&amp;#10;'" /&gt;
                        &lt;xsl:with-param name="by" select="'&amp;lt;/p&amp;gt;&amp;lt;p&amp;gt;'" /&gt;
                &lt;/xsl:call-template&gt;
            &lt;/p&gt;
        &lt;/xsl:if&gt;
    &lt;/component&gt;
&lt;/xsl:template&gt;

&lt;xsl:template name="replace-text"&gt;
   &lt;xsl:param name="text"/&gt;
   &lt;xsl:param name="replace" /&gt;
   &lt;xsl:param name="by"  /&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="$by" disable-output-escaping="yes"/&gt;
      &lt;xsl:call-template name="replace-text"&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="by" select="$by" /&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>

<p>This almost works perfectly, except that I really need it to de-dup the line feeds as the paragraphs tend to be separated by 2 or more resulting in <code>&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;</code>.  </p>

<p>Is it possible to get it so that it will only ever replace this once per paragraph?</p>

[{'answer_id': 31517, 'author': 'Mike Haboustak', 'author_id': 2146, 'author_profile': 'https://Stackoverflow.com/users/2146', 'pm_score': 1, 'selected': False, 'text': '<p>Given the XPath functions that you\'re calling which I don\'t remember having the luxury of in my MSXSL work, it looks like you\'re using an XPath 2-compatible processor. </p>\n\n<p>If that\'s the case, doesn\'t XPath 2 have a replace(string, pattern, replacement) function that takes a regex as a second parameter?  </p>\n\n<pre><code>&lt;xsl:value-of \n    select="replace(string(.), \'&amp;#10;(\\s|&amp;#10;)*\', \'&amp;lt;/p&amp;gt;&amp;lt;p&amp;gt;\')" /&gt;\n</code></pre>\n\n<p>It might help to have some sample Xml input and to know what processor you plan to use.</p>\n\n<p>From your original example, it seems that the duplicate paragraphs all have a white-space only prefix. So something like this slight modification might trim the dupes.</p>\n\n<pre><code>&lt;xsl:when test="contains($text, $replace)"&gt;\n  &lt;xsl:variable name="prefix" select="substring-before($text, $replace)" /&gt;\n  &lt;xsl:choose&gt;\n    &lt;xsl:when test="normalize-string($prefix)!=\'\'"&gt;\n      &lt;xsl:value-of select="$prefix"/&gt;\n      &lt;xsl:value-of select="$by" disable-output-escaping="yes"/&gt;\n    &lt;/xsl:when&gt;\n  &lt;/xsl:choose&gt;\n  &lt;xsl:call-template name="replace-text"&gt;\n     &lt;xsl:with-param name="text" select="substring-after($text, $replace)"/&gt;\n     &lt;xsl:with-param name="replace" select="$replace" /&gt;\n     &lt;xsl:with-param name="by" select="$by" /&gt;\n  &lt;/xsl:call-template&gt;\n</code></pre>\n\n<p></p>\n'}, {'answer_id': 31570, 'author': 'James Sulak', 'author_id': 207, 'author_profile': 'https://Stackoverflow.com/users/207', 'pm_score': 1, 'selected': False, 'text': '<p>Try this (XSLT 2.0):</p>\n\n<pre><code>    &lt;xsl:template match="/STORIES/STORY"&gt;\n        &lt;component&gt;\n            &lt;xsl:if test="boolean(ARTICLEBODY)"&gt;\n                &lt;xsl:call-template name="insert_paras"&gt;\n                    &lt;xsl:with-param name="text" select="ARTICLEBODY/text()"/&gt;\n                &lt;/xsl:call-template&gt;\n            &lt;/xsl:if&gt;\n        &lt;/component&gt;\n    &lt;/xsl:template&gt;\n\n    &lt;xsl:template name="insert_paras"&gt;\n        &lt;xsl:param name="text" /&gt;\n\n        &lt;xsl:variable name="regex"&gt;\n            &lt;xsl:text&gt;&amp;#10;(&amp;#10;|\\s)*&lt;/xsl:text&gt;\n        &lt;/xsl:variable&gt;\n        &lt;xsl:variable name="tokenized-text" select="tokenize($text, $regex)"/&gt;\n\n        &lt;xsl:for-each select="$tokenized-text"&gt;\n            &lt;p&gt;\n                &lt;xsl:value-of select="."/&gt;\n            &lt;/p&gt;\n        &lt;/xsl:for-each&gt;\n    &lt;/xsl:template&gt;\n</code></pre>\n\n<p>It\'s generally a bad idea to use literal strings to put in XML markup, since you can\'t guarantee that the results are balanced.</p>\n'}, {'answer_id': 31725, 'author': 'jelovirt', 'author_id': 2679, 'author_profile': 'https://Stackoverflow.com/users/2679', 'pm_score': 4, 'selected': True, 'text': '<p><code>disable-output-escaping</code> isn\'t evil in itself, but there are only few cases where you should use it and this isn\'t one of them. In XSLT you work with trees, not markup string. Here\'s an XSTL 1.0 solution:</p>\n\n<pre><code>&lt;xsl:template match="/STORIES/STORY"&gt;\n  &lt;component&gt;\n    &lt;xsl:if test="ARTICLEBODY"&gt;\n      &lt;xsl:call-template name="wrap-text"&gt;\n        &lt;xsl:with-param name="text" select="ARTICLEBODY"/&gt;\n        &lt;xsl:with-param name="delimiter" select="\'&amp;#10;\'"/&gt;\n        &lt;xsl:with-param name="element" select="\'p\'"/&gt;\n      &lt;/xsl:call-template&gt;\n    &lt;/xsl:if&gt;\n  &lt;/component&gt;\n&lt;/xsl:template&gt;\n\n&lt;xsl:template name="wrap-text"&gt;\n  &lt;xsl:param name="text"/&gt;\n  &lt;xsl:param name="delimiter"/&gt;\n  &lt;xsl:param name="element"/&gt;\n\n  &lt;xsl:choose&gt;\n    &lt;xsl:when test="contains($text, $delimiter)"&gt;\n      &lt;xsl:variable name="t" select="substring-before($text, $delimiter)"/&gt;\n      &lt;xsl:if test="normalize-space($t)"&gt;\n        &lt;xsl:element name="{$element}"&gt;\n        &lt;xsl:value-of select="$t"/&gt;  \n      &lt;/xsl:element&gt;\n      &lt;/xsl:if&gt;        \n      &lt;xsl:call-template name="wrap-text"&gt;\n        &lt;xsl:with-param name="text" select="substring-after($text, $delimiter)"/&gt;\n        &lt;xsl:with-param name="delimiter" select="$delimiter"/&gt;\n        &lt;xsl:with-param name="element" select="$element"/&gt;\n      &lt;/xsl:call-template&gt;\n    &lt;/xsl:when&gt;\n    &lt;xsl:otherwise&gt;\n      &lt;xsl:if test="normalize-space($text)"&gt;\n        &lt;xsl:element name="{$element}"&gt;\n          &lt;xsl:value-of select="$text"/&gt;  \n        &lt;/xsl:element&gt;\n      &lt;/xsl:if&gt;\n    &lt;/xsl:otherwise&gt;\n  &lt;/xsl:choose&gt;\n&lt;/xsl:template&gt;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/31366', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/274/']