Link to home
Start Free TrialLog in
Avatar of makman111
makman111

asked on

Converting quotes and apostrophes in XML (250 points)

I have a XML document that might contain control charecters like qoutes and apostrophes in the attributes section of a element.  How do I convert to them to be XML friendly, so that the XSL doc can render them correctly?

Example (notice the apostrophe in staff)

<%
sXML = "<?xml version=""1.0""?>"
sXML = sXML & "<root>"
sXML = sXML & "<question QuestionID=""" & iQuestionID & """ text=""" & sText & """ />"
sXML = sXML & "</root>"
     
Response.Expires = -1500
Response.CacheControl = "no-cache"
Response.ContentType = "text/xml"
Response.Write(sXML)
%>

Renders...

<?xml version="1.0"?>
<root>
  <question QuestionID="7334" text="Delegates to develop staff's abillity" />
</root>




Avatar of MMeijer
MMeijer

<%
sXML = "<?xml version=""1.0""?>"
sXML = sXML & "<root>"
sXML = sXML & "<question QuestionID=""" & SErver.HtmlEncode(iQuestionID) & """ text=""" & SErver.HtmlEncode(sText) & """ />"
sXML = sXML & "</root>"
   
Response.Expires = -1500
Response.CacheControl = "no-cache"
Response.ContentType = "text/xml"
Response.Write(sXML)
%>
Avatar of makman111

ASKER

That handles the quotes but not the apostrophes
ASKER CERTIFIED SOLUTION
Avatar of LCP
LCP

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
DOM is overkill for a simple app like this. I would just make a function with the following,

replace(
 replace(
  replace(
   replace(
    replace(text,"'","&apos;")
   ,"""","&quot;")
  ,"<","&lt;")
 ,"&","&amp;")
,">","&gt;")

And call it for each time, as toHtml(iQuestionID) or such. It's faster than DOM, for one.

If you want to change them according to a doctype's entities, then you would definitely wish to use DOM, however.