# Attribute Ordering in REXML

**URL:** <https://www.ruby-forum.com/t/attribute-ordering-in-rexml/89298>\
**Category:** Ruby\
**Created:** [March 19, 2007, 11:40am UTC](https://www.ruby-forum.com/t/attribute-ordering-in-rexml/89298 "2007-03-19T11:40:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![caritos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e0b2c6/32.png) [@caritos](https://www.ruby-forum.com/u/caritos)\
**Post date:** [March 19, 2007, 11:40am UTC](https://www.ruby-forum.com/t/attribute-ordering-in-rexml/89298/1 "2007-03-19T11:40:34Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![caritos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e0b2c6/32.png) [@caritos](https://www.ruby-forum.com/u/caritos)\
**Post date:** [March 19, 2007, 12:06pm UTC](https://www.ruby-forum.com/t/attribute-ordering-in-rexml/89298/2 "2007-03-19T12:06:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![caritos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e0b2c6/32.png) [@caritos](https://www.ruby-forum.com/u/caritos)\
**Post date:** [March 19, 2007, 1:04pm UTC](https://www.ruby-forum.com/t/attribute-ordering-in-rexml/89298/3 "2007-03-19T13:04:42Z")

</div>

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.
