From: dblack@... Date: 2003-01-11T00:47:09+09:00 Subject: Re: REXML and XPath Confusion Hi -- On Fri, 10 Jan 2003, Mark Probert wrote: > > Rubyists, > > I am confused! (Nothing unusual there! :-) > > Using XPath.match, I get an array of attributes. When I try to > match against this array, with what seems to be the same name, > it doesn't match. Any ideas? > > > $ cat test.xml > > >
> > >
>
>
>
>
>
> > $ cat test.rb > require "rexml/xpath" > require "rexml/document" > include REXML > > doc = Document.new File.new( "test.xml" ) > > panels = XPath.match( doc, "//panel/attribute::name" ) > panels.each { |panel| > puts "panel --> [#{panel}]" > } > puts "-----" > > doc.elements.each("doc/section") { |sect| > puts "sect --> #{sect.attributes["name"]}" > sect.elements.each("panel") { |panel| > name = panel.attributes["name"] > puts "panel --> [#{name}]" > puts (panels.include?(name)) ? "included" : "not found" > } > } panels is an array of REXML::Attribute objects. That array won't include name, but it will include a panel whose value is the same as name. You just have to dig a bit to get the value. Replace your last line of code with: puts panels.detect {|p| p.value == name } ? "included" : "not found" David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav