Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace line breaks with <br> in xsl?

Status
Not open for further replies.

jdbolt

Programmer
Aug 10, 2005
89
CA
Hi,

Is there a way to replace line breaks in xsl with <br> tags?

Here is my template:

Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
	<xsl:template match="/">
      	<xsl:choose>
			<xsl:when test="count(root/child) > 0">
				<xsl:for-each select="root/child">
					<xsl:sort select="announcementid" order="descending"/>
					<h2>
						<xsl:value-of select="title" />
					</h2>
					<p class="announcements">
						<xsl:value-of select="message" />
					</p>
				</xsl:for-each>
          </xsl:when>
          <xsl:otherwise>
				There are currently no announcements available for this portal
          </xsl:otherwise>
        </xsl:choose>
	</xsl:template>
</xsl:stylesheet>

I need to replace all the line breaks in 'message' with <br> tags, thanks alot!

Jon
 
Use a template for the job. Add this named template to the stylesheet.
[tt]
<xsl:template name="replace_sab">
<!-- with string s, replace substring a by string b -->
<!-- s, a and b are parameters determined upon calling -->
<xsl:param name="s" />
<xsl:param name="a" />
<xsl:param name="b" />
<xsl:choose>
<xsl:when test="contains($s,$a)">
<xsl:value-of select="substring-before($s,$a)" />
<xsl:copy-of select="$b" />
<xsl:call-template name="replace_sab">
<xsl:with-param name="s" select="substring-after($s,$a)" />
<xsl:with-param name="a" select="$a" />
<xsl:with-param name="b" select="$b" />
</xsl:call-template>
</xsl:when>
<xsl:eek:therwise>
<xsl:value-of select="$s" />
</xsl:eek:therwise>
</xsl:choose>
</xsl:template>
[/tt]
The drafting of the template is profited from this reference
-->
I've made some essential change in construction to make it much more flexible in using parameter and more concise naming.
To integrate it, modify the message section like this.
[tt]
<p class="announcements">
<xsl:call-template name="replace_sab">
<xsl:with-param name="s" select="message" />
<xsl:with-param name="a" select="'&#xA;'" />
<xsl:with-param name="b"><br /></xsl:with-param>
</xsl:call-template>
</p>
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top