Sablon.XML.Node (sablon v0.4.4)

Copy Markdown View Source

A handle on a node inside a Sablon.XML.Document.

The API mirrors the subset of Nokogiri used by the Ruby sablon gem: navigation (parent/1, ancestors/2, next_element/1), querying (xpath/2, css/2) and in-place mutation (remove/1, add_next_sibling/2, replace/2, ...). All mutating functions operate on the document's process dictionary store and therefore affect every handle pointing at the same node.

Summary

Functions

Appends child to the node's children, detaching it from a previous parent.

Inserts sibling directly after node.

Inserts sibling directly before node.

Ancestors of the node, closest first.

Like css/2 but returns only the first match.

Like xpath/3 but returns only the first match.

Returns true when the node is still attached to its document.

Fetches a single attribute value or nil.

All attributes as an ordered keyword-like list of {name, value} tuples.

All child nodes, in document order.

Alias of text/1, mirroring Nokogiri's content.

Evaluates a (very small subset of a) CSS selector, e.g. Relationship[Id="rId4"].

Removes an attribute.

Deep copies the node. The copy is detached and, by default, belongs to the same document. Pass a target document to copy across documents.

Only the child nodes that are elements.

The first child element or nil.

Serialises the children of node, i.e. its inner XML.

The local part of the node name, i.e. "p" for "w:p".

The qualified node name, e.g. "w:p".

Builds a new, detached element node belonging to doc.

Builds a new, detached text node.

The next sibling that is an element, mirroring Nokogiri's next_element.

The next sibling node, of any type.

Alias of name/1, mirroring Nokogiri's node_name.

Serialises a list of nodes to XML.

The parent node, or nil for the document node and detached nodes.

The namespace prefix of the node name or nil.

Prepends child to the node's children.

The previous sibling that is an element.

The previous sibling node, of any type.

Sets an attribute, preserving the position of an existing one.

Sets an attribute matched by its local name, keeping whatever prefix it already uses. Does nothing when no such attribute exists.

Detaches the node from its parent. The node itself stays usable.

Replaces node with the given node or list of nodes.

Returns true when both handles point at the same node of the same document.

Alias of xpath/3, mirroring Nokogiri's search.

Replaces all children of node with nodes.

Replaces all children of node with a single text node.

The concatenated text content of the subtree.

Serialises the node (and its subtree) to XML.

Walks the subtree rooted at node in document order, node included.

The node type: :element, :text, :cdata, :comment, :pi, :decl, :doctype or :document.

Evaluates an XPath expression relative to node. See Sablon.XML.XPath.

Types

t()

@type t() :: %Sablon.XML.Node{doc: Sablon.XML.Document.t(), id: non_neg_integer()}

Functions

add_child(node, child)

@spec add_child(t(), t()) :: t()

Appends child to the node's children, detaching it from a previous parent.

add_next_sibling(node, sibling)

@spec add_next_sibling(t(), t()) :: t()

Inserts sibling directly after node.

add_previous_sibling(node, sibling)

@spec add_previous_sibling(t(), t()) :: t()

Inserts sibling directly before node.

ancestors(node, selector \\ nil)

@spec ancestors(t(), binary() | nil) :: [t()]

Ancestors of the node, closest first.

When a selector is given only ancestors matching it are returned. The selector accepts the same limited form used throughout sablon, e.g. ".//w:p" which matches ancestor elements named w:p.

at_css(node, selector)

@spec at_css(t(), binary()) :: t() | nil

Like css/2 but returns only the first match.

at_xpath(node, expression, namespaces \\ [])

@spec at_xpath(t(), binary(), keyword()) :: t() | nil

Like xpath/3 but returns only the first match.

attached?(node)

@spec attached?(t()) :: boolean()

Returns true when the node is still attached to its document.

attr(node, key)

@spec attr(t(), binary() | atom()) :: binary() | nil

Fetches a single attribute value or nil.

attributes(node)

@spec attributes(t()) :: [{binary(), binary()}]

All attributes as an ordered keyword-like list of {name, value} tuples.

children(node)

@spec children(t()) :: [t()]

All child nodes, in document order.

content(node)

