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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
require "test/unit/testcase"
require "rexml/document"
module REXMLTests
class FunctionsTester < Test::Unit::TestCase
include REXML
def test_functions
# trivial text() test
# confuse-a-function
source = "<a>more <b id='1'/><b id='2'>dumb</b><b id='3'/><c/> text</a>"
doc = Document.new source
res = ""
XPath::each(doc.root, "text()") {|val| res << val.to_s}
assert_equal "more text", res
res = XPath::first(doc.root, "b[last()]")
assert_equal '3', res.attributes['id']
res = XPath::first(doc.root, "b[position()=2]")
assert_equal '2', res.attributes['id']
res = XPath::first(doc.root, "*[name()='c']")
assert_equal "c", res.name
end
# Contributed by Mike Stok
def test_starts_with
source = <<-EOF
<foo>
<a href="mailto:a@b.c">a@b.c</a>
<a href="http://www.foo.com">http://www.foo.com</a>
</foo>
EOF
doc = Document.new source
mailtos = doc.elements.to_a("//a[starts-with(@href, 'mailto:')]")
assert_equal 1, mailtos.size
assert_equal "mailto:a@b.c", mailtos[0].attributes['href']
ailtos = doc.elements.to_a("//a[starts-with(@href, 'ailto:')]")
assert_equal 0, ailtos.size
end
def test_string_length
doc = Document.new <<-EOF
<AAA>
<Q/>
<SSSS/>
<BB/>
<CCC/>
<DDDDDDDD/>
<EEEE/>
</AAA>
EOF
assert doc, "create doc"
set = doc.elements.to_a("//*[string-length(name()) = 3]")
assert_equal 2, set.size, "nodes with names length = 3"
set = doc.elements.to_a("//*[string-length(name()) < 3]")
assert_equal 2, set.size, "nodes with names length < 3"
set = doc.elements.to_a("//*[string-length(name()) > 3]")
assert_equal 3, set.size, "nodes with names length > 3"
end
# Test provided by Mike Stok
def test_contains
source = <<-EOF
<foo>
<a href="mailto:a@b.c">a@b.c</a>
<a href="http://www.foo.com">http://www.foo.com</a>
</foo>
EOF
doc = Document.new source
[['o', 2], ['foo', 1], ['bar', 0]].each { |test|
search, expected = test
set = doc.elements.to_a("//a[contains(@href, '#{search}')]")
assert_equal expected, set.size
}
end
# Mike Stok and Sean Russell
def test_substring
# examples from http://www.w3.org/TR/xpath#function-substring
doc = Document.new('<test string="12345" />')
#puts XPath.first(d, 'node()[0 + 1]')
#d = Document.new("<a b='1'/>")
#puts XPath.first(d, 'a[0 mod 0]')
[ [1.5, 2.6, '234'],
[0, 3, '12'],
[0, '0 div 0', ''],
[1, '0 div 0', ''],
['-42', '1 div 0', '12345'],
['-1 div 0', '1 div 0', '']
].each { |start, length, expected|
set = doc.elements.to_a("//test[substring(@string, #{start}, #{length}) = '#{expected}']")
assert_equal 1, set.size, "#{start}, #{length}, '#{expected}'"
}
end
def test_substring_angrez
testString = REXML::Functions::substring_after("helloworld","hello")
assert_equal( 'world', testString )
end
def test_translate
source = <<-EOF
<doc>
<case name='w3c one' result='BAr' /> <!-- w3c -->
<case name='w3c two' result='AAA' /> <!-- w3c -->
<case name='alchemy' result="gold" /> <!-- mike -->
<case name='vbxml one' result='A Space Odyssey' />
<case name='vbxml two' result='AbCdEf' />
</doc>
EOF
doc = Document.new(source)
[ ['bar', 'abc', 'ABC', 'w3c one'],
['--aaa--','abc-','ABC', 'w3c two'],
['lead', 'dear language', 'doll groover', 'alchemy'],
['A Space Odissei', 'i', 'y', 'vbxml one'],
['abcdefg', 'aceg', 'ACE', 'vbxml two'],
].each { |arg1, arg2, arg3, name|
translate = "translate('#{arg1}', '#{arg2}', '#{arg3}')"
set = doc.elements.to_a("//case[@result = #{translate}]")
assert_equal 1, set.size, translate
assert_equal name, set[0].attributes['name']
}
end
def test_name
d = REXML::Document.new("<a xmlns:x='foo'><b/><x:b/></a>")
assert_equal 1, d.root.elements.to_a('*[name() = "b"]').size
assert_equal 1, d.elements.to_a('//*[name() = "x:b"]').size
end
def test_local_name
d = REXML::Document.new("<a xmlns:x='foo'><b/><x:b/></a>")
assert_equal 2, d.root.elements.to_a('*[local_name() = "b"]').size
assert_equal 2, d.elements.to_a('//*[local_name() = "b"]').size
end
def test_substring2
doc = Document.new('<test string="12345" />')
assert_equal(1,doc.elements.to_a("//test[substring(@string,2)='2345']").size)
end
# Submitted by Kouhei
def test_floor_ceiling_round
source = "<a><b id='1'/><b id='2'/><b id='3'/></a>"
doc = REXML::Document.new(source)
id_1 = doc.elements["/a/b[@id='1']"]
id_2 = doc.elements["/a/b[@id='2']"]
id_3 = doc.elements["/a/b[@id='3']"]
good = {
"floor" => [[], [id_1], [id_2], [id_3]],
"ceiling" => [[id_1], [id_2], [id_3], []],
"round" => [[id_1], [id_2], [id_3], []]
}
good.each do |key, value|
(0..3).each do |i|
xpath = "//b[number(@id) = #{key}(#{i+0.5})]"
assert_equal(value[i], REXML::XPath.match(doc, xpath))
end
end
good["round"] = [[], [id_1], [id_2], [id_3]]
good.each do |key, value|
(0..3).each do |i|
xpath = "//b[number(@id) = #{key}(#{i+0.4})]"
assert_equal(value[i], REXML::XPath.match(doc, xpath))
end
end
end
# Submitted by Kou
def test_lang
d = Document.new(<<-XML)
<a xml:lang="en">
<b xml:lang="ja">
<c xml:lang="fr"/>
<d/>
<e xml:lang="ja-JP"/>
<f xml:lang="en-US"/>
</b>
</a>
XML
assert_equal(1, d.elements.to_a("//*[lang('fr')]").size)
assert_equal(3, d.elements.to_a("//*[lang('ja')]").size)
assert_equal(2, d.elements.to_a("//*[lang('en')]").size)
assert_equal(1, d.elements.to_a("//*[lang('en-us')]").size)
d = Document.new(<<-XML)
<root>
<para xml:lang="en"/>
<div xml:lang="en"><para/></div>
<para xml:lang="EN"/>
<para xml:lang="en-us"/>
</root>
XML
assert_equal(5, d.elements.to_a("//*[lang('en')]").size)
end
def test_ticket_60
document = REXML::Document.new("<a><b>A</b><b>1</b></a>")
assert_equal( "A", REXML::XPath.first(document, '//b[.="A"]').text )
assert_equal( "1", REXML::XPath.first(document, '//b[.="1"]').text )
end
def test_normalize_space
source = "<a><!--COMMENT A--><b><!-- COMMENT A --></b></a>"
doc = REXML::Document.new(source)
predicate = "string(.)=normalize_space('\nCOMMENT \n A \n\n ')"
m = REXML::XPath.match(doc, "//comment()[#{predicate}]")
assert_equal( [REXML::Comment.new("COMMENT A")], m )
end
end
end
|