Yex.XmlElement (y_ex v0.10.5)

View Source

A shared type that represents an XML node. Provides functionality for manipulating XML elements including child nodes, attributes, and navigation.

Summary

Functions

Returns a stream of all child nodes of the XML element.

Deletes a range of child nodes starting at the specified index. Returns :ok on success, :error on failure.

Retrieves the child node at the specified index. Returns {:ok, node} if found, :error if index is out of bounds.

Similar to fetch/2 but raises ArgumentError if the index is out of bounds.

Returns the first child node of the XML element. Returns nil if the element has no children.

Gets a child node by index from the XML element, or returns the default value if the index is out of bounds.

Returns the value of the specified attribute. Returns nil if the attribute does not exist.

Returns a map of all attributes for this XML element.

Gets a child node by index from the XML element, or lazily evaluates the given function if the index is out of bounds. This is useful when the default value is expensive to compute and should only be evaluated when needed.

Returns the tag name of the XML element. Returns nil if the element has no tag.

Inserts a new child node at the specified index. Returns :ok on success, :error on failure.

Inserts a new child node after the specified reference node. If the reference node is not found, inserts at the beginning. Returns :ok on success, :error on failure.

Inserts a new child node after the specified reference node and returns the inserted node. If the reference node is not found, inserts at the beginning. Returns the inserted node on success, raises on failure.

Inserts a new child node at the specified index and returns the inserted node. Returns the inserted node on success, raises on failure.

Adds or updates an attribute with the specified key and value. Returns :ok on success, :error on failure.

Returns the number of child nodes in the XML element.

The next sibling of this type. Is null if this is the last child of its parent.

The parent that holds this type. Is null if this xml is a top-level XML type.

The previous sibling of this type. Is null if this is the first child of its parent.

Appends a new child node at the end of the children list. Returns :ok on success, :error on failure.

Appends a new child node at the end of the children list and returns the inserted node. Returns the inserted node on success, raises on failure.

Removes the attribute with the specified key. Returns :ok on success, :error on failure.

Inserts a new child node at the beginning of the children list. Returns :ok on success, :error on failure.

Types

t()

@type t() :: %Yex.XmlElement{doc: Yex.Doc.t(), reference: reference()}

Functions

as_prelim(xml_element)

@spec as_prelim(t()) :: Yex.XmlElementPrelim.t()

children(xml_element)

@spec children(t()) :: Enumerable.t(t() | Yex.XmlText.t())

Returns a stream of all child nodes of the XML element.

delete(xml_element, index, length)

@spec delete(t(), integer(), integer()) :: :ok | :error

Deletes a range of child nodes starting at the specified index. Returns :ok on success, :error on failure.

fetch(xml_element, index)

@spec fetch(t(), integer()) :: {:ok, t() | Yex.XmlText.t()} | :error

Retrieves the child node at the specified index. Returns {:ok, node} if found, :error if index is out of bounds.

fetch!(map, index)

@spec fetch!(t(), integer()) :: t() | Yex.XmlText.t()

Similar to fetch/2 but raises ArgumentError if the index is out of bounds.

first_child(xml_element)

@spec first_child(t()) :: t() | Yex.XmlText.t() | nil

Returns the first child node of the XML element. Returns nil if the element has no children.

get(xml_element, index, default \\ nil)

@spec get(t(), integer(), default :: term()) :: t() | Yex.XmlText.t() | term()

Gets a child node by index from the XML element, or returns the default value if the index is out of bounds.

Parameters

  • xml_element - The XML element to query
  • index - The index to look up
  • default - The default value to return if index is out of bounds (defaults to nil)

Examples

iex> doc = Yex.Doc.new()
iex> xml = Yex.Doc.get_xml_fragment(doc, "xml")
iex> elem = Yex.XmlFragment.push_and_get(xml, Yex.XmlElementPrelim.empty("div"))
iex> Yex.XmlElement.push(elem, Yex.XmlTextPrelim.from("content"))
iex> text = Yex.XmlElement.get(elem, 0)
iex> match?(%Yex.XmlText{}, text)
true
iex> Yex.XmlElement.get(elem, 10)
nil
iex> Yex.XmlElement.get(elem, 10, :not_found)
:not_found

get_attribute(xml_element, key)

@spec get_attribute(t(), binary()) :: binary() | Yex.SharedType.t() | nil

Returns the value of the specified attribute. Returns nil if the attribute does not exist.

get_attributes(xml_element)

@spec get_attributes(t()) :: %{required(binary()) => binary() | Yex.SharedType.t()}

Returns a map of all attributes for this XML element.

get_lazy(xml_element, index, fun)

@spec get_lazy(t(), integer(), fun :: (-> term())) :: t() | Yex.XmlText.t() | term()

Gets a child node by index from the XML element, or lazily evaluates the given function if the index is out of bounds. This is useful when the default value is expensive to compute and should only be evaluated when needed.

