[xsl] How to Replace Special Characters ?

Subject: [xsl] How to Replace Special Characters ?
From: "Rohitav Samanta" <rohitav_samanta@xxxxxxxxx>
Date: Thu, 08 Aug 2002 05:27:51 -0700
Hi All,
I want to replace the special characters (e.g double quote right) before tranforming.

My XML looks like
<WorkHistory>
<xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
	xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
	xmlns:rs='urn:schemas-microsoft-com:rowset'
	xmlns:z='#RowsetSchema'>
<s:Schema id='RowsetSchema'>
	<s:ElementType name='row' content='eltOnly'>
		
		<s:AttributeType name='Responsibilities' rs:number='14'
			 rs:writeunknown='true'>
			<s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='4000'
			 rs:maybenull='false'/>
		</s:AttributeType>
		
		<s:extends type='rs:rowbase'/>
	</s:ElementType>
</s:Schema>
<rs:data>
	
	<z:row 
		Responsibilities='Assisted Senior Systems Analyst in the design, development, and implementation of an integrated data system for the Texas Department of Health and Human Services.  Participated in liaison and &#x26;#8220;programming&#x26;#8221; &#x22; `&#x26;#8217; &#x27; activities with State officials and private-sector providers to develop policies and data interfaces required to track enrollees&#x26;#8217; health care assessment, enrollment status, third party resources, and provider history.  Responsible for the statistical analyses of State and Federal health care policies to determine their impact on Medicaid recipients and managed care system design.'
		 />
</rs:data>
</xml>
</WorkHistory>


To get rid of the special characters I have written the following template.

<xsl:call-template name="transformXMLString">
<xsl:with-param name="StringToTransform">
<xsl:value-of select="//WorkHistory/xml/rs:data/z:row@Responsibilities" disable-output-escaping="yes"/>
</xsl:with-param>
</xsl:call-template>

<!--
**********************************************************************
	Transform XML to replace special characters
**********************************************************************
-->	   
    
<xsl:template name="transformXMLString">
<!-- import $StringToTransform -->
   <xsl:param name="StringToTransform" />
  
   <xsl:choose>
<!-- string contains quote double left -->
      <xsl:when test="contains($StringToTransform,'&#x26;#8220;')">
        <xsl:value-of select="translate($StringToTransform,'&#x26;#8220;','&quot;')" />
      </xsl:when>
<!-- string contains quote double right -->
      <xsl:when test="contains($StringToTransform,'&#x26;#8221;')">
         <xsl:value-of select="translate($StringToTransform,'&#x26;#8220;','&quot;')" />
      </xsl:when>
<!-- string contains quote left -->
	  <xsl:when test="contains($StringToTransform,'&#x26;#8216;')">
         <xsl:value-of select="translate($StringToTransform,'&#x26;#8216;','&apos;)" />
      </xsl:when>
<!-- string contains quote right -->
      <xsl:when test="contains($StringToTransform,'&#x26;#8217;')">
         <xsl:value-of select="translate($StringToTransform,'&#x26;#8217;','&apos;)" />
      </xsl:when>
<!-- string contains linefeed -->
      <xsl:when test="contains($StringToTransform,'&#10;')">
         <xsl:value-of select="substring-before($StringToTransform,'&#10;')" />
         <br></br>
<!-- repeat for the remainder of the original string -->
         <xsl:call-template name="transformXMLString">
            <xsl:with-param name="StringToTransform">
               <xsl:value-of select="substring-after($StringToTransform,'&#10;')" />
            </xsl:with-param>
         </xsl:call-template>
      </xsl:when>
<!-- string contains carriage return -->
      <xsl:when test="contains($StringToTransform,'&#13;')">
         <xsl:value-of select="substring-before($StringToTransform,'&#13;')" />
         <br></br>
<!-- repeat for the remainder of the original string -->
         <xsl:call-template name="transformXMLString">
            <xsl:with-param name="StringToTransform">
               <xsl:value-of select="substring-after($StringToTransform,'&#13;')" />
            </xsl:with-param>
         </xsl:call-template>
      </xsl:when>
<!-- string does not contain any special character, so just output it -->
      <xsl:otherwise>
         <xsl:value-of select="$StringToTransform" />
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>


This template is not very efficient. It still leaves some sepcial characters.
If you have any other template or method which is more efficient  or if you know how to improve my template please let me know.

Thanks
Rohit


















__________________________________________________________
Win a First Class Trip to Hawaii to Vacation Elvis Style!
http://r.lycos.com/r/sagel_mail/http://www.elvis.lycos.com/sweepstakes

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread