Exampple.Xml.Xmlel (exampple v0.7.2)

Xmlel is a struct data which is intended to help with the parsing of the XML elements.

Link to this section Summary

Types

t()

Xmlel.t defines the xmlel element which contains the name, the attrs (attributes) and children for the XML tags.

Functions

This function removes the extra spaces inside of the stanzas starting from xmlel to ensure we can perform matching in a proper way.

This function is a helper function to translate the tuples coming from Saxy into de data parameter to the Exampple.Xml.Xmlel structs.

Deletes an attribute by name from a xmlel struct.

This function is a helper function to translate the content of the xmlel structs to the tuples needed by Saxy.

Access the value stored under key passing the stanza in Exampple.Xml.Xmlel format into the xmlel parameter.

Access the value under key and update it at the same time for the xmlel using the function passed as paramter.

Retrieve an attribute by name from a xmlel struct. If the value is not found the default value is used instead. If default is not provided then nil is used as default value.

Creates a Xmlel struct passing the name of the stanza, the attrs as a map or keyword list to create the attributes and children for the payload of the XML tag. This is not recursive so it's intended the children has to be in a correct format.

Parser a xml string into Exampple.Xml.Xmlel struct.

Pop the value under key passed an Exampple.Xml.Xmlel struct as element.

Add or set a value by name as attribute inside of the xmlel struct passed as parameter.

Add or set one or several attributes using fields inside of the xmlel struct passed as parameter. The fields data are in keyword list format.

Sigil to use ~X to provide XML string and transform it to Xmlel struct. Note that we are not using addons.

Sigil to use ~x to provide XML string and transform it to Xmlel struct removing spaces and breaking lines. Note that we are not using addons.

Link to this section Types

Specs

attr_name() :: binary()
Link to this type

attr_value()

Specs

attr_value() :: binary()

Specs

attrs() :: %{required(attr_name()) => attr_value()}

Specs

children() :: [t()] | [String.t()]

Specs

t() :: %Exampple.Xml.Xmlel{
  attrs: attrs(),
  children: [t() | binary() | struct()],
  name: binary()
}

Xmlel.t defines the xmlel element which contains the name, the attrs (attributes) and children for the XML tags.

Link to this section Functions

Link to this function

clean_spaces(xmlel)

This function removes the extra spaces inside of the stanzas starting from xmlel to ensure we can perform matching in a proper way.

Examples:

iex> "<foo>\n    <bar>\n        Hello<br/>world!\n    </bar>\n</foo>"
iex> |> Exampple.Xml.Xmlel.parse()
iex> |> Exampple.Xml.Xmlel.clean_spaces()
iex> |> to_string()
"<foo><bar>Hello<br/>world!</bar></foo>"

This function is a helper function to translate the tuples coming from Saxy into de data parameter to the Exampple.Xml.Xmlel structs.

Examples:

iex> Exampple.Xml.Xmlel.decode({"foo", [], []})
%Exampple.Xml.Xmlel{name: "foo", attrs: %{}, children: []}

iex> Exampple.Xml.Xmlel.decode({"bar", [{"id", "10"}], ["Hello!"]})
%Exampple.Xml.Xmlel{name: "bar", attrs: %{"id" => "10"}, children: ["Hello!"]}
Link to this function

delete_attr(xmlel, name)

Deletes an attribute by name from a xmlel struct.

Examples:

iex> attrs = %{"id" => "100", "name" => "Alice"}
iex> xmlel = %Exampple.Xml.Xmlel{attrs: attrs}
iex> Exampple.Xml.Xmlel.get_attr(xmlel, "name")
"Alice"
iex> Exampple.Xml.Xmlel.delete_attr(xmlel, "name")
iex> |> Exampple.Xml.Xmlel.get_attr("name")
nil

This function is a helper function to translate the content of the xmlel structs to the tuples needed by Saxy.

Examples:

iex> Exampple.Xml.Xmlel.encode(%Exampple.Xml.Xmlel{name: "foo"})
{"foo", [], []}

iex> Exampple.Xml.Xmlel.encode(%Exampple.Xml.Xmlel{name: "bar", attrs: %{"id" => "10"}, children: ["Hello!"]})
{"bar", [{"id", "10"}], ["Hello!"]}

