View comments | RSS feed

XmlFormat

Description

Escapes special XML characters in a string so that the string can be used as text in XML.

Returns

A copy of the string parameter that is safe to use as text in XML.

Category

String functions, XML functions

Function syntax

XmlFormat(string)

See also

cfxml, XmlNew, XmlParse, XmlValidate; Using XML and WDDX in ColdFusion MX Developer's Guide

History

ColdFusion MX: Added this function.

Parameters

Parameter Description

string

A string or a variable that contains one

Usage

This function escapes characters as follows:

Text character Escaped representation

Greater than symbol (>)

>

Less than symbol (<)

&lt;

Single-quotation mark (')

&apos;

Double-quotation mark (")

&quot;

Ampersand symbol (&)

&amp;

Carriage return (but not line feed)

Removed from the text.

High ASCII characters in the range 128-255.

Replaced by unicode escape sequence; for example, É (capital E with an Acute symbol) is replaced by &#xc9;.

Example

The following example shows how XmlFormat escapes special XML characters. Use the View Source command in the browser to see the results. ColdFusion interprets the "" in the second text string as representing a single-quotation mark in text before it applies the XmlFormat function.

<?xml version = "1.0"?> 
<cfoutput>
<someXML>
   <someElement someAttribute="#XmlFormat("'a quoted value'")#"> 
      #XmlFormat("Body of element with <, >, "" and & goes here.")#
   </someElement>
</someXML>
</cfoutput>

ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | KnowledgeBase | Bug Reporting

Version 7

Comments


hpottash said on Oct 26, 2005 at 8:54 AM :
Though now in mx7 XMLFormat does cover the "high ascii characters" it still allows non-ascii characters right through, unaltered. since, in the particular system that I am working on non-ascii characters grind everything to a halt I have found it prudent to use the following in place of a regular XMLFormat() call...
XMLFormat(URLDecode(URLEncodedFormat(myData,"us-ascii"),"us-ascii"))

This will cause the non-ascii characters to be replaced with ? which, while not pretty, will also not cause anything to crash.
turtle7 said on Sep 5, 2006 at 4:09 PM :
xmlFormat does not drop or escape the collowing ascii control characters (decimal codes) : 1, 2, 3, 4, 5, 6, 7, 8, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31

You will need to remove these via some other method if your data contains them and you wish to create a valid xml object from that data.
BlueSpline said on Nov 20, 2006 at 2:07 AM :
If the number of contiguous single quotes is odd XMLFormat adds one more when converting to the escaped representation so fred's becomes fred&apos;&apos;s as does fred''s - two single quotes, not a double quote. Similarly, fred'''s becomes fred&apos;&apos;&apos;&apos;s as does fred''''s.

This doesn't seem to happen with the other four 7-bit characters, so I guess it is an unintended feature.

Mike

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/7/htmldocs/00000668.htm