Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Xpath to attributes

Reply
Thread Tools

Xpath to attributes

 
 
listrecv@gmail.com
Guest
Posts: n/a
 
      02-22-2006
Is there anyway to do generic xpath's, for both elements and attributes
(ie //customer/phone/@type --> "home")? REXML can do xpath to fetch
elements (rexml_element.text('//customer/phone')) but can't seem to use
xpath to find attributes.

 
Reply With Quote
 
 
 
 
Ross Bamford
Guest
Posts: n/a
 
      02-22-2006
On Wed, 2006-02-22 at 10:33 +0900, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Is there anyway to do generic xpath's, for both elements and attributes
> (ie //customer/phone/@type --> "home")? REXML can do xpath to fetch
> elements (rexml_element.text('//customer/phone')) but can't seem to use
> xpath to find attributes.


d = REXML:ocument.new <<EOX
<customer>
<phone type='home'>0115</phone>
<phone type='mobi'>07807</phone>
</customer>
EOX

m = REXML::XPath.match(d, '//customer/phone[@type = "home"]')
p m.to_s
# => <phone type='home'>0115</phone>

--
Ross Bamford - (E-Mail Removed)



 
Reply With Quote
 
 
 
 
William James
Guest
Posts: n/a
 
      02-23-2006
(E-Mail Removed) wrote:
> Is there anyway to do generic xpath's, for both elements and attributes
> (ie //customer/phone/@type --> "home")? REXML can do xpath to fetch
> elements (rexml_element.text('//customer/phone')) but can't seem to use
> xpath to find attributes.


class Array; alias atr first; alias txt last end
class String
def xtag(s)
scan( %r!
< #{s} (?: \s+ ( [^>]* ) )? / >
|
< #{s} (?: \s+ ( [^>]* ) )? >
( .*? ) </ #{s} >
!mx ).
map{ |unpaired, attr, data| h = { }
attr = ( unpaired || attr )
if attr
attr.scan( %r! ( \S+ ) = ( ["'] ) ( .*? ) \2 !x ){ |k,q,v|
h[k] = v }
end
[ h, data ]
}
end
def xpath(s)
s.scan(%r! [^/"]+ (?: "[^"]*" )? !x).inject([[nil,self]]){|ary,str|
if "@" == str[0,1]
str =~ /@(.*?)="(.*)"/
ary.select{|a,t| a[$1] == $2 }
else
return [] if [] == ary
ary[0].txt.xtag(str)
end
}
end
end


p DATA.read.xpath('customer/@loc="south"/phone/@type="mobi"')


__END__
<customer loc="north">
<phone type='home'>0115</phone>
<phone type='mobi'>07807</phone>
</customer>
<customer loc="south">
<phone type='home'>0319</phone>
<phone type='mobi'>09802</phone>
</customer>

 
Reply With Quote
 
listrecv@gmail.com
Guest
Posts: n/a
 
      02-24-2006
Thanks.

But I'd like to just extract the attribute value via Xpath.

If my Xpath is correct, I could just do '//customer/phone/@type' to get
'home'. Is there any way I can do this in Ruby?

 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      02-25-2006
(E-Mail Removed) wrote:
> Thanks.
>
> But I'd like to just extract the attribute value via Xpath.
>
> If my Xpath is correct, I could just do '//customer/phone/@type' to get
> 'home'. Is there any way I can do this in Ruby?



p DATA.read.xchain('customer/@loc="south"/phone').first.atr['type']

__END__
<customer loc="north">
<phone type='home'>0115</phone>
<phone type='mobi'>07807</phone>
</customer>
<customer loc="south">
<phone type='home'>0319</phone>
</customer>


The rest:

class Array; alias atr first; alias txt last end
class String
def xtag(s)
result = []
scan( %r!
< #{s} (?: \s+ ( [^>]* ) )? / >
|
< #{s} (?: \s+ ( [^>]* ) )? >
( .*? ) </ #{s} >
!mx ) \
{ |unpaired, attr, data| h = { }
attr = ( unpaired || attr )
attr.scan( %r{ ( \S+ ) = ( ["'] ) ( .*? ) \2 }x ){ |k,q,v|
h[k] = v } if attr
block_given? ? ( yield [ h, data ] ) : result << [ h, data ]
}
result
end
def xchain(s)
s.scan(%r! [^/"]+ (?: "[^"]*" )? !x).inject([[nil,self]]){|ary,str|
if "@" == str[0,1]
str =~ /@(.*?)="(.*)"/
ary.select{|a,t| a[$1] == $2 }
else
return [] if [] == ary
ary[0].txt.xtag(str)
end
}
end
end

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Memory leak" in javax.xml.xpath.XPath Marvin_123456 Java 4 07-29-2005 03:49 PM
XPath: efficiency in xpath expressions Tjerk Wolterink XML 1 11-13-2004 06:03 PM
Are there any XPath parsers that generate XPath trees? goog XML 0 01-14-2004 01:47 PM
XPath that does not include other XPath Anna XML 0 07-31-2003 07:55 AM
Problem selecting a node with XPATH if attribute value contains backslashes - how to force XPATH string to be treated as literal? Alastair Cameron XML 1 07-08-2003 07:24 PM



Advertisments