Class: Lutaml::Xsd::Validation::XmlAttribute

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

Overview

XmlAttribute represents an XML attribute with validation context

This class wraps XML attributes to provide a consistent interface for validation rules. It includes the attribute name, value, and namespace information needed for XSD validation.

Examples:

Create an XML attribute

attr = XmlAttribute.new("id", "12345", "http://example.com")

Access attribute properties

attr.name          # => "id"
attr.value         # => "12345"
attr.namespace_uri # => "http://example.com"
attr.qualified_name # => "{http://example.com}id"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value, namespace_uri = nil, prefix = nil) ⇒ XmlAttribute

Initialize a new XmlAttribute

Parameters:

  • name (String)

    The attribute name (local name without prefix)

  • value (String)

    The attribute value

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

    The namespace URI

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

    The namespace prefix



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

def initialize(name, value, namespace_uri = nil, prefix = nil)
  @name = name
  @value = value
  @namespace_uri = namespace_uri
  @prefix = prefix
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



21
22
23
# File 'lib/lutaml/xsd/validation/xml_attribute.rb', line 21

def name
  @name
end

#namespace_uriObject (readonly)

Returns the value of attribute namespace_uri.



21
22
23
# File 'lib/lutaml/xsd/validation/xml_attribute.rb', line 21

def namespace_uri
  @namespace_uri
end

#prefixObject (readonly)

Returns the value of attribute prefix.



21
22
23
# File 'lib/lutaml/xsd/validation/xml_attribute.rb', line 21

def prefix
  @prefix
end

#valueObject (readonly)

Returns the value of attribute value.



21
22
23
# File 'lib/lutaml/xsd/validation/xml_attribute.rb', line 21

def value
  @value
end

Instance Method Details

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

Compare attributes for equality

Parameters:

Returns:

  • (Boolean)


99
100
101
102
103
104
105
# File 'lib/lutaml/xsd/validation/xml_attribute.rb', line 99

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

  @name == other.name &&
    @value == other.value &&
    @namespace_uri == other.namespace_uri
end

#hashInteger

Generate hash code

Returns:

  • (Integer)


112
113
114
# File 'lib/lutaml/xsd/validation/xml_attribute.rb', line 112

def hash
  [@name, @value, @namespace_uri].hash
end

#inspectString

Detailed string representation

Returns:

  • (String)


88
89
90
91
92
93
# File 'lib/lutaml/xsd/validation/xml_attribute.rb', line 88

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

#namespaced?Boolean

Check if attribute is in a namespace

Returns:

  • (Boolean)


61
62
63
# File 'lib/lutaml/xsd/validation/xml_attribute.rb', line 61

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

#prefixed_nameString

Get the prefixed name

Returns:

  • (String)

    The prefixed name (prefix:localName) or local name



50
51
52
53
54
55
56
# File 'lib/lutaml/xsd/validation/xml_attribute.rb', line 50

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



39
40
41
42
43
44
45
# File 'lib/lutaml/xsd/validation/xml_attribute.rb', line 39

def qualified_name
  if @namespace_uri && !@namespace_uri.empty?
    "{#{@namespace_uri}}#{@name}"
  else
    @name
  end
end

#to_hHash

Convert to hash representation

Returns:

  • (Hash)


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

def to_h
  {
    name: @name,
    value: @value,
    namespace_uri: @namespace_uri,
    prefix: @prefix,
    qualified_name: qualified_name,
  }.compact
end

#to_sString

String representation

Returns:

  • (String)


81
82
83
# File 'lib/lutaml/xsd/validation/xml_attribute.rb', line 81

def to_s
  "#{prefixed_name}=\"#{@value}\""
end