construct AoA with REXML XPath?

Discussion in 'Ruby' started by Chris McMahon, Mar 15, 2006.

  1. Hi...

    say I have a document

    <plan xsi:type="ns0:plan">
    <id>123</id>
    <status>ACTIVE</status>
    <jellyBean>RED</jellyBean>
    <foo>BAR</foo>
    </plan>
    <plan xsi:type="ns0:plan">
    <id>456</id>
    <status>BOGUS</status>
    <jellyBean>BLUE</jellyBean>
    <foo>BAZ</foo>
    </plan>

    I know I can do
    ids = doc.elements.to_a( "//plan/id")
    jellybeans = doc.elements.to_a( "//plan/jellyBean")

    but is there some way to do something like

    idsAndJellyBeans = doc.elements.to_a( "//plan/id" "//plan/jellyBean")

    and get

    [[123, "RED"],[456,"BLUE"]]?

    Thanks...
    Chris McMahon, Mar 15, 2006
    #1
    1. Advertising

  2. You could use Array#zip

    ids = doc.elements.to_a( "//plan/id/").map{|i| i.text}
    => ["123", "456"]

    jellybeans = doc.elements.to_a( "//plan/jellyBean").map {|i| i.text}
    => ["RED", "BLUE"]

    idsAndJellyBeans = ids.zip(jellybeans)
    => [["123", "RED"], ["456", "BLUE"]]
    Gordon Thiesfeld, Mar 15, 2006
    #2
    1. Advertising

  3. Yes, but I wonder if there is a rock-solid guarantee that each array
    that is an element of idsAndJellyBeans is in fact from the same "plan"
    record.

    Empirical research suggests that they are, but I'm nervous. That's why
    I wonder if there is some sort of pure-REXML trick that would extract
    multiple elements from within a single record, so that I could always
    be sure that the id and the jellyBean in each individual array had FER
    SHURE come from the same "plan" record.
    Chris McMahon, Mar 15, 2006
    #3
  4. Chris McMahon wrote:
    > Hi...
    >
    > say I have a document
    >
    > <plan xsi:type="ns0:plan">
    > <id>123</id>
    > <status>ACTIVE</status>
    > <jellyBean>RED</jellyBean>
    > <foo>BAR</foo>
    > </plan>
    > <plan xsi:type="ns0:plan">
    > <id>456</id>
    > <status>BOGUS</status>
    > <jellyBean>BLUE</jellyBean>
    > <foo>BAZ</foo>
    > </plan>
    >
    > I know I can do
    > ids = doc.elements.to_a( "//plan/id")
    > jellybeans = doc.elements.to_a( "//plan/jellyBean")
    >
    > but is there some way to do something like
    >
    > idsAndJellyBeans = doc.elements.to_a( "//plan/id" "//plan/jellyBean")
    >
    > and get
    >
    > [[123, "RED"],[456,"BLUE"]]?
    >
    > Thanks...


    Using plain Ruby:

    class Array; alias atr first; alias txt last end
    class String
    def xtag(str)
    result = []
    re = %r{ < #{str} (?: \s+ ( (?> [^>"/]* (?> "[^"]*" )? )* ) )? }xi
    scan( %r{ #{re} / > | #{re} > ( .*? ) </ #{str} >
    }mix ) \
    { |unpaired, attr, data| h = { }
    ( unpaired || attr || "" ).
    scan( %r{ ( \w+ ) \s* = \s* (?: " ( [^"]* ) " | ( \S+ ) )
    }mx ) { |k,v,v2|
    h[k.downcase] = (v || v2) }
    block_given? ? ( yield [ h, data ] ) : result << [ h, data ]
    }
    result
    end
    end

    p DATA.read.xtag('plan').map{|attr,text|
    [ text.xtag('id').first.txt,
    text.xtag('jellyBean').first.txt
    ]
    }

    __END__
    <plan xsi:type="ns0:plan">
    <id>123</id>
    <status>ACTIVE</status>
    <jellyBean>RED</jellyBean>
    <foo>BAR</foo>
    </plan>
    <plan xsi:type="ns0:plan">
    <id>456</id>
    <status>BOGUS</status>
    <jellyBean>BLUE</jellyBean>
    <foo>BAZ</foo>
    </plan>
    William James, Mar 16, 2006
    #4
    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. Mukesh
    Replies:
    4
    Views:
    605
    Paul N
    Mar 26, 2010
  2. Damphyr
    Replies:
    2
    Views:
    131
    Damphyr
    Jul 16, 2003
  3. Daniel Berger

    rexml error - REXML::Validation

    Daniel Berger, Oct 12, 2004, in forum: Ruby
    Replies:
    2
    Views:
    141
    Henrik Horneber
    Oct 12, 2004
  4. Vishal G

    Merging/Joining AoA

    Vishal G, Apr 22, 2008, in forum: Perl Misc
    Replies:
    12
    Views:
    142
    Ben Bullock
    Apr 24, 2008
  5. Gundala Viswanath

    Problem with Eval in Constructing AoA

    Gundala Viswanath, Sep 4, 2008, in forum: Perl Misc
    Replies:
    2
    Views:
    86
    Eric Pozharski
    Sep 4, 2008
Loading...

Share This Page