REXML question

Discussion in 'Ruby' started by Chris McMahon, Jan 24, 2006.

  1. 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?
    Chris McMahon, Jan 24, 2006
    #1
    1. Advertising

  2. On 1/24/06, Chris McMahon <> wrote:
    >
    > 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.

    --
    R. Mark Volkmann
    Partner, Object Computing, Inc.
    Mark Volkmann, Jan 24, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Damphyr
    Replies:
    2
    Views:
    137
    Damphyr
    Jul 16, 2003
  2. Ralph Mason
    Replies:
    2
    Views:
    85
    Ralph Mason
    Jan 13, 2004
  3. Han Holl

    Rexml xpath question

    Han Holl, Feb 22, 2004, in forum: Ruby
    Replies:
    7
    Views:
    138
    Han Holl
    Mar 6, 2004
  4. Daniel Berger

    rexml error - REXML::Validation

    Daniel Berger, Oct 12, 2004, in forum: Ruby
    Replies:
    2
    Views:
    151
    Henrik Horneber
    Oct 12, 2004
  5. Phlip
    Replies:
    0
    Views:
    140
    Phlip
    Jan 15, 2008
Loading...

Share This Page