iex> Exampple.Xml.Xmlel.encode(%TestBuild{name: "bro"})
"<bro/>"
Link to this function

fetch(xmlel, key)

Access the value stored under key passing the stanza in Exampple.Xml.Xmlel format into the xmlel parameter.

Examples:

iex> import Exampple.Xml.Xmlel
iex> el = ~x(<foo><c1 v="1"/><c1 v="2"/><c2/></foo>)
iex> fetch(el, "c1")
{:ok, [%Exampple.Xml.Xmlel{attrs: %{"v" => "1"}, children: [], name: "c1"}, %Exampple.Xml.Xmlel{attrs: %{"v" => "2"}, children: [], name: "c1"}]}
iex> fetch(el, "nonexistent")
:error
Link to this function

get_and_update(xmlel, key, function)

Access the value under key and update it at the same time for the xmlel using the function passed as paramter.

Examples:

iex> import Exampple.Xml.Xmlel
iex> el = ~x(<foo><c1 v="1"/><c1 v="2"/><c2/></foo>)
iex> fun = fn els ->
iex> values = Enum.map(els, fn %Exampple.Xml.Xmlel{attrs: %{"v" => v}} = el -> %Exampple.Xml.Xmlel{el | attrs: %{"v" => "v" <> v}} end)
iex> {els, values}
iex> end
iex> get_and_update(el, "c1", fun)
{[%Exampple.Xml.Xmlel{attrs: %{"v" => "1"}, children: [], name: "c1"}, %Exampple.Xml.Xmlel{attrs: %{"v" => "2"}, children: [], name: "c1"}], %Exampple.Xml.Xmlel{attrs: %{}, children: [%Exampple.Xml.Xmlel{attrs: %{"v" => "v1"}, children: [], name: "c1"}, %Exampple.Xml.Xmlel{attrs: %{"v" => "v2"}, children: [], name: "c1"}, %Exampple.Xml.Xmlel{attrs: %{}, children: [], name: "c2"}], name: "foo"}}
iex> fun = fn _els -> :pop end
iex> get_and_update(el, "c1", fun)
{[%Exampple.Xml.Xmlel{attrs: %{"v" => "1"}, children: [], name: "c1"}, %Exampple.Xml.Xmlel{attrs: %{"v" => "2"}, children: [], name: "c1"}], %Exampple.Xml.Xmlel{attrs: %{}, children: [%Exampple.Xml.Xmlel{attrs: %{}, children: [], name: "c2"}], name: "foo"}}
Link to this function

get_attr(xmlel, name, default \\ nil)

Retrieve an attribute by name from a xmlel struct. If the value is not found the default value is used instead. If default is not provided then nil is used as default value.

Examples:

iex> attrs = %{"id" => "100", "name" => "Alice"}
iex> xmlel = %Exampple.Xml.Xmlel{attrs: attrs}
iex> Exampple.Xml.Xmlel.get_attr(xmlel, "name")
"Alice"
iex> Exampple.Xml.Xmlel.get_attr(xmlel, "surname")
nil
Link to this function

new(name, attrs \\ %{}, children \\ [])

Specs

new(name :: binary(), attrs() | [{attr_name(), attr_value()}], children()) ::
  t()

Creates a Xmlel struct passing the name of the stanza, the attrs as a map or keyword list to create the attributes and children for the payload of the XML tag. This is not recursive so it's intended the children has to be in a correct format.

The children could be or binaries (strings) representing CDATA or other Exampple.Xml.Xmlel elements.

Examples:

iex> Exampple.Xml.Xmlel.new("foo")
%Exampple.Xml.Xmlel{attrs: %{}, children: [], name: "foo"}

iex> Exampple.Xml.Xmlel.new("bar", %{"id" => "10"})
%Exampple.Xml.Xmlel{attrs: %{"id" => "10"}, children: [], name: "bar"}

iex> Exampple.Xml.Xmlel.new("bar", [{"id", "10"}])
%Exampple.Xml.Xmlel{attrs: %{"id" => "10"}, children: [], name: "bar"}

