Class: Lutaml::Xsd::Validation::XmlElement

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/xsd/validation/xml_element.rb

Overview

XmlElement represents an XML element with validation context

This class wraps Moxml elements to provide a consistent interface for validation rules. It tracks the element’s position in the document tree and provides XPath information for error reporting.

Examples:

Create an XML element

element = XmlElement.new(moxml_element, navigator)

Access element properties

element.name           # => "person"
element.namespace_uri  # => "http://example.com"
element.qualified_name # => "{http://example.com}person"
element.xpath          # => "/root/person[1]"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(moxml_element, navigator) ⇒ XmlElement

Initialize a new XmlElement

Parameters:

  • moxml_element (Moxml::Element)

    The underlying Moxml element

  • navigator (XmlNavigator)

    The navigator tracking context



29
30
31
32
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 29

def initialize(moxml_element, navigator)
  @moxml_element = moxml_element
  @navigator = navigator
end

Instance Attribute Details

#moxml_elementObject (readonly)

Returns the value of attribute moxml_element.



23
24
25
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 23

def moxml_element
  @moxml_element
end

Returns the value of attribute navigator.



23
24
25
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 23

def navigator
  @navigator
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Compare elements for equality

Parameters:

Returns:

  • (Boolean)


217
218
219
220
221
222
223
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 217

def ==(other)
  return false unless other.is_a?(XmlElement)

  name == other.name &&
    namespace_uri == other.namespace_uri &&
    xpath == other.xpath
end

#attribute(name, namespace: nil) ⇒ XmlAttribute?

Get attribute by name

Parameters:

  • name (String)

    Attribute name

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

    Attribute namespace

Returns:



98
99
100
101
102
103
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 98

def attribute(name, namespace: nil)
  attributes.find do |attr|
    attr.name == name &&
      (namespace.nil? || attr.namespace_uri == namespace)
  end
end

#attributesArray<XmlAttribute>

Get all attributes of this element

Returns:



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 80

def attributes
  return [] unless @moxml_element.respond_to?(:attributes)

  @moxml_element.attributes.map do |attr|
    XmlAttribute.new(
      attr.name,
      attr.value,
      attr.namespace&.href,
      attr.namespace&.prefix,
    )
  end
end

#childrenArray<XmlElement>

Get all child elements

Returns:



117
118
119
120
121
122
123
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 117

def children
  return [] unless @moxml_element.respond_to?(:children)

  @moxml_element.children.select(&:element?).map do |child|
    XmlElement.new(child, @navigator)
  end
end

#children_named(name, namespace: nil) ⇒ Array<XmlElement>

Get child elements by name

Parameters:

  • name (String)

    Element name

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

    Element namespace

Returns:



130
131
132
133
134
135
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 130

def children_named(name, namespace: nil)
  children.select do |child|
    child.name == name &&
      (namespace.nil? || child.namespace_uri == namespace)
  end
end

#has_attribute?(name, namespace: nil) ⇒ Boolean

Check if element has an attribute

Parameters:

  • name (String)

    Attribute name

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

    Attribute namespace

Returns:

  • (Boolean)


110
111
112
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 110

def has_attribute?(name, namespace: nil)
  !attribute(name, namespace: namespace).nil?
end

#has_children?Boolean

Check if element has child elements

Returns:

  • (Boolean)


154
155
156
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 154

def has_children?
  children.any?
end

#has_text?Boolean

Check if element has text content

Returns:

  • (Boolean)


147
148
149
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 147

def has_text?
  !text_content.empty?
end

#hashInteger

Generate hash code

Returns:

  • (Integer)


230
231
232
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 230

def hash
  [name, namespace_uri, xpath].hash
end

#inspectString

Detailed string representation

Returns:

  • (String)


206
207
208
209
210
211
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 206

def inspect
  "#<#{self.class.name} " \
    "name=#{name.inspect} " \
    "namespace_uri=#{namespace_uri.inspect} " \
    "xpath=#{xpath.inspect}>"
end

#nameString

Get the element’s local name

Returns:

  • (String)


37
38
39
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 37

def name
  @moxml_element.name
end

#namespace_uriString?

Get the element’s namespace URI

Returns:

  • (String, nil)


44
45
46
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 44

def namespace_uri
  @moxml_element.namespace&.href
end

#namespaced?Boolean

Check if element is in a namespace

Returns:

  • (Boolean)


176
177
178
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 176

def namespaced?
  !namespace_uri.nil? && !namespace_uri.empty?
end

#prefixString?

Get the element’s namespace prefix

Returns:

  • (String, nil)


51
52
53
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 51

def prefix
  @moxml_element.namespace&.prefix
end

#prefixed_nameString

Get the prefixed name

Returns:

  • (String)

    The prefixed name (prefix:localName) or local name



69
70
71
72
73
74
75
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 69

def prefixed_name
  if prefix && !prefix.empty?
    "#{prefix}:#{name}"
  else
    name
  end
end

#qualified_nameString

Get the qualified name in Clark notation

Returns:

  • (String)

    The qualified name in namespacelocalName format



58
59
60
61
62
63
64
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 58

def qualified_name
  if namespace_uri
    "{#{namespace_uri}}#{name}"
  else
    name
  end
end

#text_contentString

Get text content of the element

Returns:

  • (String)


140
141
142
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 140

def text_content
  @moxml_element.text.to_s.strip
end

#to_hHash

Convert to hash representation

Returns:

  • (Hash)


183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 183

def to_h
  {
    name: name,
    namespace_uri: namespace_uri,
    prefix: prefix,
    qualified_name: qualified_name,
    xpath: xpath,
    attributes: attributes.map(&:to_h),
    has_children: has_children?,
    has_text: has_text?,
  }.compact
end

#to_sString

String representation

Returns:

  • (String)


199
200
201
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 199

def to_s
  "<#{prefixed_name}>"
end

#with_context { ... } ⇒ Object

Execute a block with this element in navigation context

Yields:

  • Block to execute in element context

Returns:

  • (Object)

    Result of the block



169
170
171
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 169

def with_context(&)
  @navigator.with_element(self, &)
end

#xpathString

Get the XPath of this element

Returns:

  • (String)


161
162
163
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 161

def xpath
  @navigator.current_xpath
end