lib/lutaml/model/xml_adapter/nokogiri_adapter.rb in lutaml-model-0.3.17 vs lib/lutaml/model/xml_adapter/nokogiri_adapter.rb in lutaml-model-0.3.18
- old
+ new
@@ -13,11 +13,11 @@
end
def to_xml(options = {})
builder = Builder::Nokogiri.build(encoding: "UTF-8") do |xml|
if root.is_a?(Lutaml::Model::XmlAdapter::NokogiriElement)
- root.to_xml(xml)
+ root.build_xml(xml)
else
mapper_class = options[:mapper_class] || @root.class
options[:xml_attributes] =
build_namespace_attributes(mapper_class)
build_element(xml, @root, options)
@@ -71,15 +71,13 @@
if element_rule == xml_mapping.content_mapping
text = xml_mapping.content_mapping.serialize(element)
text = text[curr_index] if text.is_a?(Array)
- if element.mixed?
- prefixed_xml.text text
- else
- content << text
- end
+ next prefixed_xml.text(text) if element.mixed?
+
+ content << text
elsif !value.nil? || element_rule.render_nil?
value = value[curr_index] if attribute_def.collection?
add_to_xml(
xml,
@@ -87,10 +85,11 @@
element_rule.prefix,
value,
options.merge(
attribute: attribute_def,
rule: element_rule,
+ mapper_class: mapper_class,
),
)
end
end
@@ -122,13 +121,15 @@
attr.value,
namespace: attr.namespace&.href,
namespace_prefix: attr.namespace&.prefix,
)
end
+
default_namespace = node.namespace&.href if root_node.nil?
+
super(
- node.name,
+ node,
attributes,
parse_all_children(node, root_node: root_node || self, default_namespace: default_namespace),
node.text,
parent_document: root_node,
namespace_prefix: node.namespace&.prefix,
@@ -139,18 +140,24 @@
def text?
# false
children.empty? && text.length.positive?
end
- def to_xml(builder = nil)
+ def to_xml
+ return text if text?
+
+ build_xml.doc.root.to_xml
+ end
+
+ def build_xml(builder = nil)
builder ||= Builder::Nokogiri.build
if name == "text"
builder.text(text)
else
builder.public_send(name, build_attributes(self)) do |xml|
children.each do |child|
- child.to_xml(xml)
+ child.build_xml(xml)
end
end
end
builder