Nested call-templates

Discussion in 'XML' started by Hvid Hat, Apr 14, 2008.

  1. Hvid Hat

    Hvid Hat Guest

    Hi

    I want to search and replace multiple words in a text. I've got a template
    that does the search and replace of a word in a text. Now, I want to call
    this template for each word in a list of words. If changes are made (words
    are replaced) in the text, how can I call the search and replace template
    with this updated text the next time? Current I'm calling it with the
    original text each time.

    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0"> <xsl:template match="WordList">
    <xsl:apply-templates select="Word"/>
    </xsl:template>
    <xsl:template match="Word">
    <xsl:call-template name="replace-string">
    <xsl:with-param name="text" select="Text"/> <!-- PROBLEM HERE -->
    <xsl:with-param name="from" select="'Word'"/>
    <xsl:with-param name="to" select="'replaced'"/>
    </xsl:call-template>
    </xsl:template>
    <xsl:template name="replace-string">
    <xsl:param name="text"/>
    <xsl:param name="from"/>
    <xsl:param name="to"/>
    <xsl:choose>
    <xsl:when test="contains($text, $from)">
    <xsl:variable name="before" select="substring-before($text, $from)"/>
    <xsl:variable name="after" select="substring-after($text, $from)"/>
    <xsl:variable name="prefix" select="concat($before, $to)"/>
    <xsl:value-of select="$before"/>
    <xsl:value-of select="$to"/>
    <xsl:call-template name="replace-string">
    <xsl:with-param name="text" select="$after"/>
    <xsl:with-param name="from" select="$from"/>
    <xsl:with-param name="to" select="$to"/>
    </xsl:call-template>
    </xsl:when>
    <xsl:eek:therwise>
    <xsl:value-of select="$text"/>
    </xsl:eek:therwise>
    </xsl:choose>
    </xsl:template>
    </xsl:stylesheet>
     
    Hvid Hat, Apr 14, 2008
    #1
    1. Advertising

  2. XSLT never modifies data in place; it always returns new data. Thus, if
    you want to call a named template on changed data, it's up to you to
    explicitly pass the changed data into that template as a parameter.

    Multiple passes to successively change an XML tree are a pain unless
    you're willing to store intermediate results into temporary trees and
    reprocess those (which in 1.0 requires the nodeset extension function).
    A better solution might be to gather all the changes you want to make,
    and do a single pass over the source which applies them.

    (If XSLT is fighing you, you may have expressed the problem in a form
    which isn't natural for XSLT.)
     
    Joseph J. Kesselman, Apr 15, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fred
    Replies:
    1
    Views:
    605
    Neredbojias
    Sep 26, 2005
  2. John Harrison

    using templates in templates

    John Harrison, Jul 31, 2003, in forum: C++
    Replies:
    8
    Views:
    386
    Torsten Curdt
    Jul 31, 2003
  3. JKop
    Replies:
    3
    Views:
    479
  4. Tom McCallum

    Templates within templates

    Tom McCallum, Aug 4, 2004, in forum: C++
    Replies:
    2
    Views:
    370
    tom_usenet
    Aug 4, 2004
  5. recover
    Replies:
    2
    Views:
    813
    recover
    Jul 25, 2006
Loading...

Share This Page