Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Nested call-templates

Reply
Thread Tools

Nested call-templates

 
 
Hvid Hat
Guest
Posts: n/a
 
      04-14-2008
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 xmlnssl="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">
<xslaram name="text"/>
<xslaram name="from"/>
<xslaram 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>
<xsltherwise>
<xsl:value-of select="$text"/>
</xsltherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
 
Reply With Quote
 
 
 
 
Joseph J. Kesselman
Guest
Posts: n/a
 
      04-15-2008
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.)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested friend class in nested template problem tonvandenheuvel@gmail.com C++ 3 12-07-2007 03:02 PM
dealing with nested xml within nested xml within...... Ultrus Python 3 07-09-2007 09:00 PM
Is nested class automatically friend of class that it is nested in? request@no_spam.com C++ 5 09-25-2006 08:31 AM
Nested Vector Nester Classes are Nested in my Brain Chad E. Dollins C++ 3 11-08-2005 04:46 AM
Nested iterators (well, not nested exactly...) Russ Perry Jr Java 2 08-20-2004 06:51 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57