Class: Niceogiri::XML::Node
- Inherits:
-
Nokogiri::XML::Node
- Object
- Nokogiri::XML::Node
- Niceogiri::XML::Node
- 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)
-
+ (Object) new(name = nil, doc = nil, ns = nil)
Create a new Node object.
Instance Method Summary (collapse)
- - (Object) ==(o)
-
- (String?) content_from(name, ns = nil)
The content of the named node.
-
- (Fixnum<-1,0,1>) eql?(o, *fields)
Check that a set of fields are equal between nodes.
-
- (self) inherit(node)
Inherit the attributes and children of an XML::Node.
-
- (self) inherit_attrs(attrs)
Inherit a set of attributes.
-
- (String) inspect
The node as XML.
-
- (Object) namespace=(namespaces)
Attach a namespace to the node.
-
- (XML::Namespace?) namespace_href
Helper method to get the node's namespace.
- - (Object) nokogiri_namespace=
-
- (Object) read_attr(attr_name, to_call = nil)
Helper method to read an attribute.
-
- (Object) read_content(node, to_call = nil)
Helper method to read the content of a node.
-
- (Object) remove_child(name, ns = nil)
Remove a child with the name and (optionally) namespace given.
-
- (Object) remove_children(name)
Remove all children with a given name regardless of namespace.
-
- (Object) set_content_for(node, content = nil)
Sets the content for the specified node.
-
- (Object) write_attr(attr_name, value)
Helper method to write a value to an attribute.
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
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
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
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
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
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
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
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
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
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
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
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
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
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
35 36 37 |
# File 'lib/niceogiri/xml/node.rb', line 35 def write_attr(attr_name, value) self[attr_name.to_sym] = value end |