# REXML question

**URL:** <https://www.ruby-forum.com/t/rexml-question/96773>\
**Category:** Ruby\
**Created:** [January 24, 2006, 8:00pm UTC](https://www.ruby-forum.com/t/rexml-question/96773 "2006-01-24T20:00:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_McMahon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/74df32/32.png) [@Chris\_McMahon](https://www.ruby-forum.com/u/Chris_McMahon)\
**Post date:** [January 24, 2006, 8:00pm UTC](https://www.ruby-forum.com/t/rexml-question/96773/1 "2006-01-24T20:00:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Chris\_McMahon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/74df32/32.png) [@Chris\_McMahon](https://www.ruby-forum.com/u/Chris_McMahon)\
**Post date:** [January 24, 2006, 8:55pm UTC](https://www.ruby-forum.com/t/rexml-question/96773/2 "2006-01-24T20:55:36Z")

</div>

On 1/24/06, Chris McMahon [removed\_email\_address@domain.invalid](mailto: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.  
  
Saturn  
SC2

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.