Parser a xml string into Exampple.Xml.Xmlel struct.

Examples:

iex> Exampple.Xml.Xmlel.parse("<foo/>")
{%Exampple.Xml.Xmlel{name: "foo", attrs: %{}, children: []}, ""}

iex> Exampple.Xml.Xmlel.parse("<foo bar='10'>hello world!</foo>")
{%Exampple.Xml.Xmlel{name: "foo", attrs: %{"bar" => "10"}, children: ["hello world!"]}, ""}

iex> Exampple.Xml.Xmlel.parse("<foo><bar>hello world!</bar></foo>")
{%Exampple.Xml.Xmlel{name: "foo", attrs: %{}, children: [%Exampple.Xml.Xmlel{name: "bar", attrs: %{}, children: ["hello world!"]}]}, ""}

iex> Exampple.Xml.Xmlel.parse("<foo/><bar/>")
{%Exampple.Xml.Xmlel{name: "foo", attrs: %{}, children: []}, "<bar/>"}
Link to this function

pop(element, key)

Pop the value under key passed an Exampple.Xml.Xmlel struct as element.

Examples:

iex> import Exampple.Xml.Xmlel
iex> el = ~x(<foo><c1 v="1"/><c1 v="2"/><c2/></foo>)
iex> pop(el, "c1")
{[%Exampple.Xml.Xmlel{attrs: %{"v" => "1"}, children: [], name: "c1"}, %Exampple.Xml.Xmlel{attrs: %{"v" => "2"}, children: [], name: "c1"}], %Exampple.Xml.Xmlel{attrs: %{}, children: [%Exampple.Xml.Xmlel{attrs: %{}, children: [], name: "c2"}], name: "foo"}}
iex> pop(el, "nonexistent")
{[], %Exampple.Xml.Xmlel{attrs: %{}, children: [%Exampple.Xml.Xmlel{attrs: %{"v" => "1"}, children: [], name: "c1"}, %Exampple.Xml.Xmlel{attrs: %{"v" => "2"}, children: [], name: "c1"}, %Exampple.Xml.Xmlel{attrs: %{}, children: [], name: "c2"}], name: "foo"}}
Link to this function

put_attr(xmlel, name, value)

Add or set a value by name as attribute inside of the xmlel struct passed as parameter.

Examples:

iex> attrs = %{"id" => "100", "name" => "Alice"}
iex> %Exampple.Xml.Xmlel{attrs: attrs}
iex> |> Exampple.Xml.Xmlel.put_attr("name", "Bob")
iex> |> Exampple.Xml.Xmlel.get_attr("name")
"Bob"
Link to this function

put_attrs(xmlel, fields)

Add or set one or several attributes using fields inside of the xmlel struct passed as parameter. The fields data are in keyword list format.

Examples:

iex> fields = %{"id" => "100", "name" => "Alice", "city" => "Cordoba"}
iex> Exampple.Xml.Xmlel.put_attrs(%Exampple.Xml.Xmlel{name: "foo"}, fields) |> to_string()
"<foo city=\"Cordoba\" id=\"100\" name=\"Alice\"/>"

iex> fields = %{"id" => "100", "name" => "Alice", "city" => :"Cordoba"}
iex> Exampple.Xml.Xmlel.put_attrs(%Exampple.Xml.Xmlel{name: "foo"}, fields) |> to_string()
"<foo id=\"100\" name=\"Alice\"/>"
Link to this function

sigil_X(string, addons)

Sigil to use ~X to provide XML string and transform it to Xmlel struct. Note that we are not using addons.

Examples:

iex> import Exampple.Xml.Xmlel
iex> ~X|<foo>
iex> </foo>
iex> |
%Exampple.Xml.Xmlel{attrs: %{}, children: ["\n "], name: "foo"}
Link to this function

sigil_x(string, addons)

Sigil to use ~x to provide XML string and transform it to Xmlel struct removing spaces and breaking lines. Note that we are not using addons.

Examples:

iex> import Exampple.Xml.Xmlel
iex> ~x|<foo>
iex> </foo>
iex> |
%Exampple.Xml.Xmlel{attrs: %{}, children: [], name: "foo"}