You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Michael Giroux <ml...@gmail.com> on 2006/12/07 14:32:56 UTC

string-utils:replace deleting search string if replacement string is an HTML element

The following template attempts to convert new-lines to <br/>
elements.  Unfortunately, all this does is delete the new-line
characters.

Changing the br param to be a value other than an html element (#br/#
for example) results in the desired execution.

I believe this worked as one point because it is part of the ANT
junitreport processing.  It acts as if the replacement string is
empty, or that the replace function is not finding a corresponding
entry in the replacement node for the match on the new-line.

Is there any restrictions on using html elements in the replacement string?


<xsl:template name="br-replace">
  <xsl:param name="word"/>
  <xsl:param name="br"><br/></xsl:param>
  <xsl:value-of select='stringutils:replace(string($word),"&#xA;",$br)'/>
</xsl:template>


Thanks
Michael Giroux

Re: string-utils:replace deleting search string if replacement string is an HTML element

Posted by ke...@us.ibm.com.
That's better than mine; I forgot to handle the case where no line-break
exists.

BTW, in addition to the xsl-list (which is a great resource for XSLT
programming advice), you should be aware of the XSLT FAQ website, which is
a collection of the best answers from that list:

http://www.dpawson.co.uk/xsl/sect2/sect21.html

(It's section 2.1 because Dave has several other groups of FAQs dealing
with related topics. Good resource!)

______________________________________
"... Three things see no end: A loop with exit code done wrong,
A semaphore untested, And the change that comes along. ..."
  -- "Threes" Rev 1.1 - Duane Elms / Leslie Fish
(http://www.ovff.org/pegasus/songs/threes-rev-11.html)

Re: string-utils:replace deleting search string if replacement string is an HTML element

Posted by Mike Brown <mi...@skew.org>.
Michael Giroux wrote:
> The following template attempts to convert new-lines to <br/>

Oldest FAQ in the book.

http://www.dpawson.co.uk/xsl/sect2/break.html
http://www.dpawson.co.uk/xsl/sect2/replace.html
http://skew.org/xml/stylesheets/linefeed2br/linefeed_to_br_demo_13.xsl

<xsl:template match="/">
  <xsl:call-template name="lf2br">
    <xsl:with-param name="s" select="'here is&#10;a string&#10;that contains&#10;some linefeeds'"/>
  </xsl:call-template>
</xsl:template>

<xsl:template name="lf2br">
  <xsl:param name="s"/>
  <xsl:choose>
    <xsl:when test="contains($s,'&#10;')">
      <xsl:value-of select="substring-before($s,'&#10;')"/>
      <br/>
      <xsl:call-template name="lf2br">
        <xsl:with-param name="s" select="substring-after($s,'&#10;')"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$s"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

In the future, please ask such questions on the xsl-list.
http://www.mulberrytech.com/xsl/xsl-list/

Mike

Re: string-utils:replace deleting search string if replacement string is an HTML element

Posted by ke...@us.ibm.com.
In general, you really shouldn't be attempting to insert elements as text.
You may sometimes get away with it, but it'll fail if you ever try to use
the transformation's output directly rather than going through a serialized
text representation, and of course it breaks namespace awareness.

Better solution is to build a real XML fragment, with a genuine <br/>
element at the desired location. Here's a recursive template that should do
the trick, and do so without requiring any extension functions.

CAVEAT: UNTESTED.

<xsl:template name="br-replace">
  <xsl:param name="word"/>
  <xsl:value-of select='substring-before($word,"&#xA;")'/>
  <br/>
  <xsl:call-template name="br-replace">
    <xsl:with-param name="word", select='substring-after($word,"&#xA;")'/>
  </xsl:call-template>
</xsl:template>

______________________________________
"... Three things see no end: A loop with exit code done wrong,
A semaphore untested, And the change that comes along. ..."
  -- "Threes" Rev 1.1 - Duane Elms / Leslie Fish
(http://www.ovff.org/pegasus/songs/threes-rev-11.html)