class Nokogiri::XML::Document

def create_element(name, *contents_or_attrs, &block)

Other tags:
    Example: Passing a block to mutate the element -
    Example: An element with contents and attributes -
    Example: An element with attributes -
    Example: An element with contents -
    Example: An empty element without attributes -

Returns:
  • (Nokogiri::XML::Element) -

Other tags:
    Yieldparam: node -

Parameters:
  • contents_or_attrs (#to_s, Hash) --
  • name (String) --
def create_element(name, *contents_or_attrs, &block)
  elm = Nokogiri::XML::Element.new(name, self, &block)
  contents_or_attrs.each do |arg|
    case arg
    when Hash
      arg.each do |k, v|
        key = k.to_s
        if key =~ NCNAME_RE
          ns_name = Regexp.last_match(1)
          elm.add_namespace_definition(ns_name, v)
        else
          elm[k.to_s] = v.to_s
        end
      end
    else
      elm.content = arg
    end
  end
  if ns = elm.namespace_definitions.find { |n| n.prefix.nil? || (n.prefix == '') }
    elm.namespace = ns
  end
  elm
end