Forum: Ruby REXML question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Chris McMahon (Guest)
on 2006-01-24 21:00
(Received via mailing list)
Hi...

I cargo-culted the following REXML statement, and it's working fine:

elements = Document.new( my_xml ).elements.to_a( "//*[text()]").map {
|e|
  e.text.strip.empty? ? nil :  e.text.strip}.compact

but I'm no expert at this.  I want for this expression to return an
array containing every element of any given XML Document in a reliable
order.  It seems to do so.

Is there any XML with elements that would not be captured by this
expression?
Mark V. (Guest)
on 2006-01-24 21:55
(Received via mailing list)
On 1/24/06, Chris McMahon <removed_email_address@domain.invalid> wrote:
> array containing every element of any given XML Document in a reliable
> order.  It seems to do so.
>
> Is there any XML with elements that would not be captured by this
> expression?

Are you trying to find only elements that contain text in them that is
not just whitespace?

I can't comment on your use of REXML, but I'll comment on you XPath
expression.
"//*[text()]" means that you only want elements that have text in them.
Consider the following.
<car>
  <make>Saturn</make>
  <model>SC2</model>
  <colors exterior="purple" interior="tan"/>
</car>

Which of these elements have text in them?
Clearly make and model do. Clearly colors does not.
Somewhat surprisingly, car does. It has whitespace inside it, in
addition to child elements. Not only that, it has four pieces of text
inside. A DOM parser would say that the car element has four text
child nodes. I'm not sure how REXML treats this.
This topic is locked and can not be replied to.