Sha256: 15f2469c515e486638df86c6e6fdc941c26bfc0ef9a93698760b9ac5091f4e6c

Contents?: true

Size: 1.75 KB

Versions: 12

Compression:

Stored size: 1.75 KB

Contents

# Provide hpricot API for libxml.  Provided by Michael Guterl,
# inspired by http://thebogles.com/blog/an-hpricot-style-interface-to-libxml

class String
  def to_libxml_doc
    xp = XML::Parser.new
    xp.string = self
    xp.parse
  end
end

module LibXML
  module XML
    class Document
      alias :search :find
    end
    
    class Node
      # find the child node with the given xpath
      def at(xpath)
        self.find_first(xpath)
      end

      # find the array of child nodes matching the given xpath
      def search(xpath)
        results = self.find(xpath).to_a
        if block_given?
          results.each do |result|
            yield result
          end
        end
        return results
      end

      def /(xpath)
        search(xpath)
      end

      # return the inner contents of this node as a string
      def inner_xml
        child.to_s
      end

      # alias for inner_xml
      def inner_html
        inner_xml
      end

      # return this node and its contents as an xml string
      def to_xml
        self.to_s
      end

      # alias for path
      def xpath
        self.path
      end

      def find_with_default_ns(xpath_expr, namespace=nil)
        find_base(xpath_expr, namespace || default_namespaces)
      end

      def find_first_with_default_ns(xpath_expr, namespace=nil)
        find_first_base(xpath_expr, namespace || default_namespaces)
      end

#      alias_method :find_base, :find unless method_defined?(:find_base)
#      alias_method :find, :find_with_default_ns
#      alias_method :find_first_base, :find_first unless method_defined?(:find_first_base)
#      alias_method :find_first, :find_first_with_default_ns
#      alias :child? :first?
#      alias :children? :first?
#      alias :child :first
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
libxml-ruby-0.9.1 lib/libxml/hpricot.rb
libxml-ruby-0.9.0-x86-mswin32-60 lib/libxml/hpricot.rb
libxml-ruby-0.9.1-x86-mswin32-60 lib/libxml/hpricot.rb
libxml-ruby-0.9.2 lib/libxml/hpricot.rb
libxml-ruby-0.9.2-x86-mswin32-60 lib/libxml/hpricot.rb
libxml-ruby-0.9.3-x86-mswin32-60 lib/libxml/hpricot.rb
libxml-ruby-0.9.0 lib/libxml/hpricot.rb
libxml-ruby-0.9.5-x86-mswin32-60 lib/libxml/hpricot.rb
libxml-ruby-0.9.4-x86-mswin32-60 lib/libxml/hpricot.rb
libxml-ruby-0.9.3 lib/libxml/hpricot.rb
libxml-ruby-0.9.5 lib/libxml/hpricot.rb
libxml-ruby-0.9.4 lib/libxml/hpricot.rb