Skip to main content
Participant
August 29, 2026
Question

cfxml rearranges elements?

  • August 29, 2026
  • 4 replies
  • 14 views

<cfxml variable="xmlObject" casesensitive="yes">
<gallery>
<album title="Product Detail">
<cfoutput query="rsImageFiles">
<img src = " http://www.bloodtest.in/Images/Products/#rsImageFiles.Name#" caption = "Flat View" />
</cfoutput>
</album>
</gallery>
</cfxml>

creates this xml file:

<?xml version="1.0" encoding="UTF-8"?>
<gallery>
<album title="Product Detail">
<img caption="Flat View" src=" http://www.bloodtest.in/Images/Products/B1004Back.jpg"/>
<img caption="Flat View" src=" http://www.bloodtest.in/Images/Products/B1004Full.jpg"/>
<img caption="Flat View" src=" http://www.bloodtest.in/Images/Products/B1004Main.jpg"/>
<img caption="Flat View" src=" http://www.bloodtest.in/Images/Products/B1004Thumb.jpg"/>
</album>
</gallery>

The elements 'src' and 'caption' have been rearranged. Any way to avoid this?

  •  

    4 replies

    BKBK
    Community Expert
    Community Expert
    August 29, 2026

    @James and ​@Charlie Arehart , I had spent some time testing the code in the original post. Only after submitting my post did I notice that Charlie had replied earlier. My post is essentially a repetition of the ideas in his.

    Charlie Arehart
    Community Expert
    Community Expert
    August 29, 2026

    Great minds... :-)

    FWIW, before I submitted mine I first copied it to the clipboard and refreshed, as I suspected you could indeed be writing at the same time. :-) I saw you had not, so proceeded. Something to consider, though I grant it's an extra step. 

    /Charlie (troubleshooter, carehart. org)
    BKBK
    Community Expert
    Community Expert
    August 29, 2026

    I can see the problem, and I do sympathize. But I think you should, in general, not assume that cfxml or any other XML parser will preserve attribute order.

     

    In fact, you should not even try to preserve XML attribute order. In XML, the order of attributes is explicitly insignificant. In your code <img> is just an XML element, like any other. So the following two elements are semantically equivalent:

    <img src="ImageFile.jpg" caption="Flat View" />

    <img caption="Flat View" src="ImageFile.jpg" />

     

    This equivalence has consequences. For example, it means that, when <cfxml> parses the markup into an XML DOM, ColdFusion's XML implementation is free to store or serialize the attributes in whatever order it chooses. In your case, cfxml chooses to do so alphabetically. 

     

    If the order of the attributes really matters to your application, then I will suggest you use a raw XML string instead of, or in addition to, an XML object.

     

    <cfset rsImageFiles = queryNew("id,name","Integer,Varchar", [ [1,"B1004Back.jpg"], [2,"B1004Full.jpg"], [3,"B1004Main.jpg"], [4, "B1004Thumb.jpg"] ])>

    <!--- Order of attributes preserved --->
    <cfsavecontent variable="xmlString">
    <gallery>
    <album title="Product Detail">
    <cfoutput query="rsImageFiles">
    <img src="http://www.bloodtest.in/Images/Products/#rsImageFiles.name#" caption="Flat View" />
    </cfoutput>
    </album>
    </gallery>
    </cfsavecontent>

    <!--- Order of attributes not necessarily preserved --->
    <cfxml variable="xmlObject" casesensitive="yes">
    <cfoutput>#xmlString#</cfoutput>
    </cfxml>

    <p>
    <strong>XML String - Order of attributes preserved</strong><br>
    <cfdump var="#xmlString#">
    </p>

    <p>
    <strong>toString(xmlObject) - Order of attributes not necessarily preserved</strong><br>
    <cfdump var="#toString(xmlObject)#">
    </p>

     

    Last, but not least, if you feel that cfxml should be able to preserve the order of attributes, you should make a Feature Request.

    Charlie Arehart
    Community Expert
    Community Expert
    August 29, 2026

    Bad news/good news, ​@James .

    Sadly there is no way to stop cfxml from ordering attributes of xml elements by name alphabetically. Then again, this is because technically in xml there's no significance to the ordering of such attributes. 

    That said, it's not clear why you're using cfxml anyway. You say you're creating an xml file, but your code as shown is not doing that.

    Here's perhaps the simple solution: replace the use of cfxml here with cfsavecontent. It's literally just swapping those tags. Then output the same variable to your file instead, however you're doing that.

    Unless you're somehow using cf's xml object capabilities in ways not shown here, you don't need cfxml at all. 

    Let us know how that goes. 

    /Charlie (troubleshooter, carehart. org)