Re: [xsl] String Replacement in XML using XSLT

Subject: Re: [xsl] String Replacement in XML using XSLT
From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Mon, 25 Sep 2006 23:09:53 +0530
Hi Senthil,
  Please try this stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:output method="html" indent="yes" />

<xsl:template match="/test">
 <html>
    <body>
       <xsl:apply-templates select="content_vars/content" />
    </body>
 </html>
</xsl:template>

<xsl:template match="content">
  <p>
    <xsl:variable name="temp1" select="translate(., '[]', '')" />
    <xsl:variable name="temp2"
select="normalize-space(../following-sibling::*[contains($temp1,
local-name())])" />
    <xsl:variable name="temp3"
select="local-name(../following-sibling::*[contains($temp1,
local-name())])" />
    <xsl:value-of select="substring-before($temp1, $temp3)"
/><xsl:value-of select="$temp2" /><xsl:value-of
select="substring-after($temp1, $temp3)" />
  </p>
</xsl:template>

</xsl:stylesheet>

This when applied to XML:

<?xml version="1.0" encoding="UTF-8"?>
<test>
 <content_vars>
  <content name="subject">Hello [[BUYERS_NAME]]</content>
  <content name="text">REF Order [ORDER_NUMBER]</content>
 </content_vars>
 <BUYERS_NAME>
   SENTHIL
 </BUYERS_NAME>
 <ORDER_NUMBER>
   1234
 </ORDER_NUMBER >
</test>

Produces the wanted output:

<html>
 <body>
   <p>Hello SENTHIL</p>
   <p>REF Order 1234</p>
 </body>
</html>

This may not be the best answer, but it works for this case.

On 9/25/06, Senthilkumaravelan Krishnanatham <senthil@xxxxxxxxx> wrote:
Hi All,
I need help in logic of  replacing the string in XML for the below
input.
<test>
<content_vars>
<content name="subject"> Hello [[BUYERS_NAME]]</content>
<content name="text">  REF Order [ORDER_NUMBER]</content>
</content_vars>
<BUYERS_NAME>
SENTHIL
</BUYERS_NAME>
< ORDER_NUMBER >
1234
</ORDER_NUMBER >

</test>

Output should be
<html>
<body>
<P>Hello SENTHIL</p>
<p> REF Order 1234</p>
</body>
</html>

Is there anyway I can do it in XSLT?
Any suggestion are welcome.

Thanks,
Senthil


--
Regards,
Mukul Gandhi

http://gandhimukul.tripod.com

Current Thread