class Lutaml::Model::XmlAdapter::NokogiriAdapter

def build_ordered_element(xml, element, options = {})

def build_ordered_element(xml, element, options = {})
  mapper_class = options[:mapper_class] || element.class
  xml_mapping = mapper_class.mappings_for(:xml)
  return xml unless xml_mapping
  attributes = build_attributes(element, xml_mapping)&.compact
  prefixed_xml = if options.key?(:namespace_prefix)
                   options[:namespace_prefix] ? xml[options[:namespace_prefix]] : xml
                 elsif xml_mapping.namespace_prefix
                   xml[xml_mapping.namespace_prefix]
                 else
                   xml
                 end
  tag_name = options[:tag_name] || xml_mapping.root_element
  prefixed_xml.public_send(tag_name, attributes) do
    if options.key?(:namespace_prefix) && !options[:namespace_prefix]
      xml.parent.namespace = nil
    end
    index_hash = {}
    element.element_order.each do |name|
      index_hash[name] ||= -1
      curr_index = index_hash[name] += 1
      element_rule = xml_mapping.find_by_name(name)
      next if element_rule.nil?
      attribute_def = attribute_definition_for(element, element_rule, mapper_class: mapper_class)
      value = attribute_value_for(element, element_rule)
      nsp_xml = element_rule.prefix ? xml[element_rule.prefix] : xml
      if element_rule == xml_mapping.content_mapping
        text = element.send(xml_mapping.content_mapping.to)
        text = text[curr_index] if text.is_a?(Array)
        prefixed_xml.text text
      elsif attribute_def.collection?
        add_to_xml(nsp_xml, value[curr_index], attribute_def,
                   element_rule)
      elsif !value.nil? || element_rule.render_nil?
        add_to_xml(nsp_xml, value, attribute_def, element_rule)
      end
    end
  end
end