Method: Libis::Tools::XmlDocument.add_namespaces

Defined in:
lib/libis/tools/xml_document.rb

.add_namespaces(node, namespaces) ⇒ Object

Add namespace information to a node

Example:

xml_doc.add_namespaces xml_doc.root, jkr: 'http://JKRowling.com', node_ns: 'jkr'
xml_doc.to_xml
# =>
    <?xml version="1.0" encoding="utf-8"?>
    <jkr:patron xmlns:jkr="http://JKRowling.com">
        ...
    </jkr:patron>

xml_doc.add_namespaces xml_doc.root, nil => 'http://JKRowling.com'
xml_doc.to_xml
# =>
    <?xml version="1.0" encoding="utf-8"?>
    <patron xmlns="http://JKRowling.com">
        ...
    </patron>

Parameters:

  • node (Nokogiri::XML::Node)

    the node where the namespace info should be added to

  • namespaces (Hash)

    a Hash with prefix - URI pairs for each namespace definition that should be added. The special key :node_ns is reserved for specifying the prefix for the node itself. To set the default namespace, use the prefix nil



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/libis/tools/xml_document.rb', line 379

def self.add_namespaces(node, namespaces)

  node_ns = namespaces.delete :node_ns
  default_ns = namespaces.delete nil

  namespaces.each do |prefix, prefix_uri|
    node.add_namespace prefix.to_s, prefix_uri
  end

  node.namespace_scopes.each do |ns|
    node.namespace = ns if ns.prefix == node_ns.to_s
  end if node_ns

  node.default_namespace = default_ns if default_ns

  node

end