@spec content(t()) :: binary()

Alias of text/1, mirroring Nokogiri's content.

css(node, selector)

@spec css(t(), binary()) :: [t()]

Evaluates a (very small subset of a) CSS selector, e.g. Relationship[Id="rId4"].

delete_attr(node, key)

@spec delete_attr(t(), binary() | atom()) :: t()

Removes an attribute.

dup(node, target \\ nil)

@spec dup(t(), Sablon.XML.Document.t() | nil) :: t()

Deep copies the node. The copy is detached and, by default, belongs to the same document. Pass a target document to copy across documents.

element_children(node)

@spec element_children(t()) :: [t()]

Only the child nodes that are elements.

first_element_child(node)

@spec first_element_child(t()) :: t() | nil

The first child element or nil.

inner_xml(node)

@spec inner_xml(t()) :: binary()

Serialises the children of node, i.e. its inner XML.

local_name(node)

@spec local_name(t()) :: binary()

The local part of the node name, i.e. "p" for "w:p".

name(node)

@spec name(t()) :: binary()

The qualified node name, e.g. "w:p".

new(name, doc)

@spec new(binary(), Sablon.XML.Document.t() | t()) :: t()

Builds a new, detached element node belonging to doc.

Mirrors Nokogiri::XML::Node.new(name, document).

new_text(text, doc)

@spec new_text(binary(), Sablon.XML.Document.t() | t()) :: t()

Builds a new, detached text node.

next_element(node)

@spec next_element(t()) :: t() | nil

The next sibling that is an element, mirroring Nokogiri's next_element.

next_sibling(node)

@spec next_sibling(t()) :: t() | nil

The next sibling node, of any type.

node_name(node)

@spec node_name(t()) :: binary()

Alias of name/1, mirroring Nokogiri's node_name.

nodes_to_xml(nodes)

@spec nodes_to_xml([t()]) :: binary()

Serialises a list of nodes to XML.

parent(node)

@spec parent(t()) :: t() | nil

The parent node, or nil for the document node and detached nodes.

prefix(node)

@spec prefix(t()) :: binary() | nil

The namespace prefix of the node name or nil.

prepend_child(node, child)

@spec prepend_child(t(), t()) :: t()

Prepends child to the node's children.

previous_element(node)

@spec previous_element(t()) :: t() | nil

The previous sibling that is an element.

previous_sibling(node)

@spec previous_sibling(t()) :: t() | nil

The previous sibling node, of any type.

put_attr(node, key, value)

@spec put_attr(t(), binary() | atom(), term()) :: t()

Sets an attribute, preserving the position of an existing one.

put_local_attr(node, local_name, value)

@spec put_local_attr(t(), binary(), term()) :: t()

Sets an attribute matched by its local name, keeping whatever prefix it already uses. Does nothing when no such attribute exists.

remove(node)

@spec remove(t()) :: t()

Detaches the node from its parent. The node itself stays usable.

replace(node, replacement)

@spec replace(t(), t() | [t()]) :: t()

Replaces node with the given node or list of nodes.

same?(arg1, arg2)

@spec same?(t() | nil, t() | nil) :: boolean()

Returns true when both handles point at the same node of the same document.

search(node, expression)

@spec search(t(), binary()) :: [t()]

Alias of xpath/3, mirroring Nokogiri's search.

set_children(node, nodes)

@spec set_children(t(), [t()]) :: t()

Replaces all children of node with nodes.

set_content(node, value)

@spec set_content(t(), binary()) :: t()

Replaces all children of node with a single text node.

text(node)

@spec text(t()) :: binary()

The concatenated text content of the subtree.

to_xml(node)

@spec to_xml(t()) :: binary()

Serialises the node (and its subtree) to XML.

traverse(node)

@spec traverse(t()) :: [t()]

Walks the subtree rooted at node in document order, node included.

type(node)

@spec type(t()) :: atom()

The node type: :element, :text, :cdata, :comment, :pi, :decl, :doctype or :document.

xpath(node, expression, namespaces \\ [])

@spec xpath(t(), binary(), keyword()) :: [t()]

Evaluates an XPath expression relative to node. See Sablon.XML.XPath.