Is there any way of specify the order of the attributes in REXML. I can't find any way of doing this in the documentation. The two methods that I found "add_attribute(key, value=nil)" and "add_attributes(hash)" provides no way of specifying the order. When I output my xml document to a file or stdout, the order of the attributes are in a different order. Any work around for this problem?
on 2007-03-19 12:40
on 2007-03-19 13:06
On 19.03.2007 11:38, caritos wrote: > Is there any way of specify the order of the attributes in REXML. I > can't find any way of doing this in the documentation. > > The two methods that I found "add_attribute(key, value=nil)" and > "add_attributes(hash)" provides no way of specifying the order. > > When I output my xml document to a file or stdout, the order of the > attributes are in a different order. Any work around for this problem? In XML order of attributes is unspecified and does not matter. By relying on it you will almost certainly run into trouble. If you need specific ordering then you need to use nested elements. Other than that you either need to hack REXML code or change your XML document after it is created. Kind regards robert
on 2007-03-19 14:04
It isn't a work around, but one of the many correct ways to work with XML, XSLT XML is a big topic area, but in practice it is often either documents or hashes (associative arrays) As stated by another, XML elements are only ordered by the nesting. For an example of how this looks, export a playlist from iTunes as XML and then look at the XML file. It isn't immediately as human- friendly as you might expect. However, the data structures are very easy to manipulate. iTunes' interface is a great example of how you can push XML data around and make it do things and appear different ways. Simple key/value coding (hash, or associative array) works well as XML and can be very easy to manipulate with simple array functions (or methods) for sorting by alpha-numeric or by size or other things. XML is intended to be a human-readable data format in a sense, but it is also intended to be manipulated for presentation to most users. Load your data into arrays (hashes) then you can order it however you want to for immediate use. If you really need some ordering, you have to load it into an array or search it to sort and then serialize it by writing it to another format (like YAML) or to a database table. XML is nice but not always the best data format. Sometimes inherently ordered data is necessary. But not very.