Click to See Complete Forum and Search --> : replacing string


caesarkim
February 1st, 2004, 07:23 PM
I have a xml file like this.

<projectteam>
<projectname>The Killer Application {a href="http://www.hotmail.com"}hotmail.com{/a}</projectname>
</projectteam>

but i want to convert '}' to '>' and '{' to '<' with xsl this function.
<xsl:template name="string-replace" >
<xsl:param name="string"/>
<xsl:param name="from"/>
<xsl:param name="to"/>
<xsl:choose>
<xsl:when test="contains($string,$from)">
<xsl:value-of select="substring-before($string,$from)"/>
<xsl:value-of select="$to"/>
<xsl:call-template name="string-replace">
<xsl:with-param name="string" select="substring-after($string,$from)"/>
<xsl:with-param name="from" select="$from"/>
<xsl:with-param name="to" select="$to"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string" disable-output-escaping="yes"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>


cuz i might have to generate html and other format. but when i try like this, it converts only '>' to a&gt; which is html tag. but i want '<'. is there any way to do this? please help...

khp
February 1st, 2004, 07:57 PM
You can never output a lone < or > character in an xml file. You could set the output content type to text, in which case you should get the result you want. But this is a bad idear if you are using the stylesheet in an application that expects xml/html output.

But you are really, looking at this the wrong way, trying to produce html elements by outputting their string encoding. Thats not the way to go.

What you should do is create the anchor using an xsl:element, something like this.

<xsl:element name="a">
<xsl:attribute name="href">
<xsl:text>www.w3c.org</xsl:text>
</xsl:attribute>
<xsl:text>w3c</xsl:text>
</xsl:element>

caesarkim
February 1st, 2004, 08:08 PM
I understand that you are saying. but i have to generate it for html and other format(XSL-FO)that generates PDF.

The problem is that i don't know where the anchor tag is going to be embedded in xml. it comes from a database table. that's why i want to replace '{' with '<' something like this.

is there another solution?

khp
February 1st, 2004, 08:17 PM
Originally posted by caesarkim
I understand that you are saying. but i have to generate it for html and other format(XSL-FO)that generates PDF.


That makes no difference.

Originally posted by caesarkim
The problem is that i don't know where the anchor tag is going to be embedded in xml. it comes from a database table.


If you know where to put the < character you must also know where to put the element.

Originally posted by caesarkim
that's why i want to replace '{' with '<' something like this.


You are still looking at it the wrong way.

Originally posted by caesarkim
is there another solution?

No.

caesarkim
February 1st, 2004, 08:19 PM
i don't know where the '<' is going to show up...

khp
February 1st, 2004, 08:22 PM
Yes you do, otherwise you would not be able to put anything there.