tags:

views:

166

answers:

3

Hello, I have a problem with double quotes in classic ASP.

I want to replace double quotes " in a string. The string contains xml and I don't want to replace double quotes (for attributes) inside tags.

So if I wanted to replace double quotes with single quotes, I'd want my string to go from this:

<MyDinosaurDocument DocType="Brachysaurus">"Hello" said the little dinosaur</MyDinosaurDocument>

to this:

<MyDinosaurDocument DocType="Brachysaurus">'Hello' said the little dinosaur</MyDinosaurDocument>

I've tried using regular expressions and would like to fix this problem with them -- but I'm sadly out of my depth.

All and any help is greatly appreciated.

A: 

Use &quot; to escape quotes in XML.

Gerrie Schenck
A: 

As always when dealing with non-regular data, the answer is not to use a regular expression. Really, don’t. XML and HTML should always be parsed by appropriate parsers and furthermore ASP gives you the means to do this easily. Using regular expressions here is a serious security liability.

Konrad Rudolph
+4  A: 

I wouldn't use Regex to solve this problem. Here is a simple chunk of code that would do it:-

Dim dom : Set dom = CreateObject("MSXML2.DOMDocument.3.0")

dom.LoadXml myXMLString

Dim node
For Each node in dom.SelectNodes("//*/text()")
    node.nodeValue = Replace(node.nodeValue, """", "'")
Next

myXMLString = dom.xml

Of course you probably at some point need to load the XML into a DOM anyway so once that is done there is no need to read the string back out.

AnthonyWJones
Gerrie and Konrad, thanks for your replies. Anthony that does exactly what I want - pertect, thanks!
adrianos
@adrianos: well, accept his answer, then! (tick mark next to the question, and don’t forget to upvote it, too)
Konrad Rudolph
ah sorry - hope that's sorted now (can you tell it's my first time on here?)
adrianos