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>
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>
ASKER
That handles the quotes but not the apostrophes
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
DOM is overkill for a simple app like this. I would just make a function with the following,
replace(
replace(
replace(
replace(
replace(text,"'","'")
,"""",""")
,"<","<")
,"&","&")
,">",">")
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.
replace(
replace(
replace(
replace(
replace(text,"'","'")
,"""",""")
,"<","<")
,"&","&")
,">",">")
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.
sXML = "<?xml version=""1.0""?>"
sXML = sXML & "<root>"
sXML = sXML & "<question QuestionID=""" & SErver.HtmlEncode(iQuestio
sXML = sXML & "</root>"
Response.Expires = -1500
Response.CacheControl = "no-cache"
Response.ContentType = "text/xml"
Response.Write(sXML)
%>