struct

Mica::XML::Node

Inherits Struct < Value < Object

A node of a Mica::XML::Document: an element, attribute, text, CDATA section, comment, processing instruction, DOCTYPE, entity reference or the document itself.

Node is a struct holding its document and an index, so it is cheap to pass around and two Node values for the same node compare equal. The API mirrors stdlib XML::Node.

Instance methods

==(other : Node) : Bool
Source
[](name : String) : String

Returns the value of attribute name. Raises KeyError when missing.

Source

Sets attribute name to value, adding it when missing. A number or Bool is written with to_s (at most a few dozen bytes).

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

Returns the value of attribute name (matched by qualified name, then by local name), or nil.

Source
attribute?
Source
attributes

Returns the element's attributes (namespace declarations excluded).

Source
cdata?
Source
children

Returns the child nodes.

Source
comment?
Source
content

Returns the text content: the concatenated text of all descendant text and CDATA nodes for elements and the document; the value for other nodes.

Source
content=(text : String) : String

Sets the content. For an element this replaces its children with one text node holding text (escaped on output, never parsed as markup).

Source
delete(name : String) : String | Nil

Removes attribute name and returns its value, or nil when missing.

Source
document

Returns the document this node belongs to.

Source
document?

Returns true for the document node.

Source
dtd?
Source
element?
Source
entity_reference?
Source
first_element_child

Returns the first child that is an element, or nil.

Source
fragment?

Always false: Mica has no document fragments.

Source
hash(hasher)

See Object#hash(hasher)

Source
inner_text

Same as content.

Source
inspect(io : IO) : Nil

Appends this struct's name and instance variables names and values to the given IO.

struct Point
  def initialize(@x : Int32, @y : Int32)
  end
end

p1 = Point.new 1, 2
p1.to_s    # "Point(@x=1, @y=2)"
p1.inspect # "Point(@x=1, @y=2)"
Source
name

Returns the node's name: the local name for elements and attributes (as in stdlib), the target for PIs, the name for entity references and the DOCTYPE; "text", "#cdata-section", "comment" or "document" otherwise.

Source
name=(new_name : String) : String

Renames an element, attribute or PI. A prefixed name is resolved against the namespace declarations in scope.

Raises Mica::Error for an invalid name or an unbound prefix.

Source
namespace

Returns the namespace of an element or attribute, or nil when it is in no namespace.

Source
namespace_definitions

Returns the namespaces declared on this element.

Source
namespace_scopes

Returns the namespaces in scope on this node: its own declarations and its ancestors', the innermost declaration of each prefix winning.

Source
namespace_uri

Returns the namespace URI of an element or attribute, or nil.

Source
namespaces

Returns the namespaces in scope as "xmlns:prefix" => uri (and "xmlns" => uri for the default namespace), like stdlib.

Source
next

Returns the next sibling, or nil.

Source
next_element

Returns the next sibling that is an element, or nil.

Source
next_sibling

Returns the next sibling, or nil.

Source
parent

Returns the parent node (the owning element for attributes), or nil for the document and for unlinked nodes.

Source
prefix

Returns the namespace prefix of an element or attribute, or nil.

Source
previous

Returns the previous sibling, or nil.

Source
previous_element

Returns the previous sibling that is an element, or nil.

Source
previous_sibling

Returns the previous sibling, or nil.

Source
processing_instruction?
Source
qualified_name

Returns the qualified name (prefix:local) of an element or attribute; the same as name for other nodes.

Source
root

Returns the document's root element, or nil.

Source
text

Same as content.

Source
text=(text : String) : String

Same as content=.

Source
text?
Source
to_s(io : IO) : Nil

Same as #inspect(io).

Source
to_xml(indent : Int = 2, indent_text : String = " ") : String

Serializes this node as XML. With indent above 0, elements whose children are all elements, comments or PIs are indented (mixed content is written as is, like libxml2's format mode).

Source
to_xml(io : IO, indent : Int = 2, indent_text : String = " ") : Nil

Serializes this node as XML. With indent above 0, elements whose children are all elements, comments or PIs are indented (mixed content is written as is, like libxml2's format mode).

Source
type

Returns the node's type.

Source
xml?

Same as document? (stdlib name).

Source
xpath(path : String, namespaces : Hash(String, String) | Nil = nil, variables : Hash(String, XPath::Value) | Nil = nil, *, budget : Core::WorkBudget = XPath::DEFAULT_WORK_BUDGET, meter : Core::WorkMeter | Nil = nil) : XPath::Value

Evaluates XPath path with this node as the context node. Returns a NodeSet, String, Float64 or Bool.

namespaces maps prefixes to URIs. Without it, the namespaces declared on the root element are registered (the default namespace under the prefix xmlns), like stdlib. variables binds $name references. The evaluation runs under budget, or draws on meter when given (see XPath::Expression#evaluate).

Raises Mica::XML::XPathError on a syntax error or an unsupported feature, and Mica::LimitExceededError when the budget or deadline runs out.

Source
xpath_bool(path : String, namespaces : Hash(String, String) | Nil = nil, variables : Hash(String, XPath::Value) | Nil = nil, *, budget : Core::WorkBudget = XPath::DEFAULT_WORK_BUDGET, meter : Core::WorkMeter | Nil = nil) : Bool

Evaluates path and converts the result to a boolean.

Source
xpath_float(path : String, namespaces : Hash(String, String) | Nil = nil, variables : Hash(String, XPath::Value) | Nil = nil, *, budget : Core::WorkBudget = XPath::DEFAULT_WORK_BUDGET, meter : Core::WorkMeter | Nil = nil) : Float64

Evaluates path and converts the result to a number.

Source
xpath_node(path : String, namespaces : Hash(String, String) | Nil = nil, variables : Hash(String, XPath::Value) | Nil = nil, *, budget : Core::WorkBudget = XPath::DEFAULT_WORK_BUDGET, meter : Core::WorkMeter | Nil = nil) : Node | Nil

Evaluates path and returns the first node, or nil.

Source
xpath_nodes(path : String, namespaces : Hash(String, String) | Nil = nil, variables : Hash(String, XPath::Value) | Nil = nil, *, budget : Core::WorkBudget = XPath::DEFAULT_WORK_BUDGET, meter : Core::WorkMeter | Nil = nil) : NodeSet

Evaluates path and returns the resulting node set. Raises Mica::XML::XPathError when the result is not a node set.

Source
xpath_string(path : String, namespaces : Hash(String, String) | Nil = nil, variables : Hash(String, XPath::Value) | Nil = nil, *, budget : Core::WorkBudget = XPath::DEFAULT_WORK_BUDGET, meter : Core::WorkMeter | Nil = nil) : String

Evaluates path and converts the result to a string (XPath string() rules: a node set gives its first node's string value).

Source

Nested types