Forum Moderators: open

Message Too Old, No Replies

XSL-Replacing imbedded characters in a field

XSL functions

         

dhascuba

3:27 pm on Dec 18, 2007 (gmt 0)

10+ Year Member



In the below XSL I want to test the "@name" field for a single quote in it and replace it with 2 quotes so when the SQL "insert" runs it does not bomb with, as an example
'Mike O'Grady'
Here's the existing code (if I need another Stylesheet explain how to incorporate), thanks:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" indent = "no" encoding="UTF-8"/>
<xsl:template match="pcc">
<xsl:text>insert into mampcpwrk (name,prcode,wrkph2,adrln1,zipcod,zipcod4,titlcd,provno,wrkph1,adrln2,citycd,stacod)values(</xsl:text>
&apos;<xsl:value-of select="@name"/>&apos;,
&apos;<xsl:value-of select="@cde_service_loc"/>&apos;,
&apos;<xsl:value-of select="@num_phone"/>&apos;,
&apos;<xsl:value-of select="@adr_mail_strt1"/>&apos;,
&apos;<xsl:value-of select="@adr_mail_zip"/>&apos;,
&apos;<xsl:value-of select="@adr_mail_zip_4"/>&apos;,
&apos;<xsl:value-of select="@nam_title"/>&apos;,
&apos;<xsl:value-of select="@id_provider"/>&apos;,
&apos;<xsl:value-of select="@num_area_code"/>&apos;,
&apos;<xsl:value-of select="@adr_mail_strt2"/>&apos;,
&apos;<xsl:value-of select="@adr_mail_city"/>&apos;,
&apos;<xsl:value-of select="@adr_mail_state"/>&apos;);
</xsl:template>
</xsl:stylesheet>

cmarshall

4:02 pm on Dec 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay, the issue here is whether or not you are limited to XSLT 1.0 or 2.0.

If you are able to use XSLT 2.0 [w3.org] (This is NOT the case if you use the built-in PHP 5 XSLTProcessor [us2.php.net]. If you use this, then you MUST use XSLT 1.0 [w3.org]), then you have a built-in XPath function [w3schools.com] to do this (replace()). This function is not available in XSLT 1.0. In XSLT 1.0, you have to do some real fancy footwork, doing substring-before, then substring-after, etc.

What I recommend, if you are using XSLTProcessor in PHP, is to use a PHP function callout [us2.php.net]. This is actually pretty simple, and I use it in my own work. The only drawback is that you can't debug it in something like <oXygen/> [oxygenxml.com].

httpwebwitch

2:29 pm on Jan 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I haven't used PHP callouts in a real application yet. It's one of my sad personality flaws - when I learn a new technique I immediately start looking for an excuse to use it... like that idiom "to a hammer, everything looks like a nail"...

here's a string replacement template that has served me well for a while


<xsl:template name="string-replace">
<xsl:param name="arg" />
<xsl:param name="toReplace" />
<xsl:param name="replaceWith" />
<xsl:choose>
<xsl:when test="contains($arg, $toReplace)">
<xsl:variable name="prefix" select="substring-before($arg, $toReplace)"/>
<xsl:variable name="postfix" select="substring($arg, string-length($prefix)+string-length($toReplace)+1)"/>
<xsl:value-of select="concat($prefix, $replaceWith)"/>
<xsl:call-template name="string-replace">
<xsl:with-param name="arg" select="$postfix"/>
<xsl:with-param name="toReplace" select="$toReplace"/>
<xsl:with-param name="replaceWith" select="$replaceWith"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$arg"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>