From: Axel Etzold on

-------- Original-Nachricht --------
> Datum: Fri, 25 Sep 2009 10:54:03 +0900
> Von: Lance Pollard <lancejpollard(a)gmail.com>
> An: ruby-talk(a)ruby-lang.org
> 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

From: Robert Klemme on
2009/9/25 Lance Pollard <lancejpollard(a)gmail.com>:
> 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::Pretty
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::Document.new(DATA.read)

fmt = REXML::Formatters::Pretty.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/

From: Lance Pollard on
> require 'rexml/document'
>
> class OrderedAttributes < REXML::Formatters::Pretty
> 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::Document.new(DATA.read)
>
> fmt = REXML::Formatters::Pretty.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/.

First  |  Prev  | 
Pages: 1 2
Prev: ruby-oci8 function
Next: xmlrpc