8 Replies Latest reply on Mar 14, 2012 7:32 AM by pixelwiz19801

    Maintain attribute order in XML with a structure?

    walter@whardy.com

      I'm using xmlParse() to read an XML file into a structure, and then process the structure (replace some of the XML attributes) and then write the structure back out as an XML file. The problem is that I loose the original ordering of XML attrbutes when I convert to a structure, and instead end up with a new attribute order for each element that is alphabetical.

       

      In otherwords: <element c="text" d="text" a="text" />

      gets rewritten as: <element a="text" c="text" d="text" />

      which is a problem for this application.

       

      Is there a way to work with XML in CF but maintain attribute ordering (LinkedHashMap instead of a structure, maybe)?

       

      Thanks.

      Walter

        • 1. Re: Maintain attribute order in XML with a structure?
          Adam Cameron. Level 5

          If you need to control the order of the attributes, you'll need to write your xml out by hand... according to the XML specification, attribute ordering must not be significant, so strictly-speaking your requirement here requires something of XML that's contrary to its intent.  And, accordingly, there's no way of making CF respect something that's intrinsically not supposed to be respected.

           

          NB: "I'm using xmlParse() to read an XML file into a structure,"... xmlParse() creates an XML object, not a struct, so what you're asking in your last para doesn't really make sense.  xmlParse() will only create an XML object. If you want to read the XML data as something other than XML, you'll need to write your own function.

           

          The best solution here, if poss, is to remove the significance of the attribute order in your app, because it's "wrong" to infer any ordering, and you're making a bit of a logical rod for your own back in relying on this ordering.

           

          Not a "just do it like this" response, sorry, but that's what one gets when the question is submerged in a can of worms... ;-)

           

          --
          Adam

          • 2. Re: Maintain attribute order in XML with a structure?
            walter@whardy.com Level 1

            Thanks, Adam. I totally agree. Attribute ordering may not even be an issue for this application, but using ColdFusion to process XML in this way is making a pretty big change in the XML structure. It's that change that has me concerned. So far everything works fine with attribute reordering. But then it's a huge application in its last stage of development before GA release, and it's too risky to make the XML files "look" so different at this point. So I found I can work around using a structure and instead treat them as textual files and use find/replace for those attributes I need to revise. Hopefully next time, I'll be onboard earlier in the project development cycle so there is time to confirm that everything is well behaved.

             

            Walter

            • 3. Re: Maintain attribute order in XML with a structure?
              Adam Cameron. Level 5

              but using ColdFusion to process XML in this way is making a pretty big change in the XML structure.

               

              No, it's not changing the structure in any meaningful way: the attribute ordering isn't meaningful in XML, so if the order of them changes, then that's not a meaningful change.  In any way that XML should be used, it doesn't matter.

               

              When CF reads an string (like file contents) and parses it as XML, it needs to create an XML object; that XML object will adhere to the rules of XML; one of which is "atribute order has no significance", so it will not (and ought not) bother to "remember" the order that the attributes were in in the original string.  When one converts that object back into a string, some sort of ordering needs to be used to write them back, and CF seems to arbitrarily use alphabetical order.  But it cannot refer back to information that it doesn't have (the original ordering), so the original ordering can't be preserved.

               

              There's nothing unreasonable or that shouldn't be expected in this process, unfortunately (for you, I mean).

               

              --

              Adam

              • 4. Re: Maintain attribute order in XML with a structure?
                pixelwiz19801 Level 1

                I'm having a very similar problem, except in my case if for human readability.  I have a property called number, and it would be nice if that was displayed first, and not alphabetically towards the end of the list of properties.  Just hard to double check everything on screen when the order is all moved around when using <CFXML>

                • 5. Re: Maintain attribute order in XML with a structure?
                  Owain North Level 4

                  As has been mentioned, it's just not how XML works. Does XSLT allow for ordering?

                  • 6. Re: Maintain attribute order in XML with a structure?
                    pixelwiz19801 Level 1

                    I understand that, but it only seems to be a problem with coldfusion.  Like our vendor can send us an XML feed and the attributes in it are not order alphabetically so it makes it really easy to read.  But when I send back the same feed with updated info all the parameters are moved around alphabetically.  Everything still works, so that's not the issue, but during testing it's just really hard to read and compare the feeds.  I thought the whole original purpose behind XML was to make a format that allowed for easy data sharing that was easily human readable.  Oh well, hopefully they can add some attribute in CF10 to say don't change the order that's explicitly specified when the XML is created.

                    • 7. Re: Maintain attribute order in XML with a structure?
                      Owain North Level 4

                      hopefully they can add some attribute in CF10 to say don't change the order that's explicitly specified when the XML is created.

                      It would appear not.

                       

                      I'd be surprised if anyone made a change like that, as it's not a requirement of XML it's more that your other systems "happen" to display them in order; they equally could not and still be valid. XML was never meant to be displayed as such, and as far as I know it's only human-readable as a by-product of it being a text-based standard; I'm not sure this was so it could be "easily read" by people.

                       

                      Generally you'd parse the XML and display it in your own format, so ordering wouldn't be an issue.

                      • 8. Re: Maintain attribute order in XML with a structure?
                        pixelwiz19801 Level 1

                        Ah, found a solution on another thread.  Using CFSAVECONTENT instead of CFXML totally resolved this issue for me.