RE: Problem in displaying & in XML node

Subject: RE: Problem in displaying & in XML node
From: Kay Michael <Michael.Kay@xxxxxxx>
Date: Thu, 10 Jun 1999 12:46:03 +0100

> -----Original Message-----
> From: Hari Yerram [mailto:yerram@xxxxxxxxxx]
> Sent: 09 June 1999 19:48
> To: xsl-list@xxxxxxxxxxxxxxxx
> Subject: Problem in displaying & in XML node
> 
> 
> please look the following code.
> 
>             email = "" + Request("email");
> 
>             sError = "<ERROR TYPE='Email_Info'>";
>             sError += "<Subject>";
>             sError += subject;
(etc)
> 
> The Problem is If i have '&' (ampersand) character in my XML 
> node ...

> Is there a way to resolve it?
> 
You need to write
		 sError += xmlEscape(subject);

where xmlEscape() is a function that substitutes special characters with
their escaped forms, e.g. "&" changes to "&amp;".

Here is a very simple unoptimised VBScript version (it doesn't attempt to
handle
non-ASCII characters):

Public Function xmlEscape(s As String)
    Dim i As Integer
    Dim t As String
    Dim c As String
    
    i = 1
    t = ""
    Do While i <= Len(s)
        c = Mid$(s, i, 1)
        If c = "<" Then
            t = t & "&lt;"
        ElseIf c = ">" Then
            t = t & "&gt;"
        ElseIf c = "'" Then
            t = t & "&#39;"
        ElseIf c = """" Then
            t = t & "&#34;"
        ElseIf c = "&" Then
            t = t & "&amp;"
        Else
            t = t & c
        End If
        i = i + 1
    Loop
        
    xmlEscape = t

End Function

Mike Kay


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread