Link to home
Start Free TrialLog in
Avatar of davidlars99
davidlars99Flag for United States of America

asked on

replace apostrophe XML

Hi,

How can I replace apostrophe width \'

It has to be replaced with slash followed by apostrophe

Thanks!

<xslt:stylesheet version="1.0" xmlns:xslt="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
      <xslt:output media-type="text/html" encoding="UTF-8"/>
      <xslt:output method="html"/>
      <xslt:output method="xml" omit-xml-declaration="yes" indent="yes"/>        
        <xslt:output method="html"/>
        
      <xslt:template match="/">
            <table cellpadding="0" cellspacing="0">
                  <xslt:apply-templates select="NewDataSet/Table"/>
            </table>
      </xslt:template>
      
      <xslt:template match="NewDataSet/Table">
            <xslt:variable name="webname">
                  <xslt:value-of select="web_name"/>
            </xslt:variable>
            <xslt:variable name="lookupfield">
                  <xslt:value-of select="lookup_field"/>
            </xslt:variable>
            <xslt:variable name="defcoll">
                  <xslt:value-of select="DEFAULT_COLLECTOR"/>
            </xslt:variable>
            <xslt:variable name="empname">
                  <xslt:value-of select="EMPLOYEE_NAME"/>
            </xslt:variable>                                                                                          
            <tr>
                  <xslt:choose>
                        <xslt:when test="DEFAULT_COLLECTOR">
                              <td onmouseover="a(this)" onmouseout="b(this)" onclick="c(this,'{$defcoll}')"><xslt:value-of select="$empname"/></td>
                        </xslt:when>
                        <xslt:otherwise>
                              <xslt:choose>
                                    <xslt:when test="web_name">
                                          <td onmouseover="a(this)" onmouseout="b(this)" onclick="c(this,'{$webname}')"><xslt:value-of select="$webname"/></td>
                                    </xslt:when>
                                    <xslt:otherwise>
                                          <td onmouseover="a(this)" onmouseout="b(this)" onclick="c(this,'{$lookupfield}')"><xslt:value-of select="$lookupfield"/></td>
                                    </xslt:otherwise>                  
                              </xslt:choose>
                        </xslt:otherwise>
                  </xslt:choose>
            </tr>
      </xslt:template>
            
</xslt:stylesheet>
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Hi davidlars99,

you need to do this in a recursive way
parse through your data and do the replacement one by one

where do you want to replace the ' with \'?

Cheers!
davidlars99,

here is a template that replaces the apostrophe
As an example I called it in the element web_name

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:param name="apos"><xsl:text>'</xsl:text></xsl:param>
    <xsl:template match="/">
        <xsl:call-template name="replaceApos">
            <xsl:with-param name="strRep" select="NewDataSet/Table/web_name"/>
        </xsl:call-template>
     </xsl:template>
    <xsl:template name="replaceApos">
        <xsl:param name="strRep"/>
        <xsl:choose>
            <xsl:when test="contains($strRep, $apos)">
                <xsl:value-of select="substring-before($strRep, $apos)"/>
                <xsl:text>\'</xsl:text>
                <xsl:if test="substring-after($strRep, $apos)">
                    <xsl:call-template name="replaceApos">
                        <xsl:with-param name="strRep" select="substring-after($strRep, $apos)"/>
                    </xsl:call-template>
                </xsl:if>
            </xsl:when>
            <xsl:otherwise><xsl:value-of select="$strRep"/></xsl:otherwise>
        </xsl:choose>
       
    </xsl:template>
 </xsl:stylesheet>
Avatar of davidlars99

ASKER

Thank you Gertone. Could you please merge your template with my xsl?
somehow I can't get it to work...  :(
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
usually not tricky about points
but I am not sure why vivek should be awarded when he comes home with exactly the same solution three weeks after the facts
sorry for my lack of tolerance

Geert
Hello Gertone, it was actually the link vivekthangaswamy provided that made me recommend a split.
well, OK, no prob
Hi davidlars99

I have given you the sample XSLT code for SEARCH-AND-REPLACE, using that you can replace any character in the xml doc. For more and related topic i have given you the links.

Regards

Vivek
I appologize... I was away from the computers for a while. I don't abandon question unless there's a special reason for it  :) you can check my history on that  :)