?
Solved

Converting quotes and apostrophes in XML (250 points)

Posted on 2003-03-23
4
Medium Priority
?
1,236 Views
Last Modified: 2008-03-04
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>




0
Comment
Question by:makman111
4 Comments
 
LVL 5

Expert Comment

by:MMeijer
ID: 8192176
<%
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)
%>
0
 

Author Comment

by:makman111
ID: 8192364
That handles the quotes but not the apostrophes
0
 
LVL 1

Accepted Solution

by:
LCP earned 750 total points
ID: 8193529
I suggest you use MSXML DOMDocument to create the XML file.
There are some more reserved words in XML, you could solve apostrophes now, but not "<","&",etc in the future...


    Dim oDom, nod, attr
    set oDom=server.createObject("MSXML2.DOMDocument")
    oDom.appendChild oDom.createElement("ROOT")
    Set nod = oDom.createElement("Question")
    Set attr = oDom.createAttribute("QuestionID")
    attr.Text = "1"
    nod.Attributes.setNamedItem attr
    Set attr = oDom.createAttribute("Text")
    attr.Text = "Delegates to develop staff's abillity"
    nod.Attributes.setNamedItem attr
    oDom.firstChild.appendChild nod

    Response.Expires = -1500
    Response.CacheControl = "no-cache"
    Response.ContentType = "text/xml"
    Response.Write(oDom.XML)

    set oDom=nothing


0
 
LVL 1

Expert Comment

by:foxyshadis
ID: 8228794
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.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Screencast - Getting to Know the Pipeline
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…

579 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question