class Nokogiri::XML::Node

Public Instance Methods

get_node(element, name) click to toggle source
# File lax/patches/patch-nokogiri-xml-node.rb, line 84
def get_node element, name
  element.element_children.each do | child |
    if child.name == name
      return child
    end
  end if element && element.respond_to?(:element_children)
  return nil
end
get_nodes(element, name, find_all = false) click to toggle source
# File lax/patches/patch-nokogiri-xml-node.rb, line 93
def get_nodes element, name, find_all = false
  nodes = []
  element.element_children.each do | child |
    if child.name == name
      nodes << child
    end
    if find_all
      found = get_nodes(child, name, find_all)
      found.each do |node|
        nodes<<node if node && node.element?
      end
    end
  end if element && element.respond_to?(:element_children)
  nodes
end
simple_at_xpath(path) click to toggle source
# File lax/patches/patch-nokogiri-xml-node.rb, line 12
def simple_at_xpath path
  begin
    path = path.split('/')
    result = self
    path.each do |path|
      result = case path
        when 'text()'
          result.child if result && result.respond_to?(:child)
        when /^@/
          result.respond_to?(:attribute) ? result.attribute(path[1..path.size]) : nil
        when ".."
          parent = result.parent if result.respond_to?(:parent)
        when "."
          result
        else
          get_node result, path
        end
      break if not result
    end
    return result if result != self
  rescue Exception => e
    PIM.log_warn("Failure in simple_at_xpath: '(#{path})'", e)
  end
end
simple_xpath(path) click to toggle source
# File lax/patches/patch-nokogiri-xml-node.rb, line 37
def simple_xpath path
  begin
    path = path.split('/')
    elements = [self]
    results = Nokogiri::XML::NodeSet.new(document)
    previous_path_element = nil
    path.each_with_index do |path_element, index|
      new_elements = []
      elements.each do | element |
        result = case path_element
          when 'text()'
            element.child if element && element.respond_to?(:child)
          when /^@/
            element.respond_to?(:attribute) ? element.attribute(path_element[1..path_element.size]) : nil
          when ".."
            element.parent if element.respond_to?(:parent)
          when "."
            element
          else
            if path_element && path_element.length > 0
              find_all = previous_path_element == '' && index >0
              get_nodes element, path_element, find_all
            else
              element
            end
          end
        if result.is_a?Array
          result.each do |element|
            new_elements << element
          end
        else
          new_elements << result
        end
      end
      elements = new_elements
      previous_path_element = path_element
    end
    elements.each do |element|
      results.push element
    end
    return results
  rescue Exception => e
    PIM.log_warn("Failure in simple_xpath: '(#{path})'", e)
  end

end