Class: Canon::Xml::Nodes::ElementNode

Inherits:
Canon::Xml::Node show all
Defined in:
lib/canon/xml/nodes/element_node.rb

Overview

Element node in the XPath data model

Instance Attribute Summary collapse

Attributes inherited from Canon::Xml::Node

#children, #parent

Instance Method Summary collapse

Methods inherited from Canon::Xml::Node

#add_child, #in_node_set=, #in_node_set?, #parse_errors, #parse_errors=, #text_content

Constructor Details

#initialize(name:, namespace_uri: nil, prefix: nil) ⇒ ElementNode

Returns a new instance of ElementNode.



11
12
13
14
15
16
17
18
# File 'lib/canon/xml/nodes/element_node.rb', line 11

def initialize(name:, namespace_uri: nil, prefix: nil)
  super()
  @name = name
  @namespace_uri = namespace_uri
  @prefix = prefix
  @namespace_nodes = []
  @attribute_nodes = []
end

Instance Attribute Details

#attribute_nodesObject (readonly)

Returns the value of attribute attribute_nodes.



8
9
10
# File 'lib/canon/xml/nodes/element_node.rb', line 8

def attribute_nodes
  @attribute_nodes
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/canon/xml/nodes/element_node.rb', line 8

def name
  @name
end

#namespace_nodesObject (readonly)

Returns the value of attribute namespace_nodes.



8
9
10
# File 'lib/canon/xml/nodes/element_node.rb', line 8

def namespace_nodes
  @namespace_nodes
end

#namespace_uriObject (readonly)

Returns the value of attribute namespace_uri.



8
9
10
# File 'lib/canon/xml/nodes/element_node.rb', line 8

def namespace_uri
  @namespace_uri
end

#prefixObject (readonly)

Returns the value of attribute prefix.



8
9
10
# File 'lib/canon/xml/nodes/element_node.rb', line 8

def prefix
  @prefix
end

Instance Method Details

#add_attribute(attribute_node) ⇒ Object



33
34
35
36
# File 'lib/canon/xml/nodes/element_node.rb', line 33

def add_attribute(attribute_node)
  attribute_node.parent = self
  @attribute_nodes << attribute_node
end

#add_namespace(namespace_node) ⇒ Object



28
29
30
31
# File 'lib/canon/xml/nodes/element_node.rb', line 28

def add_namespace(namespace_node)
  namespace_node.parent = self
  @namespace_nodes << namespace_node
end

#node_infoObject



52
53
54
# File 'lib/canon/xml/nodes/element_node.rb', line 52

def node_info
  "name: #{name} namespace_uri: #{namespace_uri} prefix: #{prefix}"
end

#node_typeObject



20
21
22
# File 'lib/canon/xml/nodes/element_node.rb', line 20

def node_type
  :element
end

#qnameObject



24
25
26
# File 'lib/canon/xml/nodes/element_node.rb', line 24

def qname
  prefix.nil? || prefix.empty? ? name : "#{prefix}:#{name}"
end

#sorted_attribute_nodesObject

Get attribute nodes in sorted order (by namespace URI then local name)



46
47
48
49
50
# File 'lib/canon/xml/nodes/element_node.rb', line 46

def sorted_attribute_nodes
  @attribute_nodes.sort_by do |attr|
    [attr.namespace_uri.to_s, attr.local_name]
  end
end

#sorted_namespace_nodesObject

Get namespace nodes in sorted order (lexicographically by local name)



39
40
41
42
43
# File 'lib/canon/xml/nodes/element_node.rb', line 39

def sorted_namespace_nodes
  @namespace_nodes.sort_by do |ns|
    ns.local_name.to_s
  end
end

#to_sObject



56
57
58
# File 'lib/canon/xml/nodes/element_node.rb', line 56

def to_s
  "<#{qname}>"
end