Class: REXML::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/rdf/xsd/extensions.rb

Instance Method Summary collapse

Instance Method Details

#c14nxl(options = {}) ⇒ Object

Canonicalize the Element. Return a new instance of this node which is canonicalized and marked as such.

Apply namespaces either passed as an option, or that are in scope.

Parameters:

  • options (Hash{Symbol => Object}) (defaults to: {})

    From Nokogiri::XML::Node#c14nxl



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/rdf/xsd/extensions.rb', line 177

def c14nxl(options = {})
  # Add in-scope namespace definitions, unless supplied
  options[:namespaces] ||= self.namespaces
  element = options[:inplace] ? self : self.dup

  # Add in-scope namespace definitions
  options[:namespaces].each do |prefix, href|
    if prefix.to_s.empty?
      element.add_attribute("xmlns", href) unless element.attribute("xmlns")
    else
      element.add_attribute("xmlns:#{prefix}", href) unless element.attribute("xmlns:#{prefix}")
    end
  end

  # Add language
  element.add_attribute("xml:lang", options[:language].to_s) if
    options[:language] &&
    element.attribute("lang", "http://www.w3.org/XML/1998/namespace").to_s.empty? &&
    element.attribute("lang").to_s.empty?

  # Make sure it formats as open/close tags
  element.text = "" if element.text == nil && element.children.empty?
  
  # Recurse through children to ensure tags are set properly
  element.children.each {|c| c.c14nxl(:inplace => true, :namespaces => {}) if c.is_a?(REXML::Element)}
  element
end