Skip to main content
Participant
November 12, 2009
Question

Strange behaviour of & in XML

  • November 12, 2009
  • 3 replies
  • 1034 views

Hi,

I am using CF 8 with IIS.

Following is my code.

<cfscript>

XMLpacket = XmlNew();

XMLpacket.xmlRoot = XmlElemNew(XMLpacket, "test");

XMLpacket.xmlRoot.XmlChildren[1]   = XmlElemNew(XMLpacket, "spcial_name");

XMLpacket.xmlRoot.XmlChildren[1].XmlText = "this is a test for & and &apos;');

</cfscript>

when retrieving value for spcial_name, it returns following string.

"this is a test for & and &apos;"

I understand why it converted & to &, but it also converts &apos; to &apos;

Is this a bug or should I try something else?

Please suggest.

Nishant.

    This topic has been closed for replies.

    3 replies

    BKBK
    Community Expert
    Community Expert
    November 12, 2009
    Is this a bug or should I try something else?

    It isn't a bug. There is no need to use & or &apos; in the right-hand side of the statement

    XMLpacket.xmlRoot.XmlChildren[1].XmlText = "this is a test for & and &apos;";

    Character entity references like & or &apos;are meant to be used within an XML document. Whereas, the above statement is simply an assignment to a string variable, and so can be written as

    XMLpacket.xmlRoot.XmlChildren[1].XmlText = "this is a test for & and ' ";

    ngpanchalAuthor
    Participant
    November 13, 2009

    Hi,

    But, when I use following statement, it automatically returns &apos;.

    XMLpacket.xmlRoot.XmlChildren[1].XmlText = XmlFormat("This is Nishant's test");

    <cfoutput>#XMLpacket#</cfoutput>

    The output is: This is Nishant&apos;s test

    I need to use XmlFormat() to escape other special characters.

    My problem is special character like Ó, and other characters like that. In case of Ó, it returns &#xd3;, but displays correct Ó in HTML.

    Where as in case of aprostophe('), it returns &apos; and displays &apos; instead of aprostophe.

    I think, I need to use XmlCdata instead of XmlText.

    Thank you.

    Nishant

    Inspiring
    November 12, 2009
    Following is my code.

    <cfscript>

    XMLpacket = XmlNew();

    XMLpacket.xmlRoot = XmlElemNew(XMLpacket, "test");

    XMLpacket.xmlRoot.XmlChildren[1]   = XmlElemNew(XMLpacket, "spcial_name");

    XMLpacket.xmlRoot.XmlChildren[1].XmlText = "this is a test for & and &apos;');

    </cfscript>

    when retrieving value for spcial_name, it returns following string.

    "this is a test for & and &apos;"

    I see no reference to special_name in your code above.

    However other than that, once I fix your syntax error, my results are exactly what I'd expect:

    <cfoutput>#XMLpacket.xmlRoot.XmlChildren[1].XmlText#</cfoutput>

    Outputs exactly the same value you put in;


    <cfoutput>#toString(XMLpacket)#</cfoutput>

    Demonstrates that under the hood, CF is escaping your ampersands when it puts then in the XML string.  It needs to do this because ampersands are a "reserved character" in XML.

    --

    Adam

    Inspiring
    November 12, 2009

    Hi Nishanth,

    XMLpacket.xmlRoot.XmlChildren[1].XmlText = "this is a test for & and &apos;');

    I think you have an invalid string here.

    And when I tried something like this,

    <cfscript>

    XMLpacket = XmlNew();

    XMLpacket.xmlRoot = XmlElemNew(XMLpacket, "test");

    XMLpacket.xmlRoot.XmlChildren[1]   = XmlElemNew(XMLpacket, "spcial_name");

    XMLpacket.xmlRoot.XmlChildren[1].XmlText = "this is a test for & and &apos;";

    WriteOutput ("#XMLpacket.xmlRoot.XmlChildren[1].XmlText#");
    </cfscript>

    I got this as output,

    this is a test for & and &apos;

    ngpanchalAuthor
    Participant
    November 13, 2009

    Hi,

    I am expecting following output.

    this is a test for & and '

    Nishant.

    BKBK
    Community Expert
    Community Expert
    November 13, 2009

    You need to escape with character entities when you're dealing with an XML document, for example,  like this:

    <cfset testString = "this is a test for & and '">
    <cfxml variable="myDoc">
    <XMLpacket>
    <test>
    <special_name><cfoutput>#xmlformat(testString)#</cfoutput></special_name>
    </test>
    </XMLpacket>
    </cfxml>

    However, in the following example, calling xmlFormat is unnecessary. Coldfusion implicitly escapes the characters.

    <cfscript>
    XMLpacket = XmlNew();
    XMLpacket.xmlRoot = XmlElemNew(XMLpacket, "test");
    XMLpacket.xmlRoot.XmlChildren[1] = XmlElemNew(XMLpacket, "special_name");
    XMLpacket.xmlRoot.XmlChildren[1].XmlText = "this is a test for & and '";
    </cfscript>


    <!--- Is XMLpacket a well-formed XML document? --->
    isXML(toString(XMLpacket)): <strong><cfoutput>#isXML(toString(XMLpacket))#</cfoutput></strong><br>


    <!--- Output the Xmltext --->
    XMLpacket.xmlRoot.XmlChildren[1].XmlText: <strong><cfoutput>#XMLpacket.xmlRoot.XmlChildren[1].XmlText#</cfoutput></strong><br>

    <!--- Dump the XML doc --->

    <cfdump var="#xmlpacket#">