模块 REXML::Functions

如果您添加一个方法,请记住两件事:(1) 第一个参数始终是要从中筛选的节点列表。对于上下文方法(例如 position),该函数应返回一个数组,其中包含数组中每个子项的值。(2) 来自 XML 的所有方法调用都将用“_”替换“-”。因此,在 XML 中,“local-name()”与“local_name()”相同(实际上变成了“local_name()”)

常量

INTERNAL_METHODS
namespace_context
variables

公共类方法

boolean(object=@@context[:node]) 点击以切换源代码
# File rexml-3.4.0/lib/rexml/functions.rb, line 317
def Functions::boolean(object=@@context[:node])
  case object
  when true, false
    object
  when Float
    return false if object.zero?
    return false if object.nan?
    true
  when Numeric
    not object.zero?
  when String
    not object.empty?
  when Array
    not object.empty?
  else
    object ? true : false
  end
end
ceiling( number ) 点击以切换源代码
# File rexml-3.4.0/lib/rexml/functions.rb, line 417
def Functions::ceiling( number )
  number(number).ceil
end
compare_language(lang1, lang2) 点击以切换源代码
# File rexml-3.4.0/lib/rexml/functions.rb, line 370
def Functions::compare_language lang1, lang2
  lang2.downcase.index(lang1.downcase) == 0
end
concat( *objects ) 点击以切换源代码
# File rexml-3.4.0/lib/rexml/functions.rb, line 190
def Functions::concat( *objects )
  concatenated = ""
  objects.each do |object|
    concatenated << string(object)
  end
  concatenated
end
contains( string, test ) 点击以切换源代码

由 Mike Stok 修复

# File rexml-3.4.0/lib/rexml/functions.rb, line 204
def Functions::contains( string, test )
  string(string).include?(string(test))
end
context=(value) 点击以切换源代码
# File rexml-3.4.0/lib/rexml/functions.rb, line 38
def Functions::context=(value); @@context = value; end
count( node_set ) 点击以切换源代码

返回给定节点列表的大小。

# File rexml-3.4.0/lib/rexml/functions.rb, line 60
def Functions::count( node_set )
  node_set.size
end
false( ) 点击以切换源代码

未测试

# File rexml-3.4.0/lib/rexml/functions.rb, line 347
def Functions::false(  )
  false
end
floor( number ) 点击以切换源代码
# File rexml-3.4.0/lib/rexml/functions.rb, line 413
def Functions::floor( number )
  number(number).floor
end
get_namespace( node_set = nil ) { |context| ... } 点击以切换源代码

辅助方法。

# File rexml-3.4.0/lib/rexml/functions.rb, line 87
def Functions::get_namespace( node_set = nil )
  if node_set == nil
    yield @@context[:node] if @@context[:node].respond_to?(:namespace)
  else
    if node_set.respond_to? :each
      result = []
      node_set.each do |node|
        result << yield(node) if node.respond_to?(:namespace)
      end
      result
    elsif node_set.respond_to? :namespace
      yield node_set
    end
  end
end
id( object ) 点击以切换源代码

由于 REXML 是非验证的,因此此方法未实现,因为它需要 DTD

# File rexml-3.4.0/lib/rexml/functions.rb, line 66
def Functions::id( object )
end
lang( language ) 点击以切换源代码

未测试

# File rexml-3.4.0/lib/rexml/functions.rb, line 352
def Functions::lang( language )
  lang = false
  node = @@context[:node]
  attr = nil
  until node.nil?
    if node.node_type == :element
      attr = node.attributes["xml:lang"]
      unless attr.nil?
        lang = compare_language(string(language), attr)
        break
      else
      end
    end
    node = node.parent
  end
  lang
end
last( ) 点击以切换源代码

返回给定节点列表的最后一个节点。

# File rexml-3.4.0/lib/rexml/functions.rb, line 51
def Functions::last( )
  @@context[:size]
end
local_name(node_set=nil) 点击以切换源代码
# File rexml-3.4.0/lib/rexml/functions.rb, line 69
def Functions::local_name(node_set=nil)
  get_namespace(node_set) do |node|
    return node.local_name
  end
  ""
end
name( node_set=nil ) 点击以切换源代码
# File rexml-3.4.0/lib/rexml/functions.rb, line 80
def Functions::name( node_set=nil )
  get_namespace( node_set ) do |node|
    node.expanded_name
  end
end
namespace_context() 点击以切换源代码
# File rexml-3.4.0/lib/rexml/functions.rb, line 35
def Functions::namespace_context ; @@namespace_context ; end
namespace_context=(x) 点击以切换源代码
# File rexml-3.4.0/lib/rexml/functions.rb, line 33
def Functions::namespace_context=(x) ; @@namespace_context=x ; end
namespace_uri( node_set=nil ) 点击以切换源代码
# File rexml-3.4.0/lib/rexml/functions.rb, line 76
def Functions::namespace_uri( node_set=nil )
  get_namespace( node_set ) {|node| node.namespace}
