Re: [xsl] replace a character in an element

Subject: Re: [xsl] replace a character in an element
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Tue, 19 Jul 2005 21:50:54 +1000
On 7/19/05, Dariusz Borowski <d.borowski@xxxxxx> wrote:
>
> Hi!
>
> I would like to replace a character in a string. Because it is a quote and
mySQL-Database doesn't accept it, but I don't know how.
>
> Here is the string-element:
> ======================
> <description-list>
> <paragraph>
> The term "project plan" is used throughout the generic and specific
practices in this process area to refer to the overall plan for controlling
the project.
> </paragraph>
> </description-list>
> ======================
>
>
> At first I tried to find the element with the quotes as follows:
> ======================
> <xsl:when test="contains(., '&quot;')">
> <!-- <div class="red"><xsl:value-of select="." /></div><br/><br/>-->
> </xsl:when>
>
> <xsl:otherwise>
> "<xsl:value-of select="." />");<br/><br/>
> </xsl:otherwise>
> ======================
>
> This works, but now I would like to get an output like this:
> ======================
> The term \"project plan\" is used throughout the generic and specific
practices in this process area to refer to the overall plan for controlling
the project.
> ======================
>
> I appreciate for any help!
>

It takes 1-2 minutes to produce a solution using FXSL. This transformation:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:testmap="testmap"
exclude-result-prefixes="xsl testmap"
>
   <xsl:import href="str-dvc-map.xsl"/>

   <testmap:testmap/>

   <xsl:output omit-xml-declaration="yes" indent="yes"/>

   <xsl:template match="/">
     <xsl:variable name="vTestMap" select="document('')/*/testmap:*[1]"/>
     <xsl:call-template name="str-map">
       <xsl:with-param name="pFun" select="$vTestMap"/>
       <xsl:with-param name="pStr" select="string(/*)"/>
     </xsl:call-template>
   </xsl:template>

    <xsl:template name="escapeQuoteNL" match="testmap:*">
      <xsl:param name="arg1"/>

      <xsl:if test="$arg1='&quot;'">
        <xsl:value-of select="'\'"/>
      </xsl:if>
      <xsl:value-of select="$arg1"/>
    </xsl:template>

</xsl:stylesheet>

when applied on this xml document:

<description-list>
	<paragraph>
The term "project plan" is used throughout the generic and specific
practices in this process area to refer to the overall plan for
controlling the project.
	</paragraph>
</description-list>

produces the wanted result:

The term \"project plan\" is used throughout the generic and specific
practices in this process area to refer to the overall plan for
controlling the project.



Cheers,
Dimitre Novatchev.

Current Thread