[xsl] Trouble understanding how to use replace()

Subject: [xsl] Trouble understanding how to use replace()
From: cknell@xxxxxxxxxx
Date: Fri, 06 Jan 2006 10:55:20 -0500
Once upon a time I was adept with perl and handy with regular expressions. It has been some years since I worked with perl and my regex skills have rusted.

I have a document I'm transforming to CSV in order to import its contents to a database. The file contains text including double and single quotes and commas. The instructions for preparing the CSV file direct me to enclose any strings containing commas in quotation marks. Some of my text includes lists of items enclosed in quotation marks and separated by commas. I have no problem with using the <xsl:text> element to surround the whole string in double-quotes, but that leaves some troublesome double-quotes in the original string that cause the export field to break up.

So I thought the best approach would be to use the new XPath 2.0 "replace()" function to replace all the double-quotes in the original string with single-quotes. This is the construct I tried that's giving me a problem,

<xsl:value-of select="replace(req-text,"\"","'")" />

Among that nest of short strokes is double-quote, backslash, double-quote, double-quote, and just after the second comma I've got double-quote, single-quote, double-quote.

As I understand the function, it takes three arguments: input, a regular expression, and the text to replace the matched expression. 

The first argument I gave it is the name of an element from the current context. If I simply use select="req-text" I get the text of the element, so there is no issue with this not containing a string.

The second argument is a double-quote, which is what I want to replace, and the third agrument is a single-quote, the character I want to substitute for the double-quote. In perl I would use the backslash to escape the double-quote inside the double-quotes of the second argument. Apparently this is not legal in XPath 2.0.

I've been perusing Michael Kay's "XPath 2.0", but I can't find what must be in there, an explanation of how to do this.

Any insight will be gratefully received.

-- 
Charles Knell
cknell@xxxxxxxxxx - email

Current Thread