hi Greg,

you need a template that matches all text nodes, which tests for an occurance 
of '%' and call the recursive named template if there is one: 

<xsl:template match="text()">
  <xsl:choose>
    <xsl:when test="contains(.,'%')">
      <xsl:call-template name="convertPercent">
        <xsl:with-param name="str" select="."/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="."/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

Then the named template recursively processes the string converting everything 
after a % and before a space into a <field key=..."/> element.  Its a good 
technique to know as it comes up time and again in xslt 1.0.

<xsl:template name="convertPercent">
  <xsl:param name="str" select="'error'"/>
  <xsl:choose>
    <xsl:when test="contains($str,'%')">
      <xsl:value-of select="substring-before($str,'%')"/>
      <field key="{substring-before(substring-after($str,'%'),' ')}"/>
      <xsl:call-template name="convertPercent">
         <xsl:with-param name="str" 
select="substring-after(substring-after($str,'%'),' ')"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="."/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

You may have to modify the tests slightly to include the full stop in your 
output after %name2, as currently that would get dropped as its before the 
first occurance of a space, but it isnt so hard.  If you get stuck post back, 
or checkout Demtire's tokenize templates in fxsl, or I think exslt's tokenise 
function can handle multiple delimiters.

cheers
andrew




Hi all,

My input document contains lines that have multiple 'tokens' that need 
replacing with xml nodes.  Tokens are identified by a percent sign.  An example 
of a input line with tokens looks like the following:

"Welcome %name1 %name2.  You have received a bonus of %bonus points.  Your 
points expire on %expire365"

It would be converted to :

<line>Welcome <field key="name1"/> <field key="name2"/>.  You have received a 
bonus of <field key="bonus"/> points.  Your points expire on <field 
key="expiry" param="365"/></line>

Not being an XSL guru, I can't seem to find any way of successively performing 
multiple operation on a string in this fashion.  That is, replacing all tokens 
of like, say, %name1, and then doing %name2 and so on.

With no 'variables' this seems a little awkward.  Can anyone provide an example 
or some hints on how I might replace a number of different tokens in a string?

Many thanks.

Greg.


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.467 / Virus Database: 266 - Release Date: 01/04/2003
 

Reply via email to