REXML question

C

Chris McMahon

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?
 
M

Mark Volkmann

Hi...

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

elements =3D 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?

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 express=
ion.
"//*[text()]" means that you only want elements that have text in them.
Consider the following.
<car>
<make>Saturn</make>
<model>SC2</model>
<colors exterior=3D"purple" interior=3D"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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

REXML Input File Question 7
Question about REXML 2
REXML usage 3
REXML Element exists question 4
REXML raw all doesn't seem to work 0
REXML and Empty-Elements 1
REXML XPath bug? 1
REXML Speed Question 3

Members online

Forum statistics

Threads
474,470
Messages
2,571,809
Members
48,797
Latest member
PeterSimpson
Top