Parameters

  • xml_element - The XML element to query
  • index - The index to look up
  • fun - A function that returns the default value (only called if index is out of bounds)

Examples

iex> doc = Yex.Doc.new()
iex> xml = Yex.Doc.get_xml_fragment(doc, "xml")
iex> elem = Yex.XmlFragment.push_and_get(xml, Yex.XmlElementPrelim.empty("div"))
iex> Yex.XmlElement.push(elem, Yex.XmlTextPrelim.from("Hello"))
iex> Yex.XmlElement.get_lazy(elem, 0, fn -> Yex.XmlElement.push_and_get(elem, Yex.XmlTextPrelim.from("Computed")) end) |> to_string()
"Hello"
iex> Yex.XmlElement.get_lazy(elem, 10, fn -> Yex.XmlElement.push_and_get(elem, Yex.XmlTextPrelim.from("Computed")) end) |> to_string()
"Computed"

Particularly useful with *_and_get functions for get-or-create patterns:

iex> doc = Yex.Doc.new()
iex> xml = Yex.Doc.get_xml_fragment(doc, "xml")
iex> elem = Yex.XmlFragment.push_and_get(xml, Yex.XmlElementPrelim.empty("div"))
iex> # Get existing child or create and return new one
iex> child = Yex.XmlElement.get_lazy(elem, 0, fn ->
...>   Yex.XmlElement.push_and_get(elem, Yex.XmlElementPrelim.empty("span"))
...> end)
iex> Yex.XmlElement.get_tag(child)
"span"

get_tag(xml_element)

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

Returns the tag name of the XML element. Returns nil if the element has no tag.

insert(xml_element, index, content)

@spec insert(t(), integer(), Yex.XmlElementPrelim.t() | Yex.XmlTextPrelim.t()) ::
  :ok | :error

Inserts a new child node at the specified index. Returns :ok on success, :error on failure.

insert_after(xml_element, ref, content)

@spec insert_after(
  t(),
  t() | Yex.XmlText.t(),
  Yex.XmlElementPrelim.t() | Yex.XmlTextPrelim.t()
) :: :ok | :error

Inserts a new child node after the specified reference node. If the reference node is not found, inserts at the beginning. Returns :ok on success, :error on failure.

insert_after_and_get(xml_element, ref, content)

@spec insert_after_and_get(
  t(),
  t() | Yex.XmlText.t(),
  Yex.XmlElementPrelim.t() | Yex.XmlTextPrelim.t()
) :: t() | Yex.XmlText.t()

Inserts a new child node after the specified reference node and returns the inserted node. If the reference node is not found, inserts at the beginning. Returns the inserted node on success, raises on failure.

insert_and_get(xml_element, index, content)

@spec insert_and_get(t(), integer(), Yex.XmlElementPrelim.t() | Yex.XmlTextPrelim.t()) ::
  t() | Yex.XmlText.t()

Inserts a new child node at the specified index and returns the inserted node. Returns the inserted node on success, raises on failure.

insert_attribute(xml_element, key, value)

@spec insert_attribute(t(), binary(), binary() | Yex.PrelimType.t()) :: :ok | :error

Adds or updates an attribute with the specified key and value. Returns :ok on success, :error on failure.

length(xml_element)

@spec length(t()) :: integer()

Returns the number of child nodes in the XML element.

next_sibling(xml_element)

@spec next_sibling(t()) :: t() | Yex.XmlText.t() | nil

The next sibling of this type. Is null if this is the last child of its parent.

parent(xml_element)

@spec parent(t()) :: t() | Yex.XmlFragment.t() | nil

The parent that holds this type. Is null if this xml is a top-level XML type.

prev_sibling(xml_element)

@spec prev_sibling(t()) :: t() | Yex.XmlText.t() | nil

The previous sibling of this type. Is null if this is the first child of its parent.

push(xml_element, content)

@spec push(t(), Yex.XmlElementPrelim.t() | Yex.XmlTextPrelim.t()) :: :ok | :error

Appends a new child node at the end of the children list. Returns :ok on success, :error on failure.

push_and_get(xml_element, content)

@spec push_and_get(t(), Yex.XmlElementPrelim.t() | Yex.XmlTextPrelim.t()) ::
  t() | Yex.XmlText.t()

Appends a new child node at the end of the children list and returns the inserted node. Returns the inserted node on success, raises on failure.

remove_attribute(xml_element, key)

@spec remove_attribute(t(), binary()) :: :ok | :error

Removes the attribute with the specified key. Returns :ok on success, :error on failure.

to_string(xml_element)

@spec to_string(t()) :: binary()

unshift(xml_element, content)

@spec unshift(t(), Yex.XmlElementPrelim.t() | Yex.XmlTextPrelim.t()) :: :ok | :error

Inserts a new child node at the beginning of the children list. Returns :ok on success, :error on failure.