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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can you replace text in an XSL?

Status
Not open for further replies.

mwolf00

Programmer
Nov 5, 2001
4,177
US
I'm working with a COTS product that uses an XSL page for some of its output. We are trying to add line feeds to the text that is output by
Code:
 <TD align="center">
					   <xsl:attribute name="headers">idFolderName<xsl:value-of select="position()"/></xsl:attribute>
					   [blue]<xsl:value-of select="@eAlertMessage"/>[/blue]
					 </TD>                
         </TR>
We cannot figure out where line feeds are stripped from the text that is entered. We are considering adding a special character to the text and replacing it with a line feed in the XSL.

I know very little about XML and XSL - I would normally do this with a javascript function on the server-side but I'm not sure how to do this if it is even possible...

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
It is unclear from your description from where the line feeds (whitespace) are missing. You want a line feed in the value of a <TD> element? But the browser will ignore that...

What are you trying to accomplish?

Tom Morrison
 
Here's where I am so far - I can call the function and get the results for 'ABCZ' - but I can't figure out how to replace it with a carriage return...
Code:
<TD align="center">
	<xsl:attribute name="headers">idFolderName<xsl:value-of select="position()"/></xsl:attribute>
	<xsl:variable name="aMess" select="@eAlertMessage"/>

	<xsl:call-template name="text.replace">
		<xsl:with-param name="texttosearch" select="$aMess"/>
		<xsl:with-param name="texttofind" select="'|'"/>
		[blue]<xsl:with-param name="texttoinsert" select="'ABCZ'"/>[/blue]
	</xsl:call-template>
</TD>    


<!--
        TEMPLATE        text.replace
        PURPOSE         Replace occurrences of text within a text value with new text
        AUTHOR          Tony Rabun <trabun@yahoo.com>
        
        DATE            10 SEP 2003
        
        USAGE           
        
            <xsl:call-template name="text.replace"
                <xsl:with-param name="texttosearch" select="your-xpath-here"/>
                <xsl:with-param name="texttofind" select="your-xpath-here"/>
                <xsl:with-param name="texttoinsert" select="your-xpath-here"/>
            </xsl:call-template>
            
        PARAMS
            
            texttosearch    TEXT    Text to be searched
            texttofind      TEXT    Text to find
            texttoinsert    TEXT    Text to insert instead of $texttofind
            
        NOTES           Recursive
        
    -->
  
  <xsl:template name="text.replace">
    
        <xsl:param name="texttosearch"/>
        <xsl:param name="texttofind"/>
        <xsl:param name="texttoinsert"/>

        <xsl:variable name="textlength" select="string-length($texttosearch)"/>
        
        <!-- don't waste time if no text supplied or remaining from recursion-->
        
        <xsl:if test="$textlength &gt; 0">


            <xsl:choose>

                <xsl:when test="contains($texttosearch,$texttofind)">

                    <xsl:variable name="linebeforefirstinstance" select="substring-before($texttosearch,$texttofind)"/>

                    <xsl:variable name="lineafterfirstinstance" select="substring-after($texttosearch,$texttofind)"/>


                    <!-- if there is any text to print (i.e. text before the first occurrence) print it with the replacement text afterwards -->
                    <xsl:if test="string-length($linebeforefirstinstance) &gt; 0">
                        <xsl:value-of select="$linebeforefirstinstance"/>
                    </xsl:if>
                    <xsl:value-of select="$texttoinsert"/>
                    
                    <!-- if any is left, recurse-->
                    <xsl:if test="string-length($lineafterfirstinstance) &gt; 0">
                        <xsl:call-template name="text.replace">
                            <xsl:with-param name="texttosearch" select="$lineafterfirstinstance"/>
                            <xsl:with-param name="texttofind" select="$texttofind"/>
                            <xsl:with-param name="texttoinsert" select="$texttoinsert"/>
                        </xsl:call-template>
                    </xsl:if>
        
                </xsl:when>
                
                <xsl:otherwise>
                    <!-- nothing left to do except print remainder-->
                    <xsl:value-of select="$texttosearch"/>
                </xsl:otherwise>   
            
            </xsl:choose>
            
        </xsl:if>
        
    </xsl:template>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
Here's the final solution I worked out...

Code:
<xsl:template name="text.replace">
	<xsl:param name="texttosearch"/>
	<xsl:param name="texttofind"/>
	<xsl:variable name="textlength" select="string-length($texttosearch)"/>

	<xsl:if test="$textlength &gt; 0">
		<xsl:choose>
			<xsl:when test="contains($texttosearch,$texttofind)">
				<xsl:value-of select="substring-before($texttosearch,$texttofind)" /> 
				<br /> 
				<xsl:call-template name="text.replace">
				<xsl:with-param name="texttosearch" select="substring-after($texttosearch,$texttofind)" /> 
				<xsl:with-param name="texttofind" select="$texttofind"/>
				</xsl:call-template>
			</xsl:when>
		
			<xsl:otherwise>
				<!-- nothing left to do except print remainder-->
				<xsl:value-of select="$texttosearch"/>
			</xsl:otherwise>   
		</xsl:choose>
	</xsl:if>
</xsl:template>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top