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

.new(name = nil, doc = nil, ns = nil) ⇒ Object

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 = ns if ns
  end
end

Instance Method Details

#==(o) ⇒ Object



188
189
190
# File 'lib/niceogiri/xml/node.rb', line 188

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

#content_from(name, ns = nil) ⇒ String?

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



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

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

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

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>)


183
184
185
# File 'lib/niceogiri/xml/node.rb', line 183

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

#inherit(node) ⇒ self

Inherit the attributes and children of an XML::Node

Parameters:

Returns:

  • (self)


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

def inherit(node)
  inherit_namespaces node
  inherit_attrs node.attributes
  inherit_children node
  self
end

#inherit_attrs(attrs) ⇒ self

Inherit a set of attributes

Parameters:

  • attrs (Hash)

    a hash of attributes to set on the node

Returns:

  • (self)


153
154
155
156
157
158
159
# File 'lib/niceogiri/xml/node.rb', line 153

def inherit_attrs(attrs)
  attrs.each do |name, value|
    attr_name = value.namespace && value.namespace.prefix ? [value.namespace.prefix, name].join(':') : name
    self.write_attr attr_name, value
  end
  self
end

#inherit_children(node) ⇒ Object



161
162
163
164
165
166
167
168
169
# File 'lib/niceogiri/xml/node.rb', line 161

def inherit_children(node)
  node.children.each do |c|
    self << (n = c.dup)
    if c.respond_to?(:namespace) && c.namespace
      ns = n.add_namespace c.namespace.prefix, c.namespace.href
      n.namespace = ns
    end
  end
end

#inherit_namespaces(node) ⇒ Object



142
143
144
145
146
147
# File 'lib/niceogiri/xml/node.rb', line 142

def inherit_namespaces(node)
  node.namespace_definitions.each do |ns|
    add_namespace ns.prefix, ns.href
  end
  self.namespace = node.namespace.href if node.namespace
end

#inspectString

The node as XML

Returns:

  • (String)

    XML representation of the node



174
175
176
# File 'lib/niceogiri/xml/node.rb', line 174

def inspect
  self.to_xml
end

#namespace=(ns) ⇒ Object #namespace=(ns) ⇒ Object #namespace=(namespaces) ⇒ Object

Attach a namespace to the node

Overloads:

  • #namespace=(ns) ⇒ Object

    Attach an already created XML::Namespace

    Parameters:

    • ns (XML::Namespace)

      the namespace object

  • #namespace=(ns) ⇒ Object

    Create a new namespace and attach it

    Parameters:

    • ns (String)

      the namespace uri

  • #namespace=(namespaces) ⇒ Object

    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
77
# File 'lib/niceogiri/xml/node.rb', line 63

def namespace=(namespaces)
  case namespaces
  when Nokogiri::XML::Namespace
    self.nokogiri_namespace = namespaces
  when String
    ns = self.add_namespace nil, namespaces
    self.nokogiri_namespace = ns
  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

#namespace_hrefXML::Namespace?

Helper method to get the node's namespace

Returns:

  • (XML::Namespace, nil)

    The node's namespace object if it exists



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

def namespace_href
  namespace.href if namespace
end

#nokogiri_namespace=Object



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

alias_method :nokogiri_namespace=, :namespace=

#read_attr(attr_name, to_call = nil) ⇒ Object

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

#read_content(node, to_call = nil) ⇒ Object

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

#remove_child(name, ns = nil) ⇒ Object

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



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

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

#remove_children(name) ⇒ Object

Remove all children with a given name regardless of namespace

Parameters:

  • name (String)

    the name of the nodes to remove



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

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

#set_content_for(node, content = nil) ⇒ Object

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



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

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

#write_attr(attr_name, value, to_call = nil) ⇒ Object

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, to_call = nil)
  self[attr_name.to_sym] = value && to_call ? value.__send__(to_call) : value
end