rexml each_element_with_attribute question

Discussion in 'Ruby' started by zerohalo, Aug 27, 2005.

  1. zerohalo

    zerohalo Guest

    I'm a Ruby newbie and have been working on a script that among other
    things, parses a series of XML files, searches for elements that
    contain a certain attribute ('question') and then parsing through the
    children of all of those elements for certain tags. Note that
    'question' elements can be anywhere in the XML file - one level deep or
    ten levels deep - so doing an exact path match for 'question' doesn't
    work.

    I thought I had found an easy way with
    doc.root.each_element_with_attribute('question') and that it would
    return an array of all of the 'question' elements in the whole file.
    But after much frustration I finally realized that it only returns the
    elements with that attribute that are at that level - it doesn't
    recurse through all the children in the file to find them all. Hard to
    believe that there's not an option to have it recurse through all the
    children, but maybe I haven't found it. The documentation on rexml
    isn't that extensive and I had a hard time understanding some things
    (of course that's also due to the the fact that I'm a n00b).

    I've tried using XPath.match('question') but Xpath requires the path,
    and like I said, these 'question' elements could be at any level,
    depending on the XML file, so I'm not sure that that works either
    (other than going through each level of the document "manually", but
    that sucks).

    Does anyone know of a simple way, using something like
    each_element_with_attribute, to return an array of all of the elements
    that match a certain attribute?

    Thanks.
    zerohalo, Aug 27, 2005
    #1
    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:
    87
    Ralph Mason
    Jan 13, 2004
  3. Han Holl

    Rexml xpath question

    Han Holl, Feb 22, 2004, in forum: Ruby
    Replies:
    7
    Views:
    141
    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