Class: Lutaml::Xml::DataModel::XmlElement

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/xml/data_model.rb

Overview

Represents an XML element with namespace, attributes, and children.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, namespace_class = nil, explicit_namespace: false) ⇒ XmlElement

Initialize a new XML element

Parameters:

  • name (String)

    Element local name

  • namespace_class (Class, nil) (defaults to: nil)

    XmlNamespace class

  • explicit_namespace (Boolean) (defaults to: false)

    Whether namespace was explicitly set on this element (true = from rule’s namespace; false = inherited from parent or no namespace)



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/lutaml/xml/data_model.rb', line 65

def initialize(name, namespace_class = nil, explicit_namespace: false)
  @name = name
  @namespace_class = namespace_class
  @explicit_namespace = explicit_namespace
  @attributes = []
  @children = []
  @text_content = nil
  @cdata = false
  @form = nil
  @processing_instructions = []
end

Instance Attribute Details

#attributesArray<XmlAttribute> (readonly)

Returns Element attributes.

Returns:



20
21
22
# File 'lib/lutaml/xml/data_model.rb', line 20

def attributes
  @attributes
end

#cdataBoolean

Returns Whether text content should be wrapped in CDATA section.

Returns:

  • (Boolean)

    Whether text content should be wrapped in CDATA section



29
30
31
# File 'lib/lutaml/xml/data_model.rb', line 29

def cdata
  @cdata
end

#childrenArray<XmlElement, String> (readonly)

Returns Child elements and text nodes.

Returns:

  • (Array<XmlElement, String>)

    Child elements and text nodes



23
24
25
# File 'lib/lutaml/xml/data_model.rb', line 23

def children
  @children
end

#explicit_namespaceBoolean (readonly)

Whether this element’s namespace was explicitly declared (from mapping rule) vs inherited from parent or unset.

Returns:

  • (Boolean)


81
82
83
# File 'lib/lutaml/xml/data_model.rb', line 81

def explicit_namespace
  @explicit_namespace
end

#formSymbol?

Returns Form option (:qualified, :unqualified, or nil) :qualified forces prefix format, :unqualified forces default format.

Returns:

  • (Symbol, nil)

    Form option (:qualified, :unqualified, or nil) :qualified forces prefix format, :unqualified forces default format



33
34
35
# File 'lib/lutaml/xml/data_model.rb', line 33

def form
  @form
end

#nameString

Returns Element local name.

Returns:

  • (String)

    Element local name



14
15
16
# File 'lib/lutaml/xml/data_model.rb', line 14

def name
  @name
end

#namespace_classClass?

Returns XmlNamespace class (not instance).

Returns:

  • (Class, nil)

    XmlNamespace class (not instance)



17
18
19
# File 'lib/lutaml/xml/data_model.rb', line 17

def namespace_class
  @namespace_class
end

#namespace_scope_configArray?

Returns Namespace scope configuration (for hoisting).

Returns:

  • (Array, nil)

    Namespace scope configuration (for hoisting)



51
52
53
# File 'lib/lutaml/xml/data_model.rb', line 51

def namespace_scope_config
  @namespace_scope_config
end

#needs_xmlns_blankBoolean

Returns Whether element needs xmlns=“” declaration.

Returns:

  • (Boolean)

    Whether element needs xmlns=“” declaration



36
37
38
# File 'lib/lutaml/xml/data_model.rb', line 36

def needs_xmlns_blank
  @needs_xmlns_blank
end

#original_namespace_uriString?

Returns Original namespace URI (for alias support).

Returns:

  • (String, nil)

    Original namespace URI (for alias support)



48
49
50
# File 'lib/lutaml/xml/data_model.rb', line 48

def original_namespace_uri
  @original_namespace_uri
end

#original_xml_elementLutaml::Xml::XmlElement?

Returns Original parsed XmlElement (for prefix fallback).

Returns:



54
55
56
# File 'lib/lutaml/xml/data_model.rb', line 54

def original_xml_element
  @original_xml_element
