-
Notifications
You must be signed in to change notification settings - Fork 12
/
xpath_usage.intin.rb
51 lines (40 loc) · 1.01 KB
/
xpath_usage.intin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#:invisible:
$:.unshift "../lib" #<=
#:visible:
require 'xml/xxpath'
d=REXML::Document.new <<EOS
<foo>
<bar>
<baz key="work">Java</baz>
<baz key="play">Ruby</baz>
</bar>
<bar>
<baz key="ab">hello</baz>
<baz key="play">scrabble</baz>
<baz key="xy">goodbye</baz>
</bar>
<more>
<baz key="play">poker</baz>
</more>
</foo>
EOS
####read access
path=XML::XXPath.new("/foo/bar[2]/baz")
## path.all(document) gives all elements matching path in document
path.all(d)#<=
## loop over them
path.each(d){|elt| puts elt.text}#<=
## the first of those
path.first(d)#<=
## no match here (only three "baz" elements)
path2=XML::XXPath.new("/foo/bar[2]/baz[4]")
path2.all(d)#<=
#:handle_exceptions:
## "first" raises XML::XXPathError in such cases...
path2.first(d)#<=
#:no_exceptions:
##...unless we allow nil returns
path2.first(d,:allow_nil=>true)#<=
##attribute nodes can also be returned
keysPath=XML::XXPath.new("/foo/*/*/@key")
keysPath.all(d).map{|attr|attr.text}#<=