Skip to content

doc.create_element("<element-name>") returns nil for Ox adapter #476

@suleman-uzair

Description

@suleman-uzair
Lutaml::Model::Config.xml_adapter_type = :ox # oga and nokogiri works fine

class Mathml < Lutaml::Model::Serializable
  attribute :mrow, :string
  attribute :mstyle, :string

  xml do
    map_element :mrow, to: :mrow
    map_element :mstyle, with: { from: :mstyle_from_xml, to: :mstyle_to_xml }
  end

  def mstyle_from_xml(model, value)
    return if value.nil? || value&.empty?

    model.mstyle = "input value: #{value.first.text}"
  end

  def mstyle_to_xml(model, parent, doc)
    mstyle_element = doc.create_element("mstyle")
    doc.add_text(mstyle_element, "customized value: #{model.mstyle.sub('input value: ', '')}")
    doc.add_element(parent, mstyle_element)
  end
end

print Mathml.from_xml("<math><mrow>a</mrow><mstyle>Style</mstyle></math>").to_xml
/Users/apple/ruby_gems/lutaml-model/lib/lutaml/model/xml/builder/ox.rb:84:in 'Lutaml::Model::Xml::Builder::Ox#add_text': undefined method 'text' for nil (NoMethodError)

            element.text(text)
                   ^^^^^
        from bin/console:48:in 'Mathml#mstyle_to_xml'
        from /Users/apple/ruby_gems/lutaml-model/lib/lutaml/model/xml/document.rb:152:in 'Lutaml::Model::Xml::Document#add_to_xml'
        from /Users/apple/ruby_gems/lutaml-model/lib/lutaml/model/xml/document.rb:248:in 'block (2 levels) in Lutaml::Model::Xml::Document#build_unordered_element'
        from /Users/apple/ruby_gems/lutaml-model/lib/lutaml/model/xml/document.rb:236:in 'Array#each'
        from /Users/apple/ruby_gems/lutaml-model/lib/lutaml/model/xml/document.rb:236:in 'block in Lutaml::Model::Xml::Document#build_unordered_element'
        from /Users/apple/ruby_gems/lutaml-model/lib/lutaml/model/xml/builder/ox.rb:52:in 'block in Lutaml::Model::Xml::Builder::Ox#create_and_add_element'
        from /Users/apple/ruby_gems/lutaml-model/lib/lutaml/model/xml/builder/ox.rb:51:in 'Ox::Builder#element'
        from /Users/apple/ruby_gems/lutaml-model/lib/lutaml/model/xml/builder/ox.rb:51:in 'Lutaml::Model::Xml::Builder::Ox#create_and_add_element'
        from /Users/apple/ruby_gems/lutaml-model/lib/lutaml/model/xml/document.rb:224:in 'Lutaml::Model::Xml::Document#build_unordered_element'
        from /Users/apple/ruby_gems/lutaml-model/lib/lutaml/model/xml/document.rb:143:in 'Lutaml::Model::Xml::Document#build_element'
        from /Users/apple/ruby_gems/lutaml-model/lib/lutaml/model/xml/ox_adapter.rb:38:in 'Lutaml::Model::Xml::OxAdapter#to_xml'
        from /Users/apple/ruby_gems/lutaml-model/lib/lutaml/model/serialize.rb:398:in 'Kernel#public_send'
        from /Users/apple/ruby_gems/lutaml-model/lib/lutaml/model/serialize.rb:398:in 'Lutaml::Model::Serialize::ClassMethods#to'
        from /Users/apple/ruby_gems/lutaml-model/lib/lutaml/model/serialize.rb:730:in 'Lutaml::Model::Serialize#to_format'
        from /Users/apple/ruby_gems/lutaml-model/lib/lutaml/model/serialize.rb:627:in 'block in Lutaml::Model::Serialize#register_to_format_method'
        from (irb):3:in '<main>'
        ... 2 levels...

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions