| Module | REXML::Functions |
| In: |
lib/rexml/functions.rb
|
If you add a method, keep in mind two things: (1) the first argument will always be a list of nodes from which to filter. In the case of context methods (such as position), the function should return an array with a value for each child in the array. (2) all method calls from XML will have "-" replaced with "_". Therefore, in XML, "local-name()" is identical (and actually becomes) "local_name()"
UNTESTED
# File lib/rexml/functions.rb, line 251
251: def Functions::boolean( object=nil )
252: if object.kind_of? String
253: if object =~ /\d+/u
254: return object.to_f != 0
255: else
256: return object.size > 0
257: end
258: elsif object.kind_of? Array
259: object = object.find{|x| x and true}
260: end
261: return object ? true : false
262: end
# File lib/rexml/functions.rb, line 346
346: def Functions::ceiling( number )
347: number(number).ceil
348: end
# File lib/rexml/functions.rb, line 298
298: def Functions::compare_language lang1, lang2
299: lang2.downcase.index(lang1.downcase) == 0
300: end
UNTESTED
# File lib/rexml/functions.rb, line 131
131: def Functions::concat( *objects )
132: objects.join
133: end
Fixed by Mike Stok
# File lib/rexml/functions.rb, line 141
141: def Functions::contains( string, test )
142: string(string).include? string(test)
143: end
# File lib/rexml/functions.rb, line 39
39: def Functions::count( node_set )
40: node_set.size
41: end
# File lib/rexml/functions.rb, line 342
342: def Functions::floor( number )
343: number(number).floor
344: end
Helper method.
# File lib/rexml/functions.rb, line 66
66: def Functions::get_namespace( node_set = nil )
67: if node_set == nil
68: yield @@context[:node] if defined? @@context[:node].namespace
69: else
70: if node_set.respond_to? :each
71: node_set.each { |node| yield node if defined? node.namespace }
72: elsif node_set.respond_to? :namespace
73: yield node_set
74: end
75: end
76: end
UNTESTED
# File lib/rexml/functions.rb, line 280
280: def Functions::lang( language )
281: lang = false
282: node = @@context[:node]
283: attr = nil
284: until node.nil?
285: if node.node_type == :element
286: attr = node.attributes["xml:lang"]
287: unless attr.nil?
288: lang = compare_language(string(language), attr)
289: break
290: else
291: end
292: end
293: node = node.parent
294: end
295: lang
296: end
UNTESTED
# File lib/rexml/functions.rb, line 49
49: def Functions::local_name( node_set=nil )
50: get_namespace( node_set ) do |node|
51: return node.local_name
52: end
53: end
# File lib/rexml/functions.rb, line 362
362: def Functions::method_missing( id )
363: puts "METHOD MISSING #{id.id2name}"
364: XPath.match( @@context[:node], id.id2name )
365: end
# File lib/rexml/functions.rb, line 59
59: def Functions::name( node_set=nil )
60: get_namespace( node_set ) do |node|
61: node.expanded_name
62: end
63: end
# File lib/rexml/functions.rb, line 16
16: def Functions::namespace_context ; @@namespace_context ; end
# File lib/rexml/functions.rb, line 14
14: def Functions::namespace_context=(x) ; @@namespace_context=x ; end
# File lib/rexml/functions.rb, line 55
55: def Functions::namespace_uri( node_set=nil )
56: get_namespace( node_set ) {|node| node.namespace}
57: end
UNTESTED
# File lib/rexml/functions.rb, line 204
204: def Functions::normalize_space( string=nil )
205: string = string(@@context[:node]) if string.nil?
206: if string.kind_of? Array
207: string.collect{|x| string.to_s.strip.gsub(/\s+/um, ' ') if string}
208: else
209: string.to_s.strip.gsub(/\s+/um, ' ')
210: end
211: end
UNTESTED
# File lib/rexml/functions.rb, line 265
265: def Functions::not( object )
266: not boolean( object )
267: end
a string that consists of optional whitespace followed by an optional minus sign followed by a Number followed by whitespace is converted to the IEEE 754 number that is nearest (according to the IEEE 754 round-to-nearest rule) to the mathematical value represented by the string; any other string is converted to NaN
boolean true is converted to 1; boolean false is converted to 0
a node-set is first converted to a string as if by a call to the string function and then converted in the same way as a string argument
an object of a type other than the four basic types is converted to a number in a way that is dependent on that type
# File lib/rexml/functions.rb, line 315
315: def Functions::number( object=nil )
316: object = @@context[:node] unless object
317: case object
318: when true
319: Float(1)
320: when false
321: Float(0)
322: when Array
323: number(string( object ))
324: when Numeric
325: object.to_f
326: else
327: str = string( object )
328: #puts "STRING OF #{object.inspect} = #{str}"
329: if str =~ /^\d+/
330: object.to_s.to_f
331: else
332: (0.0 / 0.0)
333: end
334: end
335: end
# File lib/rexml/functions.rb, line 358
358: def Functions::processing_instruction( node )
359: node.node_type == :processing_instruction
360: end
# File lib/rexml/functions.rb, line 350
350: def Functions::round( number )
351: begin
352: number(number).round
353: rescue FloatDomainError
354: number(number)
355: end
356: end
Fixed by Mike Stok
# File lib/rexml/functions.rb, line 136
136: def Functions::starts_with( string, test )
137: string(string).index(string(test)) == 0
138: end
A node-set is converted to a string by returning the string-value of the node in the node-set that is first in document order. If the node-set is empty, an empty string is returned.
A number is converted to a string as follows
NaN is converted to the string NaN
positive zero is converted to the string 0
negative zero is converted to the string 0
positive infinity is converted to the string Infinity
negative infinity is converted to the string -Infinity
if the number is an integer, the number is represented in decimal form as a Number with no decimal point and no leading zeros, preceded by a minus sign (-) if the number is negative
otherwise, the number is represented in decimal form as a Number including a decimal point with at least one digit before the decimal point and at least one digit after the decimal point, preceded by a minus sign (-) if the number is negative; there must be no leading zeros before the decimal point apart possibly from the one required digit immediately before the decimal point; beyond the one required digit after the decimal point there must be as many, but only as many, more digits as are needed to uniquely distinguish the number from all other IEEE 754 numeric values.
The boolean false value is converted to the string false. The boolean true value is converted to the string true.
An object of a type other than the four basic types is converted to a string in a way that is dependent on that type.
# File lib/rexml/functions.rb, line 113
113: def Functions::string( object=nil )
114: #object = @context unless object
115: if object.instance_of? Array
116: string( object[0] )
117: elsif defined? object.node_type
118: if object.node_type == :attribute
119: object.value
120: elsif object.node_type == :element
121: object.text
122: else
123: object.to_s
124: end
125: else
126: object.to_s
127: end
128: end
UNTESTED
# File lib/rexml/functions.rb, line 199
199: def Functions::string_length( string )
200: string(string).length
201: end
Take equal portions of Mike Stok and Sean Russell; mix vigorously, and pour into a tall, chilled glass. Serves 10,000.
# File lib/rexml/functions.rb, line 166
166: def Functions::substring( string, start, length=nil )
167: ruby_string = string(string)
168: ruby_length = if length.nil?
169: ruby_string.length.to_f
170: else
171: number(length)
172: end
173: ruby_start = number(start)
174:
175: # Handle the special cases
176: return '' if (
177: ruby_length.nan? or
178: ruby_start.nan? or
179: ruby_start.infinite?
180: )
181:
182: infinite_length = ruby_length.infinite? == 1
183: ruby_length = ruby_string.length if infinite_length
184:
185: # Now, get the bounds. The XPath bounds are 1..length; the ruby bounds
186: # are 0..length. Therefore, we have to offset the bounds by one.
187: ruby_start = ruby_start.round - 1
188: ruby_length = ruby_length.round
189:
190: if ruby_start < 0
191: ruby_length += ruby_start unless infinite_length
192: ruby_start = 0
193: end
194: return '' if ruby_length <= 0
195: ruby_string[ruby_start,ruby_length]
196: end
Kouhei fixed this too
# File lib/rexml/functions.rb, line 157
157: def Functions::substring_after( string, test )
158: ruby_string = string(string)
159: test_string = string(test)
160: return $1 if ruby_string =~ /#{test}(.*)/
161: ""
162: end
Kouhei fixed this
# File lib/rexml/functions.rb, line 146
146: def Functions::substring_before( string, test )
147: ruby_string = string(string)
148: ruby_index = ruby_string.index(string(test))
149: if ruby_index.nil?
150: ""
151: else
152: ruby_string[ 0...ruby_index ]
153: end
154: end
# File lib/rexml/functions.rb, line 337
337: def Functions::sum( nodes )
338: nodes = [nodes] unless nodes.kind_of? Array
339: nodes.inject(0) { |r,n| r += number(string(n)) }
340: end
# File lib/rexml/functions.rb, line 21
21: def Functions::text( )
22: if @@context[:node].node_type == :element
23: return @@context[:node].find_all{|n| n.node_type == :text}.collect{|n| n.value}
24: elsif @@context[:node].node_type == :text
25: return @@context[:node].value
26: else
27: return false
28: end
29: end
This is entirely Mike Stok‘s beast
# File lib/rexml/functions.rb, line 214
214: def Functions::translate( string, tr1, tr2 )
215: from = string(tr1)
216: to = string(tr2)
217:
218: # the map is our translation table.
219: #
220: # if a character occurs more than once in the
221: # from string then we ignore the second &
222: # subsequent mappings
223: #
224: # if a charactcer maps to nil then we delete it
225: # in the output. This happens if the from
226: # string is longer than the to string
227: #
228: # there's nothing about - or ^ being special in
229: # http://www.w3.org/TR/xpath#function-translate
230: # so we don't build ranges or negated classes
231:
232: map = Hash.new
233: 0.upto(from.length - 1) { |pos|
234: from_char = from[pos]
235: unless map.has_key? from_char
236: map[from_char] =
237: if pos < to.length
238: to[pos]
239: else
240: nil
241: end
242: end
243: }
244:
245: string(string).unpack('U*').collect { |c|
246: if map.has_key? c then map[c] else c end
247: }.compact.pack('U*')
248: end