exampple v0.3.0 Exampple.Xml.Xmlel
Xmlel is a struct data which is intended to help with the parsing of the XML elements.
Link to this section Summary
Functions
This function removes the extra spaces inside of the stanzas to ensure we can perform matching in a proper way.
This function is a helper function to translate the tuples coming from Saxy to the Xmlel structs.
Deletes an attribute 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
Access the value under key and update it at the same time
Retrieve an attribute from a Xmlel struct.
Creates a Xmlel struct.
Parser a XML string into Xmlel struct.
Pop the value under key
Add or set an attribute inside of the Xmlel struct passed as parameter.
Add or set one or several attributes inside of the Xmlel struct passed as parameter.
Sigil to use ~X to provide XML text and transform it to Xmlel struct.
Sigil to use ~x to provide XML text and transform it to Xmlel struct removing spaces and breaking lines.
Link to this section Types
Link to this section Functions
clean_spaces(xmlel)
This function removes the extra spaces inside of the stanzas 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>"
decode(data)
This function is a helper function to translate the tuples coming from Saxy to the 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!"]}
delete_attr(xmlel, name)
Deletes an attribute 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
encode(xmlel)
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/>"
fetch(xmlel, key)
Access the value stored under key
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
get_and_update(el, key, function)
Access the value under key and update it at the same time
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"}}
get_attr(xmlel, name, default \\ nil)
Retrieve an attribute 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.get_attr(xmlel, "surname")
nil
new(name, attrs \\ %{}, children \\ [])
Creates a Xmlel struct.
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"}
parse(xml)
Parser a XML string into 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/>"}
pop(el, key)
Pop the value under key
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"}}
put_attr(xmlel, name, value)
Add or set an 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"
put_attrs(xmlel, fields)
Add or set one or several attributes inside of the Xmlel struct passed as parameter.
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\"/>"
sigil_X(string, addons)
Sigil to use ~X to provide XML text and transform it to Xmlel struct.
Examples:
iex> import Exampple.Xml.Xmlel
iex> ~X|<foo>
iex> </foo>
iex> |
%Exampple.Xml.Xmlel{attrs: %{}, children: ["\n "], name: "foo"}
sigil_x(string, addons)
Sigil to use ~x to provide XML text and transform it to Xmlel struct removing spaces and breaking lines.
Examples:
iex> import Exampple.Xml.Xmlel
iex> ~x|<foo>
iex> </foo>
iex> |
%Exampple.Xml.Xmlel{attrs: %{}, children: [], name: "foo"}