module PIM::XmlUtils

Module including ‘helper’ methods to create XML nodes This way, it may be ‘easy’ to switch from Oga to any other library

Public Instance Methods

add_xml_attribute(node, name, value = nil) click to toggle source
# File pim.rb, line 2123
def add_xml_attribute node, name, value = nil
  attribute = Oga::XML::Attribute.new(name: name.to_s)
  attribute.value = value.to_s unless value.nil?
  node.add_attribute(attribute)
  return attribute
end
add_xml_attributes(node, attributes = {}) click to toggle source
# File pim.rb, line 2114
def add_xml_attributes node, attributes = {}
  unless attributes.nil? or attributes.empty?
    attributes.each_pair do |name, value|
      add_xml_attribute(node, name, value)
    end
  end
  return node.attributes
end
add_xml_element(parent_node, name, *args, &block) click to toggle source
# File pim.rb, line 2090
def add_xml_element parent_node, name, *args, &block

  element = Oga::XML::Element.new(name: name.to_s)
  block.call(element) unless block.nil?

  unless args.empty?

    if PIM.is_hash?(args[0])
      attributes = args[0]
      text = args[1] if args.length > 1
    else
      attributes = nil
      text = args[0]
    end
    add_xml_attributes(element, attributes)
    set_xml_text(element, text)

  end

  parent_node.children << element

  return element
end
create_xml_document() click to toggle source
# File pim.rb, line 2081
def create_xml_document
  return PIM::Utils.timed(group: "validation",
                          key: "create_xml_document",
                          message: "Creating Oga::XML::Document") do
    require 'oga'
    Oga::XML::Document.new
  end
end
get_or_create_xml_element(parent_node, name, &block) click to toggle source
# File pim.rb, line 2141
def get_or_create_xml_element parent_node, name, &block
  return parent_node if name.nil?
  element = parent_node.children.find { |node| node.name == name }
  element = add_xml_element(parent_node, name, &block) if element.nil?
  element
end
get_or_create_xml_elements(parent_node, *names, &block) click to toggle source
# File pim.rb, line 2130
def get_or_create_xml_elements parent_node, *names, &block
  current_element = parent_node
  unless names.empty?
    names.each do |name|
      current_element = get_or_create_xml_element(current_element, name)
    end
  end
  block.call(current_element) unless block.nil?
  current_element
end
set_xml_text(node, text) click to toggle source
# File pim.rb, line 2148
def set_xml_text node, text
  return if text.nil?
  node.inner_text = text.to_s
end
xpath(node, xpath) click to toggle source
# File pim.rb, line 2153
def xpath node, xpath
  node.xpath(xpath)
end