xsl-list
[Top] [All Lists]

Re: [xsl] String Replacement in XML using XSLT

2006-09-25 12:50:32
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


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--