# Builder gem - xml attributes ordering

**URL:** <https://www.ruby-forum.com/t/builder-gem-xml-attributes-ordering/106225>\
**Category:** Ruby\
**Created:** [June 7, 2007, 6:14pm UTC](https://www.ruby-forum.com/t/builder-gem-xml-attributes-ordering/106225 "2007-06-07T18:14:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![arunprabu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e47774/32.png) [@arunprabu](https://www.ruby-forum.com/u/arunprabu)\
**Post date:** [June 7, 2007, 6:14pm UTC](https://www.ruby-forum.com/t/builder-gem-xml-attributes-ordering/106225/1 "2007-06-07T18:14:13Z")

</div>

Hi,

I am currently developing a xml service using Ruby on rails with builder  
gem. Builder is a great tool to create xml easily. But the only and  
important issue I am facing is - unable to set the order inwhich the  
attributes to be displayed in the xml. Whatever order I give the  
attributes of an element, Builder creates the attributes in a totally  
different order. Request someone’s help to overcome this issue. Please  
specify even if there is another method to create this service which  
return xml instead of html using REST instead of Builder.

Thanks in advance  
Arun

---

<div class="post-metadata">

**Author:** ![arunprabu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e47774/32.png) [@arunprabu](https://www.ruby-forum.com/u/arunprabu)\
**Post date:** [June 7, 2007, 6:47pm UTC](https://www.ruby-forum.com/t/builder-gem-xml-attributes-ordering/106225/2 "2007-06-07T18:47:01Z")

</div>

On 6/7/07, Arunprabu D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am … unable to set the order in which the  
> attributes to be displayed in the xml.

Why do you think the attributes for an element _should_ appear in any  
particular order?

---

<div class="post-metadata">

**Author:** ![arunprabu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e47774/32.png) [@arunprabu](https://www.ruby-forum.com/u/arunprabu)\
**Post date:** [June 7, 2007, 7:08pm UTC](https://www.ruby-forum.com/t/builder-gem-xml-attributes-ordering/106225/3 "2007-06-07T19:08:20Z")

</div>

Lyle J. wrote:

> On 6/7/07, Arunprabu D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I am … unable to set the order in which the  
> > attributes to be displayed in the xml.
> 
> Why do you think the attributes for an element _should_ appear in any  
> particular order?

Hi Johnson,

The service We are developing is going to be used by and application and  
persons. That is the service will be called from another application for  
getting the data. In this case, the ordering is not a necessary thing.  
But in case of reading the xml manually in a browser, it is easy to read  
the data if it comes in a specified order. Because, there are 5 to 10  
attributes to each elements in our xml. And it is tough to locate the  
attribute each time.

Thanks  
Arun

---

<div class="post-metadata">

**Author:** ![arunprabu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e47774/32.png) [@arunprabu](https://www.ruby-forum.com/u/arunprabu)\
**Post date:** [June 7, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/builder-gem-xml-attributes-ordering/106225/4 "2007-06-07T19:30:26Z")

</div>

Hi Johnson,

I understand that it is not specified in the XML specification and it is  
not a requirement generally. But in languages like Java using Xerces,  
the xml is generated with attributes in the sequence you have created  
it. So just trying to find if there is any way to over come this issue.  
I already have an alternative of constructing the xml as a string in the  
sequence of attributes I need. But this is an ugly approach and trying  
to find if there is any other alternative.

I also checked with REXML and it has the same behaviour as Builder.

Thanks  
Arun

---

<div class="post-metadata">

**Author:** ![arunprabu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e47774/32.png) [@arunprabu](https://www.ruby-forum.com/u/arunprabu)\
**Post date:** [June 7, 2007, 7:21pm UTC](https://www.ruby-forum.com/t/builder-gem-xml-attributes-ordering/106225/5 "2007-06-07T19:21:03Z")

</div>

On 6/7/07, Lyle J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 6/7/07, Arunprabu D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I am … unable to set the order in which the  
> > attributes to be displayed in the xml.
> 
> Why do you think the attributes for an element _should_ appear in any  
> particular order?

To be explicit, Builder’s behavior is acceptable because the order of  
attributes in XML is not defined:

“”“Note that the order of attribute specifications in a start-tag or  
empty-element tag is not significant.”“”

[http://www.w3.org/TR/REC-xml/](http://www.w3.org/TR/REC-xml/)

HTH,  
Keith

---

<div class="post-metadata">

**Author:** ![arunprabu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e47774/32.png) [@arunprabu](https://www.ruby-forum.com/u/arunprabu)\
**Post date:** [June 7, 2007, 7:51pm UTC](https://www.ruby-forum.com/t/builder-gem-xml-attributes-ordering/106225/6 "2007-06-07T19:51:11Z")

</div>

On 6/7/07, Arunprabu D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi Johnson,
> 
> I understand that it is not specified in the XML specification and it is  
> not a requirement generally.
> 
> I also checked with REXML and it has the same behaviour as Builder.

Yeah, I’m sure they both use Hashes internally for attributes (and  
Hashes are unordered in Ruby). However, it’d be easy to modify the  
attribute insertion in Builder to take an Array instead. This is what  
you’ll have to change:

gems/builder-2.0.0/lib/builder/xmlmarkup.rb

```
# Insert the attributes (given in the hash).
def _insert_attributes(attrs, order=[])
  return if attrs.nil?
  order.each do |k|

```

v = attrs[k]  
@target \<\< %{ #{k}=“#{\_attr\_value(v)}”} if v  
end  
attrs.each do |k, v|  
@target \<\< %{ #{k}=“#{\_attr\_value(v)}”} unless order.member?(k)  
end  
end

Keith
