[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Problem using translate to escape quotes

Subject: RE: Problem using translate to escape quotes
From: "Yates, Danny (ANTS)" <danny.yates@xxxxxxxxxx>
Date: Wed, 4 Dec 2002 16:27:00 -0000
translate t
Kevin Collins wrote:

> <xsl:variable name="backSlashQuote">&#92;&#39;</xsl:variable>

What's wrong with using this?
  <xsl:variable name="backSlashQuote">\'</xsl:variable>

> <xsl:variable name="backSlash">&#92;</xsl:variable>

What's wrong with using this?
  <xsl:variable name="backSlash">\</xsl:variable>

> <xsl:variable name="singleQuote">&#39;</xsl:variable>

What's wrong with using this?
  <xsl:variable name="singleQuote">'</xsl:variable>

>  <xsl:value-of select="translate( $mystr, $singleQuote,
$backSlashQuote)"/>

This is doing exactly what it's supposed to. Check the specs.

translate() translates single characters for other single characters
according to their position in the two strings,

e.g. translate("Hello World", "HW", "hw")

would transpose H->h and W->w (note their relative positions in the two
strings), giving "hello world".

So, translate() will not do what you want - that is, it won't allow you
to replace a single character (') with two characters (\'). The solution
will need to involve the substring functions, or one of Dimitre's FXSL
functions.

I'm sure this is an FAQ - check Dave Pawson's FAQ.

Dan.

-- 
Danny Yates
Technical Architect
Abbey National Treasury Services
E-mail: Danny.Yates@xxxxxxxxxx
Phone: +44 20 7756 5012
Fax: +44 20 7612 4342


-----Original Message-----
From: Kevin Collins [mailto:kcollins@xxxxxxx]
Sent: 04 December 2002 15:43
To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
Subject:  Problem using translate to escape quotes


I have a string containing single quotes held in a variable. I want to
escape each quote by inserting a backslash before it, but I can't get it
to work. The translate function appears to be inserting the backslashes
without the quotes.

Here's a test version of the stylesheet:
--------------------------------------------
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html"/>

<xsl:variable name="mystr">this is Kevin's 'string'</xsl:variable>

<xsl:variable name="backSlashQuote">&#92;&#39;</xsl:variable>
<xsl:variable name="backSlash">&#92;</xsl:variable>
<xsl:variable name="singleQuote">&#39;</xsl:variable>
<xsl:variable name="backSlashQuote2"><xsl:value-of select="concat(
$backSlash, $singleQuote )"/></xsl:variable>

<xsl:variable name="mystr_escaped">
 <xsl:value-of select="translate( $mystr, $singleQuote, $backSlashQuote
)"/>
</xsl:variable>

<xsl:template match="/">
<html>
 <body>
  <xsl:value-of select="$mystr"/>
  <br/>
  <xsl:value-of select="$mystr_escaped"/>
 </body>
</html>
</xsl:template>

</xsl:stylesheet>
--------------------------------------------

Test source document (any one will do): 
--------------------------------------------
<?xml version="1.0" encoding="iso-8859-1"?>
<mynode></mynode>
--------------------------------------------

I get the same result regardless of whether I use $backSlashQuote or
$backSlashQuote2 in the translate function. Here's the output I'm
getting:
--------------------------------------------

<html>
<body>this is Kevin's 'string'<br>this is Kevin\s \string\</body>
</html>

--------------------------------------------

I'm using MSXML 4.0.

Thanks for any help with this!
Kevin Collins


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


***************************************************************************
This communication (including any attachments) contains confidential information.  If you are not the intended recipient and you have received this communication in error, you should destroy it without copying, disclosing or otherwise using its contents.  Please notify the sender immediately of the error.

Internet communications are not necessarily secure and may be intercepted or changed after they are sent.  Abbey National Treasury Services plc does not accept liability for any loss you may suffer as a result of interception or any liability for such changes.  If you wish to confirm the origin or content of this communication, please contact the sender by using an alternative means of communication.

This communication does not create or modify any contract and, unless otherwise stated, is not intended to be contractually binding.

Abbey National Treasury Services plc. Registered Office:  Abbey National House, 2 Triton Square, Regents Place, London NW1 3AN.  Registered in England under Company Registration Number: 2338548.  Regulated by the Financial Services Authority (FSA).
***************************************************************************


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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.