Class: RXSD::XML::LibXMLNode

Inherits:
Node
  • Object
show all
Defined in:
lib/rxsd/libxml_adapter.rb

Overview

Some additions to libxml xml node interface

Instance Attribute Summary

Attributes inherited from Node

#related

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#child_obj, #child_value, #child_values, #children_objs, factory, #root

Constructor Details

#initialize(args = {}) ⇒ LibXMLNode

Create libxml node adapter w/ specified args, which may include

  • :node LibXML::Node to use to satify requests



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rxsd/libxml_adapter.rb', line 47

def initialize(args = {})
   @node = args[:node]
   @parent = args[:parent] if args.has_key? :parent

   @attributes = @node.attributes.to_h

   @children = []
   @node.children.find_all { |n| !n.text? }.each { |n|
     @children << LibXMLNode.new(:node => n, :parent => self)
   }
   
   @namespaces = []
   @node.namespaces.each { |ns| 
      @namespaces << LibXMLNamespace.new(:ns => ns)
   }
end

Class Method Details

.xml_root(xml) ⇒ Object

Implementation of RXSD::XML::Node::xml_root(xml)



41
42
43
# File 'lib/rxsd/libxml_adapter.rb', line 41

def self.xml_root(xml)
    LibXMLNode.new :node => LibXML::XML::Document.string(xml).root
end

Instance Method Details

#attrsObject

Implementation of RXSD::XML::Node.attrs



71
72
73
# File 'lib/rxsd/libxml_adapter.rb', line 71

def attrs
   @attributes
end

#childrenObject

Implementation of RXSD::XML::Node.children



88
89
90
# File 'lib/rxsd/libxml_adapter.rb', line 88

def children
   @children
end

#contentObject

Implementation of RXSD::XML::Node.content See text? method as well



100
101
102
103
104
# File 'lib/rxsd/libxml_adapter.rb', line 100

def content
   return nil unless text?
   @node.content if @node.text?
   @node.children[0].content
end

#nameObject

Implementation of RXSD::XML::Node.name



66
67
68
# File 'lib/rxsd/libxml_adapter.rb', line 66

def name 
   @node.name
end

#namespacesObject

Implementation of RXSD::XML::Node.namespaces



107
108
109
# File 'lib/rxsd/libxml_adapter.rb', line 107

def namespaces
   @namespaces
end

#parentObject

Implementation of RXSD::XML::Node.parent



83
84
85
# File 'lib/rxsd/libxml_adapter.rb', line 83

def parent
   parent? ? @parent : nil
end

#parent?Boolean

Implementation of RXSD::XML::Node.parent?

Returns:



76
77
78
# File 'lib/rxsd/libxml_adapter.rb', line 76

def parent?
   @node.parent? && @node.parent.class != LibXML::XML::Document
end

#text?Boolean

Implementation of RXSD::XML::Node.text?. See #content method as well

Returns:



94
95
96
# File 'lib/rxsd/libxml_adapter.rb', line 94

def text?
   @node.text? || (@node.children.size == 1 && @node.children[0].text?)
end