Class: Niceogiri::XML::Node

Inherits:
Nokogiri::XML::Node show all
Defined in:
lib/niceogiri/xml/node.rb

Overview

Base XML Node All XML classes subclass Node - it allows the addition of helpers

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Nokogiri::XML::Node

#[]=, #attr_set, #find_first, #nokogiri_xpath, #xpath

Class Method Details

+ (Object) new(name = nil, doc = nil, ns = nil)

Create a new Node object

not provided one will be created

Parameters:

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

    the element name

  • doc (XML::Document, nil) (defaults to: nil)

    the document to attach the node to. If

Returns:

  • a new object with the name and namespace



13
14
15
16
17
18
# File 'lib/niceogiri/xml/node.rb', line 13

def self.new(name = nil, doc = nil, ns = nil)
  super(name.to_s, (doc || Nokogiri::XML::Document.new)).tap do |node|
    node.document.root = node unless doc
    node.namespace = self.ns if ns
  end
end

Instance Method Details

- (Object) ==(o)



171
172
173
# File 'lib/niceogiri/xml/node.rb', line 171

def ==(o)
  eql?(o)
end

- (String?) content_from(name, ns = nil)

The content of the named node

Parameters:

  • name (String)

    the name or xpath of the node

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

    the namespace the node is in

Returns:

  • (String, nil)

    the content of the node



106
107
108
109
# File 'lib/niceogiri/xml/node.rb', line 106

def content_from(name, ns = nil)
  child = xpath(name, ns).first
  child.content if child
end

- (Fixnum<-1,0,1>) eql?(o, *fields)

Check that a set of fields are equal between nodes

Parameters:

  • other (Node)

    the other node to compare against

  • fields (*#to_s)

    the set of fields to compare

Returns:

  • (Fixnum<-1,0,1>)


166
167
168
# File 'lib/niceogiri/xml/node.rb', line 166

def eql?(o, *fields)
  o.is_a?(self.class) && fields.all? { |f| self.__send__(f) == o.__send__(f) }
end

- (self) inherit(node)

Inherit the attributes and children of an XML::Node

Parameters:

Returns:

  • (self)


134
135
136
137
138
139
140
141
142
143
# File 'lib/niceogiri/xml/node.rb', line 134

def inherit(node)
  set_namespace node.namespace if node.namespace
  inherit_attrs node.attributes
  node.children.each do |c|
    self << (n = c.dup)
    ns = n.namespace_definitions.find { |ns| ns.prefix == c.namespace.prefix }
    n.namespace = ns if ns
  end
  self
end

- (self) inherit_attrs(attrs)

Inherit a set of attributes

Parameters:

  • attrs (Hash)

    a hash of attributes to set on the node

Returns:

  • (self)


149
150
151
152
# File 'lib/niceogiri/xml/node.rb', line 149

def inherit_attrs(attrs)
  attrs.each { |name, value| self[name] = value }
  self
end

- (String) inspect

The node as XML

Returns:

  • (String)

    XML representation of the node



157
158
159
# File 'lib/niceogiri/xml/node.rb', line 157

def inspect
  self.to_xml
end

- (Object) namespace=(ns) - (Object) namespace=(ns) - (Object) namespace=(namespaces)

Attach a namespace to the node

Overloads:

  • - (Object) namespace=(ns)

    Attach an already created XML::Namespace

    Parameters:

    • ns (XML::Namespace)

      the namespace object

  • - (Object) namespace=(ns)

    Create a new namespace and attach it

    Parameters:

    • ns (String)

      the namespace uri

  • - (Object) namespace=(namespaces)

    Createa and add new namespaces from a hash

    Parameters:

    • namespaces (Hash)

      a hash of prefix => uri pairs



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/niceogiri/xml/node.rb', line 63

def namespace=(namespaces)
  case namespaces
  when Nokogiri::XML::Namespace
    self.nokogiri_namespace = namespaces
  when String
    self.add_namespace nil, namespaces
  when Hash
    self.add_namespace nil, ns if ns = namespaces.delete(nil)
    namespaces.each do |p, n|
      ns = self.add_namespace p, n
      self.nokogiri_namespace = ns
    end
  end
end

- (XML::Namespace?) namespace_href

Helper method to get the node's namespace

Returns:

  • (XML::Namespace, nil)

    The node's namespace object if it exists



81
82
83
# File 'lib/niceogiri/xml/node.rb', line 81

def namespace_href
  namespace.href if namespace
end

- (Object) nokogiri_namespace=



51
# File 'lib/niceogiri/xml/node.rb', line 51

alias_method :nokogiri_namespace=, :namespace=

- (Object) read_attr(attr_name, to_call = nil)

Helper method to read an attribute

the returned value

Parameters:

  • attr_name (#to_sym)

    the name of the attribute

  • to_call (String, Symbol, nil) (defaults to: nil)

    the name of the method to call on

Returns:

  • nil or the value



26
27
28
29
# File 'lib/niceogiri/xml/node.rb', line 26

def read_attr(attr_name, to_call = nil)
  val = self[attr_name.to_sym]
  val && to_call ? val.__send__(to_call) : val
end

- (Object) read_content(node, to_call = nil)

Helper method to read the content of a node

the returned value

Parameters:

  • node (#to_sym)

    the name of the node

  • to_call (String, Symbol, nil) (defaults to: nil)

    the name of the method to call on

Returns:

  • nil or the value



45
46
47
48
# File 'lib/niceogiri/xml/node.rb', line 45

def read_content(node, to_call = nil)
  val = content_from node.to_sym
  val && to_call ? val.__send__(to_call) : val
end

- (Object) remove_child(name, ns = nil)

Remove a child with the name and (optionally) namespace given

Parameters:

  • name (String)

    the name or xpath of the node to remove

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

    the namespace the node is in



89
90
91
92
# File 'lib/niceogiri/xml/node.rb', line 89

def remove_child(name, ns = nil)
  child = xpath(name, ns).first
  child.remove if child
end

- (Object) remove_children(name)

Remove all children with a given name regardless of namespace

Parameters:

  • name (String)

    the name of the nodes to remove



97
98
99
# File 'lib/niceogiri/xml/node.rb', line 97

def remove_children(name)
  xpath("./*[local-name()='#{name}']").remove
end

- (Object) set_content_for(node, content = nil)

Sets the content for the specified node. If the node exists it is updated. If not a new node is created If the node exists and the content is nil, the node will be removed entirely

Parameters:

  • node (String)

    the name of the node to update/create

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

    the content to set within the node



118
119
120
121
122
123
124
125
126
# File 'lib/niceogiri/xml/node.rb', line 118

def set_content_for(node, content = nil)
  if content
    child = xpath(node).first
    self << (child = Node.new(node, self.document)) unless child
    child.content = content
  else
    remove_child node
  end
end

- (Object) write_attr(attr_name, value)

Helper method to write a value to an attribute

Parameters:

  • attr_name (#to_sym)

    the name of the attribute

  • value (#to_s)

    the value to set the attribute to



35
36
37
# File 'lib/niceogiri/xml/node.rb', line 35

def write_attr(attr_name, value)
  self[attr_name.to_sym] = value
end