Proximal.Xmlel (proximal v0.2.3)
Xmlel is a struct data which is intended to help with the parsing of the XML elements.
Summary
Types
Xmlel.t defines the xmlel element which contains the name, the
attrs (attributes) and children for the XML tags.
Functions
Provide the name of the children tags.
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 Proximal.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
Proximal.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 Proximal.Xmlel struct.
Pop the value under key passed an Proximal.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.
Types
@type attr_value() :: String.t()
@type attrs() :: %{required(attr_name()) => attr_value()}
@type t() :: %Proximal.Xmlel{ attrs: attrs(), children: [t() | String.t() | struct()], full_name: String.t(), name: String.t(), namespaces: %{required(String.t()) => String.t()}, schema: String.t() | nil }
Xmlel.t defines the xmlel element which contains the name, the
attrs (attributes) and children for the XML tags.
Functions
Provide the name of the children tags.
Examples:
iex> import Proximal.Xmlel, only: [sigil_x: 2]
iex> xmlel = ~x[<root><child1/><child2/><child3/></root>]
iex> Proximal.Xmlel.children_tag_names(xmlel)
["child1", "child2", "child3"]
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> |> Proximal.Xmlel.parse()
iex> |> Proximal.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 Proximal.Xmlel structs.
Examples:
iex> Proximal.Xmlel.decode({"foo", [], []})
%Proximal.Xmlel{full_name: "foo", name: "foo", attrs: %{}, children: []}
iex> Proximal.Xmlel.decode({"foo", [], [{:characters, "1&1"}]})
%Proximal.Xmlel{full_name: "foo", name: "foo", children: ["1&1"]}
iex> Proximal.Xmlel.decode({"bar", [{"id", "10"}], ["Hello!"]})
%Proximal.Xmlel{full_name: "bar", name: "bar", attrs: %{"id" => "10"}, children: ["Hello!"]}
Deletes an attribute by name from a xmlel struct.
Examples:
iex> attrs = %{"id" => "100", "name" => "Alice"}
iex> xmlel = %Proximal.Xmlel{attrs: attrs}
iex> Proximal.Xmlel.get_attr(xmlel, "name")
"Alice"
iex> Proximal.Xmlel.delete_attr(xmlel, "name")
iex> |> Proximal.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> Proximal.Xmlel.encode(%Proximal.Xmlel{name: "foo"})
{"foo", [], []}
iex> Proximal.Xmlel.encode(%Proximal.Xmlel{name: "bar", attrs: %{"id" => "10"}, children: ["Hello!"]})
{"bar", [{"id", "10"}], [{:characters, "Hello!"}]}
iex> Proximal.Xmlel.encode(%TestBuild{name: "bro"})
{"bro", [], []}
Access the value stored under key passing the stanza in
Proximal.Xmlel format into the xmlel parameter.
Examples:
iex> import Proximal.Xmlel
iex> el = ~x(<foo><c1 v="1"/><c1 v="2"/><c2/></foo>)
iex> fetch(el, "c1")
{:ok, [%Proximal.Xmlel{attrs: %{"v" => "1"}, children: [], name: "c1", full_name: "c1"}, %Proximal.Xmlel{attrs: %{"v" => "2"}, children: [], name: "c1", full_name: "c1"}]}
iex> fetch(el, "nonexistent")
:error
Access the value under key and update it at the same time for the xmlel
using the function passed as paramter.
Examples:
iex> import Proximal.Xmlel
iex> el = ~x(<foo><c1 v="1"/><c1 v="2"/><c2/></foo>)
iex> fun = fn els ->
iex> values = Enum.map(els, fn %Proximal.Xmlel{attrs: %{"v" => v}} = el -> %Proximal.Xmlel{el | attrs: %{"v" => "v" <> v}} end)
iex> {els, values}
iex> end
iex> get_and_update(el, "c1", fun)
{[%Proximal.Xmlel{attrs: %{"v" => "1"}, children: [], name: "c1", full_name: "c1"}, %Proximal.Xmlel{attrs: %{"v" => "2"}, children: [], name: "c1", full_name: "c1"}], %Proximal.Xmlel{attrs: %{}, children: [%Proximal.Xmlel{attrs: %{"v" => "v1"}, children: [], name: "c1", full_name: "c1"}, %Proximal.Xmlel{attrs: %{"v" => "v2"}, children: [], name: "c1", full_name: "c1"}, %Proximal.Xmlel{attrs: %{}, children: [], name: "c2", full_name: "c2"}], name: "foo", full_name: "foo"}}
iex> fun = fn _els -> :pop end
iex> get_and_update(el, "c1", fun)
{[%Proximal.Xmlel{attrs: %{"v" => "1"}, children: [], name: "c1", full_name: "c1"}, %Proximal.Xmlel{attrs: %{"v" => "2"}, children: [], name: "c1", full_name: "c1"}], %Proximal.Xmlel{attrs: %{}, children: [%Proximal.Xmlel{attrs: %{}, children: [], name: "c2", full_name: "c2"}], name: "foo", full_name: "foo"}}
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", {"ns1", "name"} => "Alice"}
iex> xmlel = %Proximal.Xmlel{attrs: attrs}
iex> Proximal.Xmlel.get_attr(xmlel, "name")
"Alice"
iex> Proximal.Xmlel.get_attr(xmlel, "id")
"100"
iex> Proximal.Xmlel.get_attr(xmlel, "ns1:name")
"Alice"
iex> Proximal.Xmlel.get_attr(xmlel, "surname")
nil
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
Proximal.Xmlel elements.
Examples:
iex> Proximal.Xmlel.new("foo")
%Proximal.Xmlel{full_name: "foo", attrs: %{}, children: [], name: "foo"}
iex> Proximal.Xmlel.new("bar", %{"id" => "10"})
%Proximal.Xmlel{full_name: "bar", attrs: %{"id" => "10"}, children: [], name: "bar"}
iex> Proximal.Xmlel.new("bar", [{"id", "10"}])
%Proximal.Xmlel{full_name: "bar", attrs: %{"id" => "10"}, children: [], name: "bar"}
Parser a xml string into Proximal.Xmlel struct.
Examples:
iex> Proximal.Xmlel.parse("<foo/>")
{%Proximal.Xmlel{full_name: "foo", name: "foo", attrs: %{}, children: []}, ""}
iex> Proximal.Xmlel.parse("<foo bar='10'>hello world!</foo>")
{%Proximal.Xmlel{full_name: "foo", name: "foo", attrs: %{"bar" => "10"}, children: ["hello world!"]}, ""}
iex> Proximal.Xmlel.parse("<foo><bar>hello world!</bar></foo>")
{%Proximal.Xmlel{full_name: "foo", name: "foo", attrs: %{}, children: [%Proximal.Xmlel{full_name: "bar", name: "bar", attrs: %{}, children: ["hello world!"]}]}, ""}
iex> Proximal.Xmlel.parse("<foo/><bar/>")
{%Proximal.Xmlel{full_name: "foo", name: "foo", attrs: %{}, children: []}, "<bar/>"}
Pop the value under key passed an Proximal.Xmlel struct as element.
Examples:
iex> import Proximal.Xmlel
iex> el = ~x(<foo><c1 v="1"/><c1 v="2"/><c2/></foo>)
iex> pop(el, "c1")
{[%Proximal.Xmlel{attrs: %{"v" => "1"}, children: [], name: "c1", full_name: "c1"}, %Proximal.Xmlel{attrs: %{"v" => "2"}, children: [], name: "c1", full_name: "c1"}], %Proximal.Xmlel{attrs: %{}, children: [%Proximal.Xmlel{attrs: %{}, children: [], name: "c2", full_name: "c2"}], name: "foo", full_name: "foo"}}
iex> pop(el, "nonexistent")
{[], %Proximal.Xmlel{attrs: %{}, children: [%Proximal.Xmlel{attrs: %{"v" => "1"}, children: [], name: "c1", full_name: "c1"}, %Proximal.Xmlel{attrs: %{"v" => "2"}, children: [], name: "c1", full_name: "c1"}, %Proximal.Xmlel{attrs: %{}, children: [], name: "c2", full_name: "c2"}], name: "foo", full_name: "foo"}}
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> %Proximal.Xmlel{attrs: attrs}
iex> |> Proximal.Xmlel.put_attr("name", "Bob")
iex> |> Proximal.Xmlel.get_attr("name")
"Bob"
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> Proximal.Xmlel.put_attrs(%Proximal.Xmlel{name: "foo"}, fields) |> to_string()
"<foo city=\"Cordoba\" id=\"100\" name=\"Alice\"/>"
iex> fields = %{"id" => "100", "name" => "Alice", "city" => :"Cordoba"}
iex> Proximal.Xmlel.put_attrs(%Proximal.Xmlel{name: "foo"}, fields) |> to_string()
"<foo id=\"100\" name=\"Alice\"/>"
Sigil to use ~X to provide XML string and transform it to Xmlel struct.
Note that we are not using addons.
Examples:
iex> import Proximal.Xmlel
iex> ~X|<foo>
iex> </foo>
iex> |
%Proximal.Xmlel{attrs: %{}, children: ["\n "], name: "foo", full_name: "foo"}
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 Proximal.Xmlel
iex> ~x|<foo>
iex> </foo>
iex> |
%Proximal.Xmlel{attrs: %{}, children: [], name: "foo", full_name: "foo"}