Class: Nokogiri::XML::Node

Inherits:
Object
  • Object
show all
Includes:
Enumerable, PP::Node
Defined in:
lib/nokogiri/xml/node.rb,
lib/nokogiri/ffi/xml/node.rb,
lib/nokogiri/xml/node/save_options.rb,
ext/nokogiri/xml_dtd.c,
ext/nokogiri/xml_attr.c,
ext/nokogiri/xml_node.c,
ext/nokogiri/xml_text.c,
ext/nokogiri/xml_cdata.c,
ext/nokogiri/xml_comment.c,
ext/nokogiri/xml_document.c,
ext/nokogiri/html_document.c,
ext/nokogiri/xml_entity_decl.c,
ext/nokogiri/xml_element_decl.c,
ext/nokogiri/xml_attribute_decl.c,
ext/nokogiri/xml_entity_reference.c,
ext/nokogiri/xml_document_fragment.c,
ext/nokogiri/xml_processing_instruction.c

Overview

Nokogiri::XML::Node is your window to the fun filled world of dealing with XML and HTML tags. A Nokogiri::XML::Node may be treated similarly to a hash with regard to attributes. For example (from irb):

irb(main):004:0> node
=> <a href="#foo" id="link">link</a>
irb(main):005:0> node['href']
=> "#foo"
irb(main):006:0> node.keys
=> ["href", "id"]
irb(main):007:0> node.values
=> ["#foo", "link"]
irb(main):008:0> node['class'] = 'green'
=> "green"
irb(main):009:0> node
=> <a href="#foo" id="link" class="green">link</a>
irb(main):010:0>

See Nokogiri::XML::Node#[] and Nokogiri::XML#[]= for more information.

Nokogiri::XML::Node also has methods that let you move around your tree. For navigating your tree, see:

  • Nokogiri::XML::Node#parent

  • Nokogiri::XML::Node#children

  • Nokogiri::XML::Node#next

  • Nokogiri::XML::Node#previous

You may search this node’s subtree using Node#xpath and Node#css

Defined Under Namespace

Classes: SaveOptions

Constant Summary collapse

ELEMENT_NODE =

Element node type, see Nokogiri::XML::Node#element?

1
ATTRIBUTE_NODE =

Attribute node type

2
TEXT_NODE =

Text node type, see Nokogiri::XML::Node#text?

3
CDATA_SECTION_NODE =

CDATA node type, see Nokogiri::XML::Node#cdata?

4
ENTITY_REF_NODE =

Entity reference node type

5
ENTITY_NODE =

Entity node type

6
PI_NODE =

PI node type

7
COMMENT_NODE =

Comment node type, see Nokogiri::XML::Node#comment?

8
DOCUMENT_NODE =

Document node type, see Nokogiri::XML::Node#xml?

9
DOCUMENT_TYPE_NODE =

Document type node type

10
DOCUMENT_FRAG_NODE =

Document fragment node type

11
NOTATION_NODE =

Notation node type

12
HTML_DOCUMENT_NODE =

HTML document node type, see Nokogiri::XML::Node#html?

13
DTD_NODE =

DTD node type

14
ELEMENT_DECL =

Element declaration type

15
ATTRIBUTE_DECL =

Attribute declaration type

16
ENTITY_DECL =

Entity declaration type

17
NAMESPACE_DECL =

Namespace declaration type

18
XINCLUDE_START =

XInclude start type

19
XINCLUDE_END =

XInclude end type

20
DOCB_DOCUMENT_NODE =

DOCB document node type

21

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PP::Node

#inspect, #pretty_print

Constructor Details

#initialize(name, document) ⇒ Node

:nodoc:



83
84
85
# File 'lib/nokogiri/xml/node.rb', line 83

def initialize name, document # :nodoc:
  # ... Ya.  This is empty on purpose.
end

Instance Attribute Details

#cstructObject

:stopdoc:



6
7
8
# File 'lib/nokogiri/ffi/xml/node.rb', line 6

def cstruct
  @cstruct
end

Class Method Details

.new(name, document) ⇒ Object

Create a new node with name sharing GC lifecycle with document



1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
# File 'ext/nokogiri/xml_node.c', line 1104

def self.new(name, doc, *rest)
  ptr = LibXML.xmlNewNode(nil, name.to_s)

  node_cstruct = LibXML::XmlNode.new(ptr)
  node_cstruct[:doc] = doc.cstruct[:doc]
  node_cstruct.keep_reference_from_document!

  node = Node.wrap(
    node_cstruct,
    Node == self ? nil : self
  )
  node.send :initialize, name, doc, *rest
  yield node if block_given?
  node
end

.node_properties(cstruct) ⇒ Object



436
437
438
439
440
441
442
443
444
445
# File 'lib/nokogiri/ffi/xml/node.rb', line 436

def node_properties(cstruct)
  attr = []
  prop_cstruct = cstruct[:properties]
  while ! prop_cstruct.null?
    prop = Node.wrap(prop_cstruct)
    attr << prop
    prop_cstruct = prop.cstruct[:next]
  end
  attr
end

.wrap(node_struct, klass = nil) ⇒ Object



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/nokogiri/ffi/xml/node.rb', line 350

