Class: Vectory::SvgMapping::Namespace

Inherits:
Object
  • Object
show all
Defined in:
lib/vectory/svg_mapping.rb

Overview

Namespace helper for XML namespace handling

Instance Method Summary collapse

Constructor Details

#initialize(xmldoc) ⇒ Namespace

Returns a new instance of Namespace.

Parameters:

  • xmldoc (Nokogiri::XML::Document)

    The XML document



34
35
36
# File 'lib/vectory/svg_mapping.rb', line 34

def initialize(xmldoc)
  @namespace = xmldoc.root.namespace
end

Instance Method Details

#ns(path) ⇒ String

Converts XPath to use document namespace

Parameters:

  • path (String)

    The XPath expression

Returns:

  • (String)

    XPath with namespace prefixes



41
42
43
44
45
46
47
48
# File 'lib/vectory/svg_mapping.rb', line 41

def ns(path)
  return path if @namespace.nil?

  path.gsub(%r{/([a-zA-z])}, "/xmlns:\\1")
    .gsub(%r{::([a-zA-z])}, "::xmlns:\\1")
    .gsub(%r{\[([a-zA-z][a-z0-9A-Z@/]* ?=)}, "[xmlns:\\1")
    .gsub(%r{\[([a-zA-z][a-z0-9A-Z@/]*\])}, "[xmlns:\\1")
end