Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 26th, 2007, 11:30 AM
Authorized User
 
Join Date: Feb 2007
Posts: 51
Thanks: 3
Thanked 0 Times in 0 Posts
Default String Replace Template w/out Outputting

Is there any way to perform a global substring replace within a template in such a manner that you are capturing the replaced context as part of a new string, variable, etc rather than simply outputting the new string as its being parsed?

All string-replace template examples I find require spitting the parsed chunks to the output stream as they are identified. I need to be able to do it in such a way that I preserve the new string intact BEFORE it is output. Aren't there some tricks or something for doing this??? Thanks

 
Old September 26th, 2007, 11:43 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Yes, you can capture the output of a template in a variable:

<xsl:variable name="temp">
  <xsl:call-template name="string-replace">
    <xsl:with-param...
  </xsl:call-template>
</xsl:variable>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 26th, 2007, 11:49 AM
Authorized User
 
Join Date: Feb 2007
Posts: 51
Thanks: 3
Thanked 0 Times in 0 Posts
Default

what in the template populates the variable? Would it be from value-of element? For instance, what I see are templates like (below), where the value-of streams to the output as the string is parsed. If I wrapper the (below) in a variable definition, will the value-of output build the variable string as opposed to streaming to output?

<xsl:template name="globalReplace">
        <xsl:param name="outputString"/>
        <xsl:param name="target"/>
        <xsl:param name="replacement"/>
        <xsl:choose>
            <xsl:when test="contains($outputString,$target)">
                <xsl:value-of select="concat(substring-before($outputString,$target),
               $replacement)"/>
                <xsl:call-template name="globalReplace">
                    <xsl:with-param name="outputString" select="substring-after($outputString,$target)"/>
                    <xsl:with-param name="target" select="$target"/>
                    <xsl:with-param name="replacement" select="$replacement"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$outputString"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>


 
Old September 26th, 2007, 12:12 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The xsl:variable (with content instructions) creates the root of a temporary tree, and all instructions within its dynamic scope that create nodes then write those nodes to the temporary tree instead of to the final output tree. That includes xsl:value-of, which writes a text node.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 26th, 2007, 03:38 PM
Authorized User
 
Join Date: Feb 2007
Posts: 51
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Thanks! That is a new wrinkle to my understanding of xslt. Now things work as I had hoped. Many Thanks!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple String Replace berna_isct XSLT 5 May 20th, 2008 02:47 AM
Help in instruction string-replace hugoscp XML 9 October 20th, 2006 12:07 PM
Replace string with a control shenku ASP.NET 1.0 and 1.1 Professional 3 August 22nd, 2005 09:20 AM
replace string value str33ts XSLT 1 April 22nd, 2005 04:01 AM
how to replace a string with another string/number crmpicco Javascript How-To 4 March 14th, 2005 12:59 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.