Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Ordering XML Attributes with Hpricot?

Reply
Thread Tools

Ordering XML Attributes with Hpricot?

 
 
Lance Pollard
Guest
Posts: n/a
 
      09-24-2009
Hey,

Is there a way to organize/print out the xml attributes using Hpricot,
or do I have to run through the xml file again and replace patterns?

I would like to be able to say "put this attribute first, put this
attribute next ...", so I can say, I want this:

<node id="name" property="value"/>

not this:

<node property="value" id="name"/>

Since the attributes are kept in a hash there's no order to them, so
they appear in seemingly random order, but it's the same random order
consistently.

Any ideas how to do that?

And is there a way to say "after two attributes, make a new line". So I
can print out xml that can be edited by humans like code.

Thanks,
Lance
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Axel Etzold
Guest
Posts: n/a
 
      09-24-2009
Dear Lance,

>
> Since the attributes are kept in a hash there's no order to them, so
> they appear in seemingly random order, but it's the same random order
> consistently.
>
> Any ideas how to do that?
>


a Hash can be sorted to give an Array with Hash#sort :

http://ruby-doc.org/core/classes/Hash.html#M002865


> And is there a way to say "after two attributes, make a new line". So I
> can print out xml that can be edited by humans like code.


You can then iterate through the Array with Array#each_with_index,
eg.

my_array.each_with_index{|x,i|





>
> Thanks,
> Lance
> --
> Posted via http://www.ruby-forum.com/.


--
Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 -
sicherer, schneller und einfacher! http://portal.gmx.net/de/go/chbrowser

 
Reply With Quote
 
 
 
 
Axel Etzold
Guest
Posts: n/a
 
      09-24-2009
Dear Lance,

I accidentally hit the "send" button too early:

>
> Since the attributes are kept in a hash there's no order to them, so
> they appear in seemingly random order, but it's the same random order
> consistently.
>
> Any ideas how to do that?
>


a Hash can be sorted to give an Array with Hash#sort :

http://ruby-doc.org/core/classes/Hash.html#M002865


> And is there a way to say "after two attributes, make a new line". So I
> can print out xml that can be edited by humans like code.


You can then iterate through the Array with Array#each_with_index,
eg.

my_array.each_with_index{|x,i| if i%2==0 ; p x + "\n"; else p x; end}

Best regards,

Axel




--
Neu: GMX Doppel-FLAT mit Internet-Flatrate + Telefon-Flatrate
für nur 19,99 Euro/mtl.!* http://portal.gmx.net/de/go/dsl02

 
Reply With Quote
 
Lance Pollard
Guest
Posts: n/a
 
      09-25-2009
Thanks a lot axel, I'll give these a try

Best,
Lance

Axel Etzold wrote:
> Dear Lance,
>
> I accidentally hit the "send" button too early:
>
>>
>> Since the attributes are kept in a hash there's no order to them, so
>> they appear in seemingly random order, but it's the same random order
>> consistently.
>>
>> Any ideas how to do that?
>>

>
> a Hash can be sorted to give an Array with Hash#sort :
>
> http://ruby-doc.org/core/classes/Hash.html#M002865
>
>
>> And is there a way to say "after two attributes, make a new line". So I
>> can print out xml that can be edited by humans like code.

>
> You can then iterate through the Array with Array#each_with_index,
> eg.
>
> my_array.each_with_index{|x,i| if i%2==0 ; p x + "\n"; else p x; end}
>
> Best regards,
>
> Axel


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Lance Pollard
Guest
Posts: n/a
 
      09-25-2009
This means though I have to do two passes on the XML:

1) Modify the nodes with data the way nokogiri or hpricot do it (xpath
and whatnot)
2) Format the xml using regular expression on pure strings, not using
the xml parsing engines.

Is that correct?

Thanks,
Lance
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Axel Etzold
Guest
Posts: n/a
 
      09-25-2009

-------- Original-Nachricht --------
> Datum: Fri, 25 Sep 2009 10:54:03 +0900
> Von: Lance Pollard <>
> An: ruby-
> Betreff: Re: Ordering XML Attributes with Hpricot?


> This means though I have to do two passes on the XML:
>
> 1) Modify the nodes with data the way nokogiri or hpricot do it (xpath
> and whatnot)
> 2) Format the xml using regular expression on pure strings, not using
> the xml parsing engines.
>
> Is that correct?


Lance,

I remember that Hpricot and Nokogiri both have pretty_print methods, but I have never used them. Also, I don't know whether "pretty" can be defined so that everybody agrees

Best regards,

Axel
--
Neu: GMX Doppel-FLAT mit Internet-Flatrate + Telefon-Flatrate
für nur 19,99 Euro/mtl.!* http://portal.gmx.net/de/go/dsl02

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      09-25-2009
2009/9/25 Lance Pollard <>:
> This means though I have to do two passes on the XML:
>
> 1) Modify the nodes with data the way nokogiri or hpricot do it (xpath
> and whatnot)
> 2) Format the xml using regular expression on pure strings, not using
> the xml parsing engines.
>
> Is that correct?


I would not work on the output XML via String replacements. I would
rather adjust the output process. For example, if you would be
working with REXML you could implement a Formatter which outputs
attributes in a particular order. I don't know whether this can be
done with Nokogiri or Hpricot as well or as easily.

Kind regards

robert


require 'rexml/document'

class OrderedAttributes < REXML::Formatters:retty
def write_element(elm, out)
att = elm.attributes

class <<att
alias _each_attribute each_attribute

def each_attribute(&b)
to_enum(:_each_attribute).sort_by {|x| x.name}.each(&b)
end
end

super(elm, out)
end
end

doc = REXML:ocument.new(DATA.read)

fmt = REXML::Formatters:retty.new
fmt.write(doc, $stdout)
puts

fmt = OrderedAttributes.new
fmt.write(doc, $stdout)
puts

__END__
<foo battr="1" aattr="2" cattr="3">
</foo>



--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 
Reply With Quote
 
Lance Pollard
Guest
Posts: n/a
 
      09-25-2009
> require 'rexml/document'
>
> class OrderedAttributes < REXML::Formatters:retty
> def write_element(elm, out)
> att = elm.attributes
>
> class <<att
> alias _each_attribute each_attribute
>
> def each_attribute(&b)
> to_enum(:_each_attribute).sort_by {|x| x.name}.each(&b)
> end
> end
>
> super(elm, out)
> end
> end
>
> doc = REXML:ocument.new(DATA.read)
>
> fmt = REXML::Formatters:retty.new
> fmt.write(doc, $stdout)
> puts
>
> fmt = OrderedAttributes.new
> fmt.write(doc, $stdout)
> puts
>
> __END__
> <foo battr="1" aattr="2" cattr="3">
> </foo>


Thanks a lot Robert, I will try that out immediately.

Best,
Lance
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Z-Ordering (Morton ordering) question nbigaouette C Programming 2 11-06-2009 05:26 AM
Ordering attributes for dynamically generated class David Pratt Python 3 01-19-2009 10:29 PM
Builder gem - xml attributes ordering Arunprabu Durairaju Ruby 5 06-07-2007 05:50 PM
web.xml / XML schema issue, why do some XML schema attributes disappear asciz@starmail.com Java 3 02-20-2007 09:56 AM
Why is XML "stupid" about element ordering? Arturius mac Aidan XML 5 10-31-2005 03:23 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57