end

#processing_instructionsArray<XmlProcessingInstruction> (readonly)

Returns Processing instructions before this element.

Returns:



57
58
59
# File 'lib/lutaml/xml/data_model.rb', line 57

def processing_instructions
  @processing_instructions
end

#raw_contentString?

Returns Raw XML content for map_all custom methods.

Returns:

  • (String, nil)

    Raw XML content for map_all custom methods



42
43
44
# File 'lib/lutaml/xml/data_model.rb', line 42

def raw_content
  @raw_content
end

#text_contentString?

Returns Direct text content (for simple elements).

Returns:

  • (String, nil)

    Direct text content (for simple elements)



26
27
28
# File 'lib/lutaml/xml/data_model.rb', line 26

def text_content
  @text_content
end

#xml_namespace_prefixString?

Returns XML namespace prefix (for doubly-defined namespace support).

Returns:

  • (String, nil)

    XML namespace prefix (for doubly-defined namespace support)



45
46
47
# File 'lib/lutaml/xml/data_model.rb', line 45

def xml_namespace_prefix
  @xml_namespace_prefix
end

#xsi_nilBoolean

Returns Whether element was created from nil value with render_nil.

Returns:

  • (Boolean)

    Whether element was created from nil value with render_nil



39
40
41
# File 'lib/lutaml/xml/data_model.rb', line 39

def xsi_nil
  @xsi_nil
end

Instance Method Details

#add_attribute(attribute) ⇒ self

Add an attribute to this element

Parameters:

Returns:

  • (self)


96
97
98
99
# File 'lib/lutaml/xml/data_model.rb', line 96

def add_attribute(attribute)
  @attributes << attribute
  self
end

#add_child(child) ⇒ self

Add a child element or text node

Parameters:

Returns:

  • (self)


87
88
89
90
# File 'lib/lutaml/xml/data_model.rb', line 87

def add_child(child)
  @children << child
  self
end

#add_processing_instruction(target, content) ⇒ Object



101
102
103
104
105
# File 'lib/lutaml/xml/data_model.rb', line 101

def add_processing_instruction(target, content)
  @processing_instructions << XmlProcessingInstruction.new(target,
                                                           content)
  self
end

#has_attributes?Boolean

Check if element has any attributes

Returns:

  • (Boolean)


117
118
119
# File 'lib/lutaml/xml/data_model.rb', line 117

def has_attributes?
  !@attributes.empty?
end

#has_children?Boolean

Check if element has any children

Returns:

  • (Boolean)


110
111
112
# File 'lib/lutaml/xml/data_model.rb', line 110

def has_children?
  !@children.empty?
end

#inspectString

Detailed inspection for debugging

Returns:

  • (String)


163
164
165
# File 'lib/lutaml/xml/data_model.rb', line 163

def inspect
  "#<#{self.class.name} #{self}>"
end

#qualified_name(prefix = nil) ⇒ String

Get qualified name (prefix:name or name)

Parameters:

  • prefix (String, nil) (defaults to: nil)

    Optional prefix override

Returns:

  • (String)


125
126
127
128
129
130
131
132
133
134
# File 'lib/lutaml/xml/data_model.rb', line 125

def qualified_name(prefix = nil)
  if prefix
    "#{prefix}:#{name}"
  elsif namespace_class.respond_to?(:prefix_default)
    ns_prefix = namespace_class.prefix_default
    ns_prefix ? "#{ns_prefix}:#{name}" : name
  else
    name
  end
end

#to_sString

String representation for debugging

Returns:

  • (String)


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/lutaml/xml/data_model.rb', line 139

def to_s
  parts = ["<#{qualified_name}"]

  if namespace_class
    parts << " (ns: #{namespace_class})"
  end

  if has_attributes?
    parts << " attrs: #{attributes.length}"
  end

  if text_content
    parts << " text: #{text_content.inspect}"
  elsif has_children?
    parts << " children: #{children.length}"
  end

  parts << ">"
  parts.join
end