def self.wrap(node_struct, klass=nil)
  if node_struct.is_a?(FFI::Pointer)
    # cast native pointers up into a node cstruct
    return nil if node_struct.null?
    node_struct = LibXML::XmlNode.new(node_struct)
  end

  raise "wrapping a node without a document" unless node_struct.document

  document_struct = node_struct.document
  document_obj = document_struct.nil? ? nil : document_struct.ruby_doc
  if node_struct[:type] == DOCUMENT_NODE || node_struct[:type] == HTML_DOCUMENT_NODE
    return document_obj
  end

  ruby_node = node_struct.ruby_node
  return ruby_node unless ruby_node.nil?

  klasses = case node_struct[:type]
            when ELEMENT_NODE then [XML::Element]
            when TEXT_NODE then [XML::Text]
            when ENTITY_REF_NODE then [XML::EntityReference]
            when ATTRIBUTE_DECL then [XML::AttributeDecl, LibXML::XmlAttribute]
            when ELEMENT_DECL then [XML::ElementDecl, LibXML::XmlElement]
            when COMMENT_NODE then [XML::Comment]
            when DOCUMENT_FRAG_NODE then [XML::DocumentFragment]
            when PI_NODE then [XML::ProcessingInstruction]
            when ATTRIBUTE_NODE then [XML::Attr]
            when ENTITY_DECL then [XML::EntityDecl, LibXML::XmlEntity]
            when CDATA_SECTION_NODE then [XML::CDATA]
            when DTD_NODE then [XML::DTD, LibXML::XmlDtd]
            else [XML::Node]
            end

  if klass
    node = klass.allocate
  else
    node = klasses.first.allocate
  end
  node.cstruct = klasses[1] ? klasses[1].new(node_struct.pointer) : node_struct

  node.cstruct.ruby_node = node

  if document_obj
    node.instance_variable_set(:@document, document_obj)
    cache = document_obj.instance_variable_get(:@node_cache)
    cache << node
    document_obj.decorate(node)
  end

  node
end

Instance Method Details

#<=>(other) ⇒ Object

Compare two Node objects with respect to their Document. Nodes from different documents cannot be compared.



856
857
858
859
860
# File 'lib/nokogiri/xml/node.rb', line 856

def <=> other
  return nil unless other.is_a?(Nokogiri::XML::Node)
  return nil unless document == other.document
  compare other
end

#==(other) ⇒ Object

Test to see if this Node is equal to other



695
696
697
698
699
# File 'lib/nokogiri/xml/node.rb', line 695

def == other
  return false unless other
  return false unless other.respond_to?(:pointer_id)
  pointer_id == other.pointer_id
end

#>(selector) ⇒ Object

Search this node’s immediate children using CSS selector selector



218
219
220
221
# File 'lib/nokogiri/xml/node.rb', line 218

def > selector
  ns = document.root.namespaces
  xpath CSS.xpath_for(selector, :prefix => "./", :ns => ns).first
end

#[](name) ⇒ Object Also known as: get_attribute, attr

Get the attribute value for the attribute name



252
253
254
255
# File 'lib/nokogiri/xml/node.rb', line 252

def [] name
  return nil unless key?(name.to_s)
  get(name.to_s)
end

#[]=(property, value) ⇒ Object

Set the property to value



668
669
670
671
# File 'ext/nokogiri/xml_node.c', line 668

def []=(property, value)
  LibXML.xmlSetProp(cstruct, property, value)
  value
end

#accept(visitor) ⇒ Object

Accept a visitor. This method calls “visit” on visitor with self.



689
690
691
# File 'lib/nokogiri/xml/node.rb', line 689

def accept visitor
  visitor.visit(self)
end

#add_child(node_or_tags) ⇒ Object Also known as: <<

Add node_or_tags as a child of this Node. node_or_tags can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.

Returns the reparented node (if node_or_tags is a Node), or NodeSet (if node_or_tags is a DocumentFragment, NodeSet, or string).



262
263
264
265
266
267
268
269
270
# File 'lib/nokogiri/xml/node.rb', line 262

def add_child node_or_tags
  node_or_tags = coerce(node_or_tags)
  if node_or_tags.is_a?(XML::NodeSet)
    node_or_tags.each { |n| add_child_node n }
  else
    add_child_node node_or_tags
  end
  node_or_tags
end

#add_namespaceObject



659
# File 'lib/nokogiri/xml/node.rb', line 659

alias :add_namespace :add_namespace_definition

#add_namespace_definition(prefix, href) ⇒ Object

Adds a namespace definition with prefix using href value. The result is as if parsed XML for this node had included an attribute ‘xmlns:prefix=value’. A default namespace for this node (“xmlns=”) can be added by passing ‘nil’ for prefix. Namespaces added this way will not show up in #attributes, but they will be included as an xmlns attribute when the node is serialized to XML.



1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
# File 'ext/nokogiri/xml_node.c', line 1066

def add_namespace_definition(prefix, href)
  ns = LibXML.xmlSearchNs(cstruct.document, cstruct, prefix.nil? ? nil : prefix.to_s)
  namespacee = self
  if ns.null?
    namespacee = parent if type != ELEMENT_NODE
    ns = LibXML.xmlNewNs(namespacee.cstruct, href, prefix)
  end
  return nil if ns.null?
  LibXML.xmlSetNs(cstruct, ns) if (prefix.nil? || self != namespacee)
  Namespace.wrap(cstruct.document, ns)
end

#add_next_sibling(node_or_tags) ⇒ Object Also known as: next=

Insert node_or_tags after this Node (as a sibling). node_or_tags can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.

Returns the reparented node (if node_or_tags is a Node), or NodeSet (if node_or_tags is a DocumentFragment, NodeSet, or string).

Also see related method after.



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/nokogiri/xml/node.rb', line 303

def add_next_sibling node_or_tags
  node_or_tags = coerce(node_or_tags)
  if node_or_tags.is_a?(XML::NodeSet)
    if text?
      pivot = Nokogiri::XML::Node.new 'dummy', document
      add_next_sibling_node pivot
    else
      pivot = self
    end
    node_or_tags.reverse.each { |n| pivot.send :add_next_sibling_node, n }
    pivot.unlink if text?
  else
    add_next_sibling_node node_or_tags
  end
  node_or_tags
end

#add_previous_sibling(node_or_tags) ⇒ Object Also known as: previous=

Insert node_or_tags before this Node (as a sibling). node_or_tags can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.

Returns the reparented node (if node_or_tags is a Node), or NodeSet (if node_or_tags is a DocumentFragment, NodeSet, or string).

Also see related method before.



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/nokogiri/xml/node.rb', line 279

def add_previous_sibling node_or_tags
  node_or_tags = coerce(node_or_tags)
  if node_or_tags.is_a?(XML::NodeSet)
    if text?
      pivot = Nokogiri::XML::Node.new 'dummy', document
      add_previous_sibling_node pivot
    else
      pivot = self
    end
    node_or_tags.each { |n| pivot.send :add_previous_sibling_node, n }
    pivot.unlink if text?
  else
    add_previous_sibling_node node_or_tags
  end
  node_or_tags
