Module: LibxmlBindings::XML::Document

Included in:
LibXML::XML::Document
Defined in:
lib/libxml_bindings.rb

Instance Method Summary collapse

Instance Method Details

#last=(node) ⇒ Object

:call-seq:

doc.last = node -> XML::Node

Appends node to the end of the list of nodes at this level



16
17
18
# File 'lib/libxml_bindings.rb', line 16

def last=(node)
  self.last? ? self.root.sibling=node : self.root = node
end

#nodeObject

:call-seq:

doc.node["/xpath"] -> XML::Node

Returns the first Node object matching “/xpath”, or nil if no object could not be found.

Note its usually recommended to remove all namespaces with strip! before xpath traversal.



26
27
28
# File 'lib/libxml_bindings.rb', line 26

def node()
  self.root.node()
end

#node_at(xpath) ⇒ Object

:nodoc:



30
31
32
# File 'lib/libxml_bindings.rb', line 30

def node_at(xpath) #:nodoc:
  self.root.node_at(xpath)
end

#nodesObject

:call-seq:

doc.nodes["/xpath"] -> [XML::Node, XML::Node, XML::Node, ...]

Returns an array containing all nodes matching “/xpath”, or nil if no object could not be found.

Note its usually recommended to remove all namespaces with strip! before xpath traversal.



40
41
42
# File 'lib/libxml_bindings.rb', line 40

def nodes()
  self.root.nodes()
end

#nodes_at(xpath) ⇒ Object

:nodoc:



44
45
46
# File 'lib/libxml_bindings.rb', line 44

def nodes_at(xpath) #:nodoc:
  self.root.search(xpath)
end

#strip!Object Also known as: strip_namespaces!

:call-seq:

doc.strip! -> XML::Document
doc.strip_namespaces! -> XML::Document

Strip all namespaces from this XML::Document. replaces the root node with a stripped XML::Node (and children)

Note Xml elements with a namespace context are usually omited from xpath search criteria.

See: libxml.rubyforge.org/rdoc/classes/LibXML/XML/XPath.html



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/libxml_bindings.rb', line 58

def strip!
  # Parse xml
  xml_str = self.to_s
  # puts "xml_str=#{xml_str}\\n"
  xml_str.gsub! /\ ?(soap|xmlns)(:\w*)?=\"[^\"]*\"/, ""
  xml_str.gsub! /\<\w*:/, "<"
  xml_str.gsub! /\<\/\w*:/, "</"
  # puts "xml_str=#{xml_str}\\n"
  xml_doc_stripped = xml_str.to_xmldoc
  self.root = xml_doc_stripped.root.copy(true)
  return self
end

#to_xmlObject

:call-seq:

doc.to_xml -> String

Alias for XML::Document.to_s



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

def to_xml
  return self.to_s
end