Class: Lutaml::Xsd::Validation::XmlElement
- Inherits:
-
Object
- Object
- Lutaml::Xsd::Validation::XmlElement
- 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.
Instance Attribute Summary collapse
-
#moxml_element ⇒ Object
readonly
Returns the value of attribute moxml_element.
-
#navigator ⇒ Object
readonly
Returns the value of attribute navigator.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Compare elements for equality.
-
#attribute(name, namespace: nil) ⇒ XmlAttribute?
Get attribute by name.
-
#attributes ⇒ Array<XmlAttribute>
Get all attributes of this element.
-
#children ⇒ Array<XmlElement>
Get all child elements.
-
#children_named(name, namespace: nil) ⇒ Array<XmlElement>
Get child elements by name.
-
#has_attribute?(name, namespace: nil) ⇒ Boolean
Check if element has an attribute.
-
#has_children? ⇒ Boolean
Check if element has child elements.
-
#has_text? ⇒ Boolean
Check if element has text content.
-
#hash ⇒ Integer
Generate hash code.
-
#initialize(moxml_element, navigator) ⇒ XmlElement
constructor
Initialize a new XmlElement.
-
#inspect ⇒ String
Detailed string representation.
-
#name ⇒ String
Get the element’s local name.
-
#namespace_uri ⇒ String?
Get the element’s namespace URI.
-
#namespaced? ⇒ Boolean
Check if element is in a namespace.
-
#prefix ⇒ String?
Get the element’s namespace prefix.
-
#prefixed_name ⇒ String
Get the prefixed name.
-
#qualified_name ⇒ String
Get the qualified name in Clark notation.
-
#text_content ⇒ String
Get text content of the element.
-
#to_h ⇒ Hash
Convert to hash representation.
-
#to_s ⇒ String
String representation.
-
#with_context { ... } ⇒ Object
Execute a block with this element in navigation context.
-
#xpath ⇒ String
Get the XPath of this element.
Constructor Details
#initialize(moxml_element, navigator) ⇒ XmlElement
Initialize a new XmlElement
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_element ⇒ Object (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 |
#navigator ⇒ Object (readonly)
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
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
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 |
#attributes ⇒ Array<XmlAttribute>
Get all attributes of this element
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 |
#children ⇒ Array<XmlElement>
Get all child elements
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
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
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
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
147 148 149 |
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 147 def has_text? !text_content.empty? end |
#hash ⇒ Integer
Generate hash code
230 231 232 |
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 230 def hash [name, namespace_uri, xpath].hash end |
#inspect ⇒ String
Detailed string representation
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 |
#name ⇒ String
Get the element’s local name
37 38 39 |
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 37 def name @moxml_element.name end |
#namespace_uri ⇒ String?
Get the element’s namespace URI
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
176 177 178 |
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 176 def namespaced? !namespace_uri.nil? && !namespace_uri.empty? end |
#prefix ⇒ String?
Get the element’s namespace prefix
51 52 53 |
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 51 def prefix @moxml_element.namespace&.prefix end |
#prefixed_name ⇒ String
Get the prefixed 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_name ⇒ String
Get the qualified name in Clark notation
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_content ⇒ String
Get text content of the element
140 141 142 |
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 140 def text_content @moxml_element.text.to_s.strip end |
#to_h ⇒ Hash
Convert to hash representation
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_s ⇒ String
String representation
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
169 170 171 |
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 169 def with_context(&) @navigator.with_element(self, &) end |
#xpath ⇒ String
Get the XPath of this element
161 162 163 |
# File 'lib/lutaml/xsd/validation/xml_element.rb', line 161 def xpath @navigator.current_xpath end |