Sha256: 4ffb2ab319230d3120835ac5c82c38cba3f99fcab2409579ee95353d4d11987d

Contents?: true

Size: 1.28 KB

Versions: 8

Compression:

Stored size: 1.28 KB

Contents

require 'libxml'

module ROXML
  module XML # :nodoc:all
    Document = LibXML::XML::Document
    Node = LibXML::XML::Node
    Parser = LibXML::XML::Parser

    module NamespacedSearch
      def search(xpath)
        if default_namespace && !xpath.include?(':')
          find(namespaced(xpath),
               in_default_namespace(default_namespace.href))
        else
          find(xpath)
        end
      end

    private
      def namespaced(xpath)
        xpath.between('/') do |component|
          if component =~ /\w+/ && !component.include?(':') && !component.starts_with?('@')
            in_default_namespace(component)
          else
            component
          end
        end
      end

      def in_default_namespace(name)
        "roxmldefaultnamespace:#{name}"
      end
    end

    class Document
      include NamespacedSearch

    private
      delegate :default_namespace, :to => :root
    end

    class Node
      include NamespacedSearch

    private
      def default_namespace
        @default_namespace ||= namespace && namespace.find {|n| n.to_s.nil? }
      end
    end

    class Parser
      class << self
        def parse(str_data)
          string(str_data).parse
        end

        def parse_file(path)
          file(path).parse
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
Empact-roxml-2.2 lib/roxml/xml/libxml.rb
Empact-roxml-2.3.0 lib/roxml/xml/libxml.rb
Empact-roxml-2.3.1 lib/roxml/xml/libxml.rb
Empact-roxml-2.4.0 lib/roxml/xml/libxml.rb
Empact-roxml-2.4.1 lib/roxml/xml/libxml.rb
roxml-2.4.0 lib/roxml/xml/libxml.rb
roxml-2.3.2 lib/roxml/xml/libxml.rb
roxml-2.4.1 lib/roxml/xml/libxml.rb