end

#after(node_or_tags) ⇒ Object

Insert node_or_tags after this node (as a sibling). node_or_tags can be a Nokogiri::XML::Node, a Nokogiri::XML::DocumentFragment, or a string containing markup.

Returns self, to support chaining of calls.

Also see related method add_next_sibling.



339
340
341
342
# File 'lib/nokogiri/xml/node.rb', line 339

def after node_or_tags
  add_next_sibling node_or_tags
  self
end

#ancestors(selector = nil) ⇒ Object

Get a list of ancestor Node for this Node. If selector is given, the ancestors must match selector



630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
# File 'lib/nokogiri/xml/node.rb', line 630

def ancestors selector = nil
  return NodeSet.new(document) unless respond_to?(:parent)
  return NodeSet.new(document) unless parent

  parents = [parent]

  while parents.last.respond_to?(:parent)
    break unless ctx_parent = parents.last.parent
    parents << ctx_parent
  end

  return NodeSet.new(document, parents) unless selector

  root = parents.last

  NodeSet.new(document, parents.find_all { |parent|
    root.search(selector).include?(parent)
  })
end

#at(path, ns = document.root ? document.root.namespaces : {}) ⇒ Object Also known as: %

Search for the first occurrence of path.

Returns nil if nothing is found, otherwise a Node.



227
228
229
# File 'lib/nokogiri/xml/node.rb', line 227

def at path, ns = document.root ? document.root.namespaces : {}
  search(path, ns).first
end

#at_css(*rules) ⇒ Object

Search this node for the first occurrence of CSS rules. Equivalent to css(rules).first See Node#css for more information.



246
247
248
# File 'lib/nokogiri/xml/node.rb', line 246

def at_css *rules
  css(*rules).first
end

#at_xpath(*paths) ⇒ Object

Search this node for the first occurrence of XPath paths. Equivalent to xpath(paths).first See Node#xpath for more information.



237
238
239
# File 'lib/nokogiri/xml/node.rb', line 237

def at_xpath *paths
  xpath(*paths).first
end

#attribute(name) ⇒ Object

Get the attribute node with name



731
732
733
# File 'ext/nokogiri/xml_node.c', line 731

def attribute(name)
  attribute_nodes.find { |x| x.name == name }
end

#attribute_nodesObject

returns a list containing the Node attributes.



766
767
768
# File 'ext/nokogiri/xml_node.c', line 766

def attribute_nodes
  Node.node_properties cstruct
end

#attribute_with_ns(name, namespace) ⇒ Object

Get the attribute node with name and namespace



748
749
750
751
752
753
# File 'ext/nokogiri/xml_node.c', line 748

def attribute_with_ns(name, namespace)
  prop = LibXML.xmlHasNsProp(cstruct, name.to_s,
    namespace.nil? ? NULL : namespace.to_s)
  return prop if prop.null?
  Node.wrap(prop)
end

#attributesObject

Returns a hash containing the node’s attributes. The key is the attribute name without any namespace, the value is a Nokogiri::XML::Attr representing the attribute. If you need to distinguish attributes with the same name, with different namespaces use #attribute_nodes instead.



441
442
443
444
445
# File 'lib/nokogiri/xml/node.rb', line 441

def attributes
  Hash[*(attribute_nodes.map { |node|
    [node.node_name, node]
  }.flatten)]
end

#before(node_or_tags) ⇒ Object

Insert node_or_tags before this node (as a sibling). node_or_tags can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.

Returns self, to support chaining of calls.

Also see related method add_previous_sibling.



327
328
329
330
# File 'lib/nokogiri/xml/node.rb', line 327

def before node_or_tags
  add_previous_sibling node_or_tags
  self
end

#blank?Boolean

Is this node blank?

Returns:

  • (Boolean)


412
413
414
# File 'ext/nokogiri/xml_node.c', line 412

def blank?
  LibXML.xmlIsBlankNode(cstruct) == 1
end

#cdata?Boolean

Returns true if this is a CDATA

Returns:

  • (Boolean)


563
564
565
# File 'lib/nokogiri/xml/node.rb', line 563

def cdata?
  type == CDATA_SECTION_NODE
end

#childObject

Returns the child node



578
579
580
# File 'ext/nokogiri/xml_node.c', line 578

def child
  (val = cstruct[:children]).null? ? nil : Node.wrap(val)
end

#childrenObject

Get the list of children for this node as a NodeSet



505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# File 'ext/nokogiri/xml_node.c', line 505

def children
  return NodeSet.new(nil) if cstruct[:children].null?
  child = Node.wrap(cstruct[:children])

  set = NodeSet.wrap(LibXML.xmlXPathNodeSetCreate(child.cstruct), self.document)
  return set unless child

  child_ptr = child.cstruct[:next]
  while ! child_ptr.null?
    child = Node.wrap(child_ptr)
    LibXML.xmlXPathNodeSetAddUnique(set.cstruct, child.cstruct)
    child_ptr = child.cstruct[:next]
  end

  return set
end

#children=(node_or_tags) ⇒ Object

Set the inner html for this Node node_or_tags node_or_tags can be a Nokogiri::XML::Node, a Nokogiri::XML::DocumentFragment, or a string containing markup.

Returns the reparented node (if node_or_tags is a Node), or NodeSet (if node_or_tags is a DocumentFragment, NodeSet, or string).

Also see related method inner_html=



363
364
365
366
367
368
369
370
371
372
# File 'lib/nokogiri/xml/node.rb', line 363

def children= node_or_tags
  node_or_tags = coerce(node_or_tags)
  children.unlink
  if node_or_tags.is_a?(XML::NodeSet)
    node_or_tags.each { |n| add_child_node n }
  else
    add_child node_or_tags
  end
  node_or_tags
end

#cloneObject



432
# File 'lib/nokogiri/xml/node.rb', line 432

alias :clone          :dup

#comment?Boolean

Returns true if this is a Comment

Returns:

  • (Boolean)


558
559
560
# File 'lib/nokogiri/xml/node.rb', line 558

