Parsing through XML with REXML/XPath

Discussion in 'Ruby' started by Peter Bailey, Sep 25, 2007.

  1. Peter Bailey

    Peter Bailey Guest

    Hi,
    I need to sort groups of xml data based on the first instances of
    particular elements down deep in the element structure of documents.
    regs = []
    regs = XPath.match(doc, "//registration")
    regs.each do |reg|
    codes = XPath.match(regs, "//issue[1]/")# { |element| puts
    element.text }
    puts codes
    end
    I'm getting:
    <issue code='ENG'>Energy/Nuclear</issue>
    <issue code='EDU'>Education</issue>
    <issue code='AGR'>Agriculture</issue>
    ...

    There are only 86 entries of <issue> in the document, but, I'm getting
    over 2,550 results here for "puts codes!" Obviously, it's looping and I
    don't know why. It is pulling just the first entries, which I want, but,
    obiously, it's doing it lots and lots of times.

    I'm also trying to parse out these results, so that, I only end up with
    the actual element text, not any attributes. So, for example, in the
    above results, I only want "Energy/Nuclear, Education, and Agriculture,"
    not any of the surrounding stuff. So, I've tried this, inside the above:
    codes.each do |code|
    code.to_s.gsub!(/<issue code='[A-Z]{3}'>(.*?)\/*.*<\/issue>/, "$1")
    puts code
    end

    Thanks,
    Peter
    --
    Posted via http://www.ruby-forum.com/.
    Peter Bailey, Sep 25, 2007
    #1
    1. Advertising

  2. Peter Bailey

    Peter Bailey Guest

    Thanks, Mark. Yes, I'm sorry about the lingo. I'm not that versed in
    XML-speak yet. But, basically, I want to sort all of the <registration>
    data sets in my files, just alphabetically. But, as I said, I need to
    put in some headings into my output, and, those headings are essentially
    the text that's in the very first instance of the <issue> element. The
    <issue> element is a child element to <registration>. Now, your
    suggestion with the use of /text() worked, meaning, I got just that text
    that I want. That's great. But, it's still looping and giving me many
    repeats of these instances.

    Here's my e-mail address, if you'd like to strike up a separate
    conversation.

    Thanks,
    Peter

    --
    Posted via http://www.ruby-forum.com/.
    Peter Bailey, Sep 25, 2007
    #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. Daniel Berger

    rexml error - REXML::Validation

    Daniel Berger, Oct 12, 2004, in forum: Ruby
    Replies:
    2
    Views:
    151
    Henrik Horneber
    Oct 12, 2004
  3. Derek
    Replies:
    2
    Views:
    150
    Robert Klemme
    May 1, 2005
  4. ermac

    Rexml Xpath parsing change

    ermac, Oct 29, 2007, in forum: Ruby
    Replies:
    2
    Views:
    151
  5. Phlip
    Replies:
    0
    Views:
    140
    Phlip
    Jan 15, 2008
Loading...

Share This Page