class

XML::Node

Inherits Reference / Object

Constants

LOOKS_LIKE_XPATH = /^(\.\/|\/|\.\.|\.$)/

Instance methods

==(other : Node)

Compares with other.

Source
[](attribute : String) : String

Gets the attribute content for the attribute given by name. Raises KeyError if attribute is not found.

Source
[]=(name : String, value)

Sets attribute of this node to value. Raises XML::Error if this node does not support attributes.

Source
[]?(attribute : String) : String | Nil

Gets the attribute content for the attribute given by name. Returns nil if attribute is not found.

Source
attribute?

Returns true if this is an attribute node.

Source
attributes

Returns attributes of this node as an XML::Attributes.

Source
cdata?

Returns true if this is a CDATA section node.

Source
children

Gets the list of children for this node as a XML::NodeSet.

Source
comment?

Returns true if this is a comment node.

Source
content

Returns the content for this Node. An empty string is returned if the node has no content.

Source
content=(content)

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

Source
delete(name : String) : String | Nil

Deletes attribute given by name. Returns attributes value, or nil if attribute not found.

Source
document

Gets the document for this node.

Source
document?

Returns true if this is a Document or HTML Document node.

Source
element?

Returns true if this is an Element node.

Source
encoding

Returns the encoding of this node's document.

Source
errors

DEPRECATED Use XML::Document#errors instead.

Source
first_element_child

Returns the first child node of this node that is an element. Returns nil if not found.

Source
fragment?

Returns true if this is a DocumentFragment.

Source
hash(hasher)

See Object#hash(hasher)

Source
inner_text

Returns the content for this Node.

Source
inspect(io : IO) : Nil

Returns detailed information for this node including node type, name, attributes and children.

Source
name

Returns the name for this Node.

Source
name=(name)

Sets the name for this Node.

Source
namespace

Returns the namespace for this node or nil if not found.

Source
namespace_definitions

Returns namespaces defined on this node directly.

Source
namespace_scopes

Returns namespaces in scope for this node – those defined on this node directly or any ancestor node – as an Array of XML::Namespace objects.

Default namespaces ("xmlns=" style) for this node are included in this array; default namespaces for ancestors, however, are not.

See also #namespaces

Source
namespaces

Returns a Hash(String, String?) of prefix => href for all namespaces on this node and its ancestors.

This method returns the same namespaces as #namespace_scopes.

Returns namespaces in scope for this node – those defined on this node directly or any ancestor node – as a Hash of attribute-name/value pairs.

NOTE: 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.

Source
next

Returns the next sibling node or nil if not found.

Source
next_element

Returns the next element node sibling or nil if not found.

Source
next_sibling

Returns the next sibling node or nil if not found.

Source
object_id

Returns the address of underlying LibXML::Node* in memory.

Source
parent

Returns the parent node or nil if not found.

Source
pretty_print(pp : PrettyPrint) : Nil

Pretty prints self into the given printer.

By default appends a text that is the result of invoking #inspect on self. Subclasses should override for custom pretty printing.

Source
previous

Returns the previous sibling node or nil if not found.

Source
previous_element

Returns the previous sibling node that is an element or nil if not found.

Source
previous_sibling

Returns the previous sibling node or nil if not found. Same with #previous.

Source
processing_instruction?

Returns true if this is a Processing Instruction node.

Source
root

Returns the root node for this document or nil.

Source
text

Same as #content.

Source
text=(text)

Same as #content=.

Source
text?

Returns true if this is a Text node.

Source
to_s(io : IO) : Nil

Serialize this Node as XML to io using default options.

See #to_xml.

Source
to_xml(indent : Int = 2, indent_text = " ", options : SaveOptions = SaveOptions.xml_default) : String

Serialize this Node as XML and return a String using default options.

See XML::SaveOptions.xml_default for default options.

Source
to_xml(io : IO, indent = 2, indent_text = " ", options : SaveOptions = SaveOptions.xml_default)

Serialize this Node as XML to io using default options.

See XML::SaveOptions.xml_default for default options.

Source
type

Returns the type for this Node as XML::Node::Type.

Source
version

Returns the version of this node's document.

Source
xml?

Returns true if this is an xml Document node.

Source
xpath(path, namespaces = nil, variables = nil)

Searches this node for XPath path. Returns result with appropriate type (Bool | Float64 | String | XML::NodeSet).

Raises XML::Error on evaluation error.

Source
xpath_bool(path, namespaces = nil, variables = nil)

Searches this node for XPath path and restricts the return type to Bool.

require "xml"

doc = XML.parse("<person></person>")

doc.xpath_bool("count(//person) > 0") # => true
Source
xpath_float(path, namespaces = nil, variables = nil)

Searches this node for XPath path and restricts the return type to Float64.

require "xml"

doc = XML.parse("<person></person>")

doc.xpath_float("count(//person)") # => 1.0
Source
xpath_node(path, namespaces = nil, variables = nil)

Searches this node for XPath path for nodes and returns the first one. or nil if not found

require "xml"

doc = XML.parse("<person></person>")

doc.xpath_node("//person")  # => #<XML::Node:0x2013e80 name="person">
doc.xpath_node("//invalid") # => nil
Source
xpath_nodes(path, namespaces = nil, variables = nil)

Searches this node for XPath path and restricts the return type to NodeSet.

require "xml"

doc = XML.parse("<person></person>")

nodes = doc.xpath_nodes("//person")
nodes.class       # => XML::NodeSet
nodes.map(&.name) # => ["person"]
Source
xpath_string(path, namespaces = nil, variables = nil)

Searches this node for XPath path and restricts the return type to String.

require "xml"

doc = XML.parse("<person></person>")

doc.xpath_string("string(/persons/person[1])")
Source

Nested types