?
Solved

Converting quotes and apostrophes in XML (250 points)

Posted on 2003-03-23
4
Medium Priority
?
1,229 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

762 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