def comment?
  type == COMMENT_NODE
end

#contentObject

Returns the content for this Node



901
902
903
904
905
906
907
# File 'ext/nokogiri/xml_node.c', line 901

def content
  content_ptr = LibXML.xmlNodeGetContent(cstruct)
  return nil if content_ptr.null?
  content = content_ptr.read_string # TODO: encoding?
  LibXML.xmlFree(content_ptr)
  content
end

#content=(string) ⇒ Object

Set the Node’s content to a Text node containing string. The string gets XML escaped, not interpreted as markup.



519
520
521
# File 'lib/nokogiri/xml/node.rb', line 519

def content= string
  self.native_content = encode_special_chars(string.to_s)
end

#create_external_subset(name, external_id, system_id) ⇒ Object

Create an external subset



293
294
295
296
297
298
299
300
301
302
# File 'ext/nokogiri/xml_node.c', line 293

def create_external_subset name, external_id, system_id
  raise("Document already has an external subset") if external_subset

  doc = cstruct.document
  dtd_ptr = LibXML.xmlNewDtd doc, name, external_id, system_id

  return nil if dtd_ptr.null?

  Node.wrap dtd_ptr
end

#create_internal_subset(name, external_id, system_id) ⇒ Object

Create the internal subset of a document.

doc.create_internal_subset("chapter", "-//OASIS//DTD DocBook XML//EN", "chapter.dtd")
# => <!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML//EN" "chapter.dtd">

doc.create_internal_subset("chapter", nil, "chapter.dtd")
# => <!DOCTYPE chapter SYSTEM "chapter.dtd">


262
263
264
265
266
267
268
269
270
271
# File 'ext/nokogiri/xml_node.c', line 262

def create_internal_subset name, external_id, system_id
  raise("Document already has an internal subset") if internal_subset

  doc = cstruct.document
  dtd_ptr = LibXML.xmlCreateIntSubset doc, name, external_id, system_id

  return nil if dtd_ptr.null?

  Node.wrap dtd_ptr
end

#css(*rules) ⇒ Object

call-seq: css *rules, [namespace-bindings, custom-pseudo-class]

Search this node for CSS rules. rules must be one or more CSS selectors. For example:

node.css('title')
node.css('body h1.bold')
node.css('div + p.green', 'div#one')

A hash of namespace bindings may be appended. For example:

node.css('bike|tire', {'bike' => 'http://schwinn.com/'})

Custom CSS pseudo classes may also be defined. To define custom pseudo classes, create a class and implement the custom pseudo class you want defined. The first argument to the method will be the current matching NodeSet. Any other arguments are ones that you pass in. For example:

node.css('title:regex("\w+")', Class.new {
  def regex node_set, regex
    node_set.find_all { |node| node['some_attribute'] =~ /#{regex}/ }
  end
}.new)

Note that the CSS query string is case-sensitive with regards to your document type. That is, if you’re looking for “H1” in an HTML document, you’ll never find anything, since HTML tags will match only lowercase CSS queries. However, “H1” might be found in an XML document, where tags names are case-sensitive (e.g., “H1” is distinct from “h1”).



204
205
206
207
208
209
210
211
212
213
214
# File 'lib/nokogiri/xml/node.rb', line 204

def css *rules
  rules, handler, ns, binds = extract_params(rules)

  prefix = "#{implied_xpath_context}/"

  rules = rules.map { |rule|
    CSS.xpath_for(rule, :prefix => prefix, :ns => ns)
  }.flatten.uniq + [ns, handler, binds].compact

  xpath(*rules)
end

#css_pathObject

Get the path to this node as a CSS expression



621
622
623
624
625
# File 'lib/nokogiri/xml/node.rb', line 621

def css_path
  path.split(/\//).map { |part|
    part.length == 0 ? nil : part.gsub(/\[(\d+)\]/, ':nth-of-type(\1)')
  }.compact.join(' > ')
end

#decorate!Object

Decorate this node with the decorators set up in this node’s Document



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

def decorate!
  document.decorate(self)
end

#default_namespace=(url) ⇒ Object

Adds a default namespace supplied as a string url href, to self. The consequence is as an xmlns attribute with supplied argument were present in parsed XML. A default namespace set with this method will now show up in #attributes, but when this node is serialized to XML an “xmlns” attribute will appear. See also #namespace and #namespace=



656
657
658
# File 'lib/nokogiri/xml/node.rb', line 656

def default_namespace= url
  add_namespace_definition(nil, url)
end

#descriptionObject

Fetch the Nokogiri::HTML::ElementDescription for this node. Returns nil on XML documents and on unknown tags.



590
591
592
593
# File 'lib/nokogiri/xml/node.rb', line 590

def description
  return nil if document.xml?
  Nokogiri::HTML::ElementDescription[name]
end

#documentObject

Get the document for this Node



205
206
207
# File 'ext/nokogiri/xml_node.c', line 205

def document
  cstruct.document.ruby_doc
end

#dupObject

Copy this node. An optional depth may be passed in, but it defaults to a deep copy. 0 is a shallow copy, 1 is a deep copy.



373
374
375
376
377
# File 'ext/nokogiri/xml_node.c', line 373

def dup(deep = 1)
  dup_ptr = LibXML.xmlDocCopyNode(cstruct, cstruct.document, deep)
  return nil if dup_ptr.null?
  Node.wrap(dup_ptr, self.class)
end

#each(&block) ⇒ Object

Iterate over each attribute name and value pair for this Node.



461
462
463
464
465
# File 'lib/nokogiri/xml/node.rb', line 461

def each &block
  attribute_nodes.each { |node|
    block.call([node.node_name, node.value])
  }
end

#element?Boolean Also known as: elem?

Returns true if this is an Element node

Returns:

  • (Boolean)


603
604
605
# File 'lib/nokogiri/xml/node.rb', line 603

def element?
  type == ELEMENT_NODE
end

#element_childrenObject

Get the list of children for this node as a NodeSet. All nodes will be element nodes.

Example:

@doc.root.element_children.all? { |x| x.element? } # => true


544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# File 'ext/nokogiri/xml_node.c', line 544

def element_children
  child = LibXML.xmlFirstElementChildHack(self)
  return NodeSet.new(nil) if child.nil?

  set = NodeSet.wrap(LibXML.xmlXPathNodeSetCreate(child.cstruct), self.document)
  return set unless child

  next_sibling = LibXML.xmlNextElementSiblingHack(child)
  while ! next_sibling.nil?
    child = next_sibling
    LibXML.xmlXPathNodeSetAddUnique(set.cstruct, child.cstruct)
    next_sibling = LibXML.xmlNextElementSiblingHack(child)
  end

  return set
end

#elementsObject



433
# File 'lib/nokogiri/xml/node.rb', line 433

alias :elements       :element_children

#encode_special_chars(string) ⇒ Object

Encode any special characters in string



232
233
234
235
236
237
238
# File 'ext/nokogiri/xml_node.c', line 232

def encode_special_chars(string)
  char_ptr = LibXML.xmlEncodeSpecialChars(self[:doc], string)
  encoded = char_ptr.read_string
  # TODO: encoding?
  LibXML.xmlFree(char_ptr)
  encoded
end

#external_subsetObject

Get the external subset



324
325
326
327
328
329
# File 'ext/nokogiri/xml_node.c', line 324

def external_subset
  doc = cstruct.document
  return nil if doc[:extSubset].null?

  Node.wrap(doc[:extSubset])
end

#first_element_childObject

Returns the first child node of this node that is an element.

Example:

@doc.root.first_element_child.element? # => true


599
600
601
# File 'ext/nokogiri/xml_node.c', line 599

def first_element_child
  LibXML.xmlFirstElementChildHack(self)
end

#fragment(tags) ⇒ Object

Create a DocumentFragment containing tags that is relative to this context node.



483
484
485
486
# File 'lib/nokogiri/xml/node.rb', line 483

def fragment tags
  type = document.html? ? Nokogiri::HTML : Nokogiri::XML
  type::DocumentFragment.new(document, tags, self)
end

#fragment?Boolean

Returns true if this is a DocumentFragment

Returns:

  • (Boolean)


583
584
585
# File 'lib/nokogiri/xml/node.rb', line 583

def fragment?
  type == DOCUMENT_FRAG_NODE
end

#has_attribute?Object



426
# File 'lib/nokogiri/xml/node.rb', line 426

alias :has_attribute? :key?

#html?Boolean

Returns true if this is an HTML::Document node

Returns:

  • (Boolean)


573
574
575
# File 'lib/nokogiri/xml/node.rb', line 573

def html?
  type == HTML_DOCUMENT_NODE
end

#inner_html(*args) ⇒ Object

Get the inner_html for this node’s Node#children



616
617
618
# File 'lib/nokogiri/xml/node.rb', line 616

def inner_html *args
  children.map { |x| x.to_html(*args) }.join
end

#inner_html=(node_or_tags) ⇒ Object

Set the inner html for this Node to node_or_tags node_or_tags can be a Nokogiri::XML::Node, a Nokogiri::XML::DocumentFragment, or a string containing markup.

Returns self.

Also see related method children=



351
352
353
354
# File 'lib/nokogiri/xml/node.rb', line 351

def inner_html= node_or_tags
  self.children = node_or_tags
  self
end

#inner_textObject



425
# File 'lib/nokogiri/xml/node.rb', line 425

alias :inner_text     :content

#internal_subsetObject

Get the internal subset



348
349
350
351
352
353
# File 'ext/nokogiri/xml_node.c', line 348

def internal_subset
  doc = cstruct.document
  dtd = LibXML.xmlGetIntSubset(doc)
  return nil if dtd.null?
  Node.wrap(dtd)
end

#key?(attribute) ⇒ Boolean

Returns true if attribute is set

Returns:

  • (Boolean)


637
638
639
# File 'ext/nokogiri/xml_node.c', line 637

def key?(attribute)
  ! (prop = LibXML.xmlHasProp(cstruct, attribute.to_s)).null?
end

#keysObject

Get the attribute names for this Node.



455
456
457
# File 'lib/nokogiri/xml/node.rb', line 455

def keys
  attribute_nodes.map { |node| node.node_name }
end

#last_element_childObject

Returns the last child node of this node that is an element.

Example:

@doc.root.last_element_child.element? # => true


620
621
622
# File 'ext/nokogiri/xml_node.c', line 620

def last_element_child
  LibXML.xmlLastElementChildHack(self)
end

#lineObject

Returns the line for this Node



1047
1048
1049
# File 'ext/nokogiri/xml_node.c', line 1047

def line
  cstruct[:line]
end

#matches?(selector) ⇒ Boolean

Returns true if this Node matches selector

Returns:

  • (Boolean)


476
477
478
# File 'lib/nokogiri/xml/node.rb', line 476

def matches? selector
  ancestors.last.search(selector).include?(self)
end

#nameObject



428
# File 'lib/nokogiri/xml/node.rb', line 428

alias :name           :node_name

#name=Object



429
# File 'lib/nokogiri/xml/node.rb', line 429

alias :name=          :node_name=

#namespaceObject

returns the default namespace set on this node (as with an “xmlns=” attribute), as a Namespace object.



788
789
790
# File 'ext/nokogiri/xml_node.c', line 788

def namespace
  cstruct[:ns].null? ? nil : Namespace.wrap(cstruct.document, cstruct[:ns])
end

#namespace=(ns) ⇒ Object

Set the default namespace on this node (as would be defined with an “xmlns=” attribute in XML source), as a Namespace object ns. Note that a Namespace added this way will NOT be serialized as an xmlns attribute for this node. You probably want #default_namespace= instead, or perhaps #add_namespace_definition with a nil prefix argument.



667
668
669
670
671
672
673
674
675
676
677
678
# File 'lib/nokogiri/xml/node.rb', line 667

def namespace= ns
  return set_namespace(ns) unless ns

  unless Nokogiri::XML::Namespace === ns
    raise TypeError, "#{ns.class} can't be coerced into Nokogiri::XML::Namespace"
  end
  if ns.document != document
    raise ArgumentError, 'namespace must be declared on the same document'
  end

  set_namespace ns
end

#namespace_definitionsObject

returns namespaces defined on self element directly, as an array of Namespace objects. Includes both a default namespace (as in“xmlns=”), and prefixed namespaces (as in “xmlns:prefix=”).



805
806
807
808
809
810
811
812
813
814
815
# File 'ext/nokogiri/xml_node.c', line 805

def namespace_definitions
  list = []
  ns_ptr = cstruct[:nsDef]
  return list if ns_ptr.null?
  while ! ns_ptr.null?
    ns = Namespace.wrap(cstruct.document, ns_ptr)
    list << ns
    ns_ptr = ns.cstruct[:next]
  end
  list
end

#namespace_scopesObject

returns namespaces in scope for self – those defined on self element directly or any ancestor node – as an array of Namespace objects. Default namespaces (“xmlns=” style) for self are included in this array; Default namespaces for ancestors, however, are not. See also #namespaces



837
838
839
840
841
842
843
844
845
846
847
848
# File 'ext/nokogiri/xml_node.c', line 837

def namespace_scopes
  ns_list = LibXML.xmlGetNsList(self.cstruct[:doc], self.cstruct)
  return [] if ns_list.null?

  list = []
  until (ns_ptr = ns_list.get_pointer(LibXML.pointer_offset(list.length))).null?
    list << Namespace.wrap(cstruct.document, ns_ptr)
  end

  LibXML.xmlFree(ns_list)
  list
end

#namespaced_key?(attribute, namespace) ⇒ Boolean

Returns true if attribute is set with namespace

Returns:

  • (Boolean)


652
653
654
655
656
# File 'ext/nokogiri/xml_node.c', line 652

def namespaced_key?(attribute, namespace)
  prop = LibXML.xmlHasNsProp(cstruct, attribute.to_s,
    namespace.nil? ? nil : namespace.to_s)
  !prop.null?
end

#namespacesObject

Returns a Hash of => value for all namespaces on this node and its ancestors.

This method returns the same namespaces as #namespace_scopes.

Returns namespaces in scope for self – those defined on self element directly or any ancestor node – as a Hash of attribute-name/value pairs. Note that the keys in this hash XML attributes that would be used to define this namespace, such as “xmlns:prefix”, not just the prefix. Default namespace set on self will be included with key “xmlns”. However, default namespaces set on ancestor will NOT be, even if self has no explicit default namespace.



544
545
546
547
548
549
550
551
552
553
554
555
# File 'lib/nokogiri/xml/node.rb', line 544

def namespaces
  Hash[*namespace_scopes.map { |nd|
    key = ['xmlns', nd.prefix].compact.join(':')
    if RUBY_VERSION >= '1.9' && document.encoding
      begin
        key.force_encoding document.encoding
      rescue ArgumentError
      end
    end
    [key, nd.href]
  }.flatten]
end

#nextObject



411
# File 'lib/nokogiri/xml/node.rb', line 411

alias :next           :next_sibling

#next_elementObject

Returns the next Nokogiri::XML::Element type sibling node.



459
460
461
# File 'ext/nokogiri/xml_node.c', line 459

def next_element
  LibXML.xmlNextElementSiblingHack self
end

#next_siblingObject

Returns the next sibling node



425
426
427
# File 'ext/nokogiri/xml_node.c', line 425

def next_sibling
  cstruct_node_from :next
end

#nameObject

Returns the name for this Node



960
961
962
# File 'ext/nokogiri/xml_node.c', line 960

def node_name
  cstruct[:name] # TODO: encoding?
end

#name=(new_name) ⇒ Object

Set the name for this Node



946
947
948
949
# File 'ext/nokogiri/xml_node.c', line 946

def node_name=(string)
  LibXML.xmlNodeSetName(cstruct, string)
  string
end

#node_typeObject

Get the type for this Node



865
866
867
# File 'ext/nokogiri/xml_node.c', line 865

def node_type
  cstruct[:type]
end

#parentObject

Get the parent Node for this Node



929
930
931
# File 'ext/nokogiri/xml_node.c', line 929

def parent
  cstruct_node_from :parent
end

#parent=(parent_node) ⇒ Object

Set the parent Node for this Node



525
526
527
528
# File 'lib/nokogiri/xml/node.rb', line 525

def parent= parent_node
  parent_node.add_child(self)
  parent_node
end

#parse(string_or_io, options = nil) {|options| ... } ⇒ Object

Parse string_or_io as a document fragment within the context of this node. Returns a XML::NodeSet containing the nodes parsed from string_or_io.

Yields:

  • (options)


492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# File 'lib/nokogiri/xml/node.rb', line 492

def parse string_or_io, options = nil
  options ||= (document.html? ? ParseOptions::DEFAULT_HTML : ParseOptions::DEFAULT_XML)
  if Fixnum === options
    options = Nokogiri::XML::ParseOptions.new(options)
  end
  # Give the options to the user
  yield options if block_given?

  contents = string_or_io.respond_to?(:read) ?
    string_or_io.read :
    string_or_io

  return Nokogiri::XML::NodeSet.new(document) if contents.empty?

  ##
  # This is a horrible hack, but I don't care. See #313 for background.
  error_count = document.errors.length
  node_set = in_context(contents, options.to_i)
  if node_set.empty? and document.errors.length > error_count and options.recover?
    fragment = Nokogiri::HTML::DocumentFragment.parse contents
    node_set = fragment.children
  end
  node_set
end

#pathObject

Returns the path associated with this Node



975
976
977
978
979
980
# File 'ext/nokogiri/xml_node.c', line 975

def path
  path_ptr = LibXML.xmlGetNodePath(cstruct)
  val = path_ptr.null? ? nil : path_ptr.read_string # TODO: encoding?
  LibXML.xmlFree(path_ptr)
  val
end

#pointer_idObject

Get the internal pointer number



218
219
220
# File 'ext/nokogiri/xml_node.c', line 218

def pointer_id
  cstruct.pointer
end

#previousObject



412
# File 'lib/nokogiri/xml/node.rb', line 412

alias :previous       :previous_sibling

#previous_elementObject

Returns the previous Nokogiri::XML::Element type sibling node.



476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'ext/nokogiri/xml_node.c', line 476

def previous_element
  #
  #  note that we don't use xmlPreviousElementSibling here because it's buggy pre-2.7.7.
  #
  sibling_ptr = cstruct[:prev]

  while ! sibling_ptr.null?
    sibling_cstruct = LibXML::XmlNode.new(sibling_ptr)
    break if sibling_cstruct[:type] == ELEMENT_NODE
    sibling_ptr = sibling_cstruct[:prev]
  end

  return sibling_ptr.null? ? nil : Node.wrap(sibling_ptr)
end

#previous_siblingObject

Returns the previous sibling node



442
443
444
# File 'ext/nokogiri/xml_node.c', line 442

def previous_sibling
  cstruct_node_from :prev
end

#read_only?Boolean

Is this a read only node?

Returns:

  • (Boolean)


597
598
599
600
# File 'lib/nokogiri/xml/node.rb', line 597

def read_only?
  # According to gdome2, these are read-only node types
  [NOTATION_NODE, ENTITY_NODE, ENTITY_DECL].include?(type)
end

#removeObject



420
# File 'lib/nokogiri/xml/node.rb', line 420

alias :remove         :unlink

#remove_attribute(name) ⇒ Object Also known as: delete

Remove the attribute named name



469
470
471
# File 'lib/nokogiri/xml/node.rb', line 469

def remove_attribute name
  attributes[name].remove if key? name
end

#replace(node_or_tags) ⇒ Object

Replace this Node with node_or_tags. node_or_tags can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.

Returns the reparented node (if node_or_tags is a Node), or NodeSet (if node_or_tags is a DocumentFragment, NodeSet, or string).

Also see related method swap.



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/nokogiri/xml/node.rb', line 381

def replace node_or_tags
  node_or_tags = coerce(node_or_tags)
  if node_or_tags.is_a?(XML::NodeSet)
    if text?
      replacee = Nokogiri::XML::Node.new 'dummy', document
      add_previous_sibling_node replacee
      unlink
    else
      replacee = self
    end
    node_or_tags.each { |n| replacee.add_previous_sibling n }
    replacee.unlink
  else
    replace_node node_or_tags
  end
  node_or_tags
end

#search(*paths) ⇒ Object Also known as: /

Search this node for paths. paths can be XPath or CSS, and an optional hash of namespaces may be appended. See Node#xpath and Node#css.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/nokogiri/xml/node.rb', line 97

def search *paths
  # TODO use         paths, handler, ns, binds = extract_params(paths)
  ns = paths.last.is_a?(Hash) ? paths.pop :
    (document.root ? document.root.namespaces : {})

  prefix = "#{implied_xpath_context}/"

  xpath(*(paths.map { |path|
    path = path.to_s
    path =~ /^(\.\/|\/)/ ? path : CSS.xpath_for(
      path,
      :prefix => prefix,
      :ns     => ns
    )
  }.flatten.uniq) + [ns])
end

#serialize(*args, &block) ⇒ Object

Serialize Node using options. Save options can also be set using a block. See SaveOptions.

These two statements are equivalent:

node.serialize(:encoding => 'UTF-8', :save_with => FORMAT | AS_XML)

or

node.serialize(:encoding => 'UTF-8') do |config|
  config.format.as_xml
end


715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
# File 'lib/nokogiri/xml/node.rb', line 715

def serialize *args, &block
  options = args.first.is_a?(Hash) ? args.shift : {
    :encoding   => args[0],
    :save_with  => args[1] || SaveOptions::FORMAT
  }

  encoding = options[:encoding] || document.encoding

  outstring = ""
  if encoding && outstring.respond_to?(:force_encoding)
    outstring.force_encoding(Encoding.find(encoding))
  end
  io = StringIO.new(outstring)
  write_to io, options, &block
  io.string
end

#set_attributeObject



423
# File 'lib/nokogiri/xml/node.rb', line 423

alias :set_attribute  :[]=

#swap(node_or_tags) ⇒ Object

Swap this Node for node_or_tags node_or_tags can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.

Returns self, to support chaining of calls.

Also see related method replace.



406
407
408
409
# File 'lib/nokogiri/xml/node.rb', line 406

def swap node_or_tags
  replace node_or_tags
  self
end

#textObject Also known as: to_str



424
# File 'lib/nokogiri/xml/node.rb', line 424

alias :text           :content

#text?Boolean

Returns true if this is a Text node

Returns:

  • (Boolean)


578
579
580
# File 'lib/nokogiri/xml/node.rb', line 578

def text?
  type == TEXT_NODE
end

#to_html(options = {}) ⇒ Object

Serialize this Node to HTML

doc.to_html

See Node#write_to for a list of options. For formatted output, use Node#to_xhtml instead.



739
740
741
742
743
744
745
746
747
748
749
# File 'lib/nokogiri/xml/node.rb', line 739

def to_html options = {}
  # FIXME: this is a hack around broken libxml versions
  return dump_html if %w[2 6] === LIBXML_VERSION.split('.')[0..1]

  options[:save_with] ||= SaveOptions::FORMAT |
                          SaveOptions::NO_DECLARATION |
                          SaveOptions::NO_EMPTY_TAGS |
                          SaveOptions::AS_HTML

  serialize(options)
end

#to_sObject

Turn this node in to a string. If the document is HTML, this method returns html. If the document is XML, this method returns XML.



611
612
613
# File 'lib/nokogiri/xml/node.rb', line 611

def to_s
  document.xml? ? to_xml : to_html
end

#to_xhtml(options = {}) ⇒ Object

Serialize this Node to XHTML using options

doc.to_xhtml(:indent => 5, :encoding => 'UTF-8')

See Node#write_to for a list of options



769
770
771
772
773
774
775
776
777
778
779
# File 'lib/nokogiri/xml/node.rb', line 769

def to_xhtml options = {}
  # FIXME: this is a hack around broken libxml versions
  return dump_html if %w[2 6] === LIBXML_VERSION.split('.')[0..1]

  options[:save_with] ||= SaveOptions::FORMAT |
                          SaveOptions::NO_DECLARATION |
                          SaveOptions::NO_EMPTY_TAGS |
                          SaveOptions::AS_XHTML

  serialize(options)
end

#to_xml(options = {}) ⇒ Object

Serialize this Node to XML using options

doc.to_xml(:indent => 5, :encoding => 'UTF-8')

See Node#write_to for a list of options



757
758
759
760
761
# File 'lib/nokogiri/xml/node.rb', line 757

def to_xml options = {}
  options[:save_with] ||= SaveOptions::FORMAT | SaveOptions::AS_XML

  serialize(options)
end

#traverse(&block) ⇒ Object

Yields self and all children to block recursively.



682
683
684
685
# File 'lib/nokogiri/xml/node.rb', line 682

def traverse &block
  children.each{|j| j.traverse(&block) }
  block.call(self)
end

#typeObject



430
# File 'lib/nokogiri/xml/node.rb', line 430

alias :type           :node_type

Unlink this node from its current context.



397
398
399
400
401
# File 'ext/nokogiri/xml_node.c', line 397

def unlink
  LibXML.xmlUnlinkNode(cstruct)
  cstruct.keep_reference_from_document!
  self
end

#valuesObject

Get the attribute values for this Node.



449
450
451
# File 'lib/nokogiri/xml/node.rb', line 449

def values
  attribute_nodes.map { |node| node.value }
end

#write_html_to(io, options = {}) ⇒ Object

Write Node as HTML to io with options

See Node#write_to for a list of options



816
817
818
819
820
821
822
823
824
825
# File 'lib/nokogiri/xml/node.rb', line 816

def write_html_to io, options = {}
  # FIXME: this is a hack around broken libxml versions
  return (io << dump_html) if %w[2 6] === LIBXML_VERSION.split('.')[0..1]

  options[:save_with] ||= SaveOptions::FORMAT |
    SaveOptions::NO_DECLARATION |
    SaveOptions::NO_EMPTY_TAGS |
    SaveOptions::AS_HTML
  write_to io, options
end

#write_to(io, *options) {|config| ... } ⇒ Object

Write Node to io with options. options modify the output of this method. Valid options are:

  • :encoding for changing the encoding

  • :indent_text the indentation text, defaults to one space

  • :indent the number of :indent_text to use, defaults to 2

  • :save_with a combination of SaveOptions constants.

To save with UTF-8 indented twice:

node.write_to(io, :encoding => 'UTF-8', :indent => 2)

To save indented with two dashes:

node.write_to(io, :indent_text => '-', :indent => 2

Yields:

  • (config)


798
799
800
801
802
803
804
805
806
807
808
809
810
# File 'lib/nokogiri/xml/node.rb', line 798

def write_to io, *options
  options       = options.first.is_a?(Hash) ? options.shift : {}
  encoding      = options[:encoding] || options[0]
  save_options  = options[:save_with] || options[1] || SaveOptions::FORMAT
  indent_text   = options[:indent_text] || ' '
  indent_times  = options[:indent] || 2


  config = SaveOptions.new(save_options)
  yield config if block_given?

  native_write_to(io, encoding, indent_text * indent_times, config.options)
end

#write_xhtml_to(io, options = {}) ⇒ Object

Write Node as XHTML to io with options

See Node#write_to for a list of options



831
832
833
834
835
836
837
838
839
840
# File 'lib/nokogiri/xml/node.rb', line 831

def write_xhtml_to io, options = {}
  # FIXME: this is a hack around broken libxml versions
  return (io << dump_html) if %w[2 6] === LIBXML_VERSION.split('.')[0..1]

  options[:save_with] ||= SaveOptions::FORMAT |
    SaveOptions::NO_DECLARATION |
    SaveOptions::NO_EMPTY_TAGS |
    SaveOptions::AS_XHTML
  write_to io, options
end

#write_xml_to(io, options = {}) ⇒ Object

Write Node as XML to io with options

doc.write_xml_to io, :encoding => 'UTF-8'

See Node#write_to for a list of options



848
849
850
851
# File 'lib/nokogiri/xml/node.rb', line 848

def write_xml_to io, options = {}
  options[:save_with] ||= SaveOptions::FORMAT | SaveOptions::AS_XML
  write_to io, options
end

#xml?Boolean

Returns true if this is an XML::Document node

Returns:

  • (Boolean)


568
569
570
# File 'lib/nokogiri/xml/node.rb', line 568

def xml?
  type == DOCUMENT_NODE
end

#xpath(*paths) ⇒ Object

call-seq: xpath *paths, [namespace-bindings, variable-bindings, custom-handler-class]

Search this node for XPath paths. paths must be one or more XPath queries.

node.xpath('.//title')

A hash of namespace bindings may be appended. For example:

node.xpath('.//foo:name', {'foo' => 'http://example.org/'})
node.xpath('.//xmlns:name', node.root.namespaces)

A hash of variable bindings may also be appended to the namespace bindings. For example:

node.xpath('.//address[@domestic=$value]', nil, {:value => 'Yes'})

Custom XPath functions may also be defined. To define custom functions create a class and implement the function you want to define. The first argument to the method will be the current matching NodeSet. Any other arguments are ones that you pass in. Note that this class may appear anywhere in the argument list. For example:

node.xpath('.//title[regex(., "\w+")]', Class.new {
  def regex node_set, regex
    node_set.find_all { |node| node['some_attribute'] =~ /#{regex}/ }
  end
}.new)


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/nokogiri/xml/node.rb', line 145

def xpath *paths
  return NodeSet.new(document) unless document

  paths, handler, ns, binds = extract_params(paths)

  sets = paths.map { |path|
    ctx = XPathContext.new(self)
    ctx.register_namespaces(ns)

    binds.each do |key,value|
      ctx.register_variable key.to_s, value
    end if binds

    ctx.evaluate(path, handler)
  }
  return sets.first if sets.length == 1

  NodeSet.new(document) do |combined|
    sets.each do |set|
      set.each do |node|
        combined << node
      end
    end
  end
end