[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: String Replacement in XML using XSLT

Subject: Re: String Replacement in XML using XSLT
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Mon, 25 Sep 2006 20:41:43 +0200
xslt insert string
Dimitre Novatchev wrote:
Or I could make all replacements using a single template only:

<content name="subject"> Hello <insert>BUYERS_NAME</insert></content>

<xsl:template match="insert">
 <xsl:value-of select="/*/*[name() = current()]">
</xsl:template>


Nice thinking, I should use that myself (I tend to invent new tags and constructs every now and then, like {{REPLACEME}} :D ). Though I wonder if Senthil really has the choice of changing his input data. Of course, only Senthil could answer that Q.


In addition, you (Senthil) may use the best of breed, if you can use XSLT 2, or XSLT 1 with node-set extension (not tested):

<xsl:variable name="content-tree">
   <xsl:for-each select="//content">
      <xsl:copy>
       <xsl:for-each select="tokenize(., '\[|\]')">
           <xsl:if test="position() mod 2">
               <text><xsl:value-of select="." /></text>
           </xsl:if>
           <xsl:if test="not(position() mod 2)">
               <insert><xsl:value-of select="." /></insert>
           </xsl:if>
       </xsl:for-each>
     </xsl:copy>
   </xsl:for-each>
</xsl:variable>


This will give you a tree that looks something like this (if more [bla] pairs are inside a string, it goes well as well, I think):


<content name="subject">
   <text>Hello </text>
   <insert>BUYERS_NAME</insert>
</content>
<content name="text">
   <text>REF Order </text>
   <insert>ORDER_NUMBER</insert>
</content>

This variable $content-tree can now be used anywhere with the template from Dimitre. I use similar techniques to tokenize CSV or likewise delimited input. For XSLT-1 remember to use the exslt:node-set extension

Cheers,
Abel Braaksma
http://abelleba.metacarpus.com

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.