class Lutaml::Model::Xml::OxElement

def build_attributes(node)

def build_attributes(node)
  attrs = node.attributes.transform_values(&:value)
  node.own_namespaces.each_value do |namespace|
    attrs[namespace.attr_name] = namespace.uri
  end
  attrs
end

def build_xml(builder = nil)

def build_xml(builder = nil)
  builder ||= Builder::Ox.build
  attrs = build_attributes(self)
  if text?
    builder.add_text(builder, text)
  else
    builder.create_and_add_element(name, attributes: attrs) do |el|
      children.each { |child| child.build_xml(el) }
    end
  end
  builder
end

def cdata

def cdata
  super || cdata_children.first&.text
end

def initialize(node, root_node: nil)

def initialize(node, root_node: nil)
  case node
  when String
    super("text", {}, [], node, parent_document: root_node)
  when Ox::Comment
    super("comment", {}, [], node.value, parent_document: root_node)
  when Ox::CData
    super("#cdata-section", {}, [], node.value, parent_document: root_node)
  else
    namespace_attributes(node.attributes).each do |(name, value)|
      if root_node
        root_node.add_namespace(XmlNamespace.new(value, name))
      else
        add_namespace(XmlNamespace.new(value, name))
      end
    end
    attributes = node.attributes.each_with_object({}) do |(name, value), hash|
      next if attribute_is_namespace?(name)
      namespace_prefix = name.to_s.split(":").first
      if (n = name.to_s.split(":")).length > 1
        namespace = (root_node || self).namespaces[namespace_prefix]&.uri
        namespace ||= XML_NAMESPACE_URI
        prefix = n.first
      end
      hash[name.to_s] = XmlAttribute.new(
        name.to_s,
        value,
        namespace: namespace,
        namespace_prefix: prefix,
      )
    end
    super(
      node,
      attributes,
      parse_children(node, root_node: root_node || self),
      node.text,
      parent_document: root_node,
    )
  end
end

def inner_xml

def inner_xml
  # Ox builder by default, adds a newline at the end, so `chomp` is used
  children.map { |child| child.to_xml.chomp }.join
end

def namespace_attributes(attributes)

def namespace_attributes(attributes)
  attributes.select { |attr| attribute_is_namespace?(attr) }
end

def nodes

def nodes
  children
end

def parse_children(node, root_node: nil)

def parse_children(node, root_node: nil)
  node.nodes.map do |child|
    OxElement.new(child, root_node: root_node)
  end
end

def text

def text
  super || cdata
end

def text?

def text?
  # false
  children.empty? && text&.length&.positive?
end

def to_xml

def to_xml
  return text if text?
  build_xml.xml.to_s
end