end
normalize_space( string=nil ) 点击以切换源代码
# File rexml-3.4.0/lib/rexml/functions.rb, line 265
def Functions::normalize_space( string=nil )
  string = string(@@context[:node]) if string.nil?
  if string.kind_of? Array
    string.collect{|x| x.to_s.strip.gsub(/\s+/um, ' ') if x}
  else
    string.to_s.strip.gsub(/\s+/um, ' ')
  end
end
not( object ) 点击以切换源代码

未测试

# File rexml-3.4.0/lib/rexml/functions.rb, line 337
def Functions::not( object )
  not boolean( object )
end
number(object=@@context[:node]) 点击以切换源代码

由可选的空白符、后跟可选的减号、后跟一个数字、后跟空白符组成的字符串被转换为最接近(根据 IEEE 754 四舍五入到最近规则)该字符串表示的数学值的 IEEE 754 数字;任何其他字符串都被转换为 NaN

布尔值 true 被转换为 1;布尔值 false 被转换为 0

节点集首先通过调用 string 函数转换为字符串,然后以与字符串参数相同的方式转换

其他四种基本类型以外的类型的对象以依赖于该类型的方式转换为数字

# File rexml-3.4.0/lib/rexml/functions.rb, line 387
def Functions::number(object=@@context[:node])
  case object
  when true
    Float(1)
  when false
    Float(0)
  when Array
    number(string(object))
  when Numeric
    object.to_f
  else
    str = string(object)
    case str.strip
    when /\A\s*(-?(?:\d+(?:\.\d*)?|\.\d+))\s*\z/
      $1.to_f
    else
      Float::NAN
    end
  end
end
position( ) 点击以切换源代码
# File rexml-3.4.0/lib/rexml/functions.rb, line 55
def Functions::position( )
  @@context[:index]
end
processing_instruction( node ) 点击以切换源代码
# File rexml-3.4.0/lib/rexml/functions.rb, line 432
def Functions::processing_instruction( node )
  node.node_type == :processing_instruction
end
round( number ) 点击以切换源代码
# File rexml-3.4.0/lib/rexml/functions.rb, line 421
def Functions::round( number )
  number = number(number)
  begin
    neg = number.negative?
    number = number.abs.round
    neg ? -number : number
  rescue FloatDomainError
    number
  end
end
send(name, *args) 点击以切换源代码
调用超类方法
# File rexml-3.4.0/lib/rexml/functions.rb, line 436
def Functions::send(name, *args)
  if @@available_functions[name.to_sym]
    super
  else
    # TODO: Maybe, this is not XPath spec behavior.
    # This behavior must be reconsidered.
    XPath.match(@@context[:node], name.to_s)
  end
end
singleton_method_added(name) 点击以切换源代码
# File rexml-3.4.0/lib/rexml/functions.rb, line 26
def singleton_method_added(name)
  unless INTERNAL_METHODS.include?(name)
    @@available_functions[name] = true
  end
end
starts_with( string, test ) 点击以切换源代码

由 Mike Stok 修复

# File rexml-3.4.0/lib/rexml/functions.rb, line 199
def Functions::starts_with( string, test )
  string(string).index(string(test)) == 0
end
string( object=@@context[:node] ) 点击以切换源代码

通过返回文档顺序中第一个节点集中节点的字符串值,将节点集转换为字符串。如果节点集为空,则返回一个空字符串。

数字按如下方式转换为字符串

NaN 被转换为字符串 NaN

正零被转换为字符串 0

负零被转换为字符串 0

正无穷大被转换为字符串 Infinity

负无穷大被转换为字符串 -Infinity

如果该数字是整数,则该数字以十进制形式表示为没有小数点且没有前导零的数字,如果该数字为负数,则在其前面加上减号 (-)

否则,该数字以十进制形式表示为数字,包括小数点,小数点前至少有一位数字,小数点后至少有一位数字,如果该数字为负数,则在其前面加上减号 (-); 小数点之前除了紧接小数点之前所需的数字之外,不能有前导零; 在小数点后所需的一个数字之外,必须有尽可能多的数字,但只能有足够多的数字,以将该数字与所有其他 IEEE 754 数值唯一区分开来。

布尔值 false 被转换为字符串 false。布尔值 true 被转换为字符串 true。

其他四种基本类型以外的类型的对象以依赖于该类型的方式转换为字符串。

# File rexml-3.4.0/lib/rexml/functions.rb, line 138
def Functions::string( object=@@context[:node] )
  if object.respond_to?(:node_type)
    case object.node_type
    when :attribute
      object.value
    when :element
      string_value(object)
    when :document
      string_value(object.root)
    when :processing_instruction
      object.content
    else
      object.to_s
    end
  else
    case object
    when Array
      string(object[0])
    when Float
      if object.nan?
        "NaN"
      else
        integer = object.to_i
        if object == integer
          "%d" % integer
        else
          object.to_s
        end
      end
    else
      object.to_s
    end
  end
end
string_length( string ) 点击以切换源代码

未测试

# File rexml-3.4.0/lib/rexml/functions.rb, line 261
def Functions::string_length( string )
  string(string).length
end
string_value( o ) 点击以切换源代码

通过返回文档顺序中第一个节点集中节点的每个子节点的字符串值的串联,将节点集转换为字符串。如果节点集为空,则返回一个空字符串。

# File rexml-3.4.0/lib/rexml/functions.rb, line 178
def Functions::string_value( o )
  rv = ""
  o.children.each { |e|
    if e.node_type == :text
      rv << e.to_s
    elsif e.node_type == :element
      rv << string_value( e )
    end
  }
  rv
end
substring( string, start, length=nil ) 点击以切换源代码

取 Mike Stok 和 Sean Russell 的相等份量;用力混合,倒入一个高高的冰镇玻璃杯中。服务 10,000 人。

# File rexml-3.4.0/lib/rexml/functions.rb, line 228
def Functions::substring( string, start, length=nil )
  ruby_string = string(string)
  ruby_length = if length.nil?
                  ruby_string.length.to_f
                else
                  number(length)
                end
  ruby_start = number(start)

  # Handle the special cases
  return '' if (
    ruby_length.nan? or
    ruby_start.nan? or
    ruby_start.infinite?
  )

  infinite_length = ruby_length.infinite? == 1
  ruby_length = ruby_string.length if infinite_length

  # Now, get the bounds.  The XPath bounds are 1..length; the ruby bounds
  # are 0..length.  Therefore, we have to offset the bounds by one.
  ruby_start = round(ruby_start) - 1
  ruby_length = round(ruby_length)

  if ruby_start < 0
   ruby_length += ruby_start unless infinite_length
   ruby_start = 0
  end
  return '' if ruby_length <= 0
  ruby_string[ruby_start,ruby_length]
end
substring_after( string, test ) 点击以切换源代码

Kouhei 也修复了这个

# File rexml-3.4.0/lib/rexml/functions.rb, line 220
def Functions::substring_after( string, test )
  ruby_string = string(string)
  return $1 if ruby_string =~ /#{test}(.*)/
  ""
end
substring_before( string, test ) 点击以切换源代码

Kouhei 修复了这个

# File rexml-3.4.0/lib/rexml/functions.rb, line 209
def Functions::substring_before( string, test )
  ruby_string = string(string)
  ruby_index = ruby_string.index(string(test))
  if ruby_index.nil?
    ""
  else
    ruby_string[ 0...ruby_index ]
  end
end
sum( nodes ) 点击以切换源代码
# File rexml-3.4.0/lib/rexml/functions.rb, line 408
def Functions::sum( nodes )
  nodes = [nodes] unless nodes.kind_of? Array
  nodes.inject(0) { |r,n| r + number(string(n)) }
end
text( ) 点击以切换源代码
# File rexml-3.4.0/lib/rexml/functions.rb, line 40
def Functions::text( )
  if @@context[:node].node_type == :element
    return @@context[:node].find_all{|n| n.node_type == :text}.collect{|n| n.value}
  elsif @@context[:node].node_type == :text
    return @@context[:node].value
  else
    return false
  end
end
translate( string, tr1, tr2 ) 点击以切换源代码

这完全是 Mike Stok 的杰作

# File rexml-3.4.0/lib/rexml/functions.rb, line 275
def Functions::translate( string, tr1, tr2 )
  from = string(tr1)
  to = string(tr2)

  # the map is our translation table.
  #
  # if a character occurs more than once in the
  # from string then we ignore the second &
  # subsequent mappings
  #
  # if a character maps to nil then we delete it
  # in the output.  This happens if the from
  # string is longer than the to string
  #
  # there's nothing about - or ^ being special in
  # http://www.w3.org/TR/xpath#function-translate
  # so we don't build ranges or negated classes

  map = Hash.new
  0.upto(from.length - 1) { |pos|
    from_char = from[pos]
    unless map.has_key? from_char
      map[from_char] =
      if pos < to.length
        to[pos]
      else
        nil
      end
    end
  }

  if ''.respond_to? :chars
    string(string).chars.collect { |c|
      if map.has_key? c then map[c] else c end
    }.compact.join
  else
    string(string).unpack('U*').collect { |c|
      if map.has_key? c then map[c] else c end
    }.compact.pack('U*')
  end
end
true( ) 点击以切换源代码

未测试

# File rexml-3.4.0/lib/rexml/functions.rb, line 342
def Functions::true( )
  true
end
variables() 点击以切换源代码
# File rexml-3.4.0/lib/rexml/functions.rb, line 36
def Functions::variables ; @@variables ; end
variables=(x) 点击以切换源代码
# File rexml-3.4.0/lib/rexml/functions.rb, line 34
def Functions::variables=(x) ; @@variables=x ; end