2002-11-20 21:19:20 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : xml.erl
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2002-11-20 21:19:20 +01:00
|
|
|
%%% Purpose : XML utils
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Created : 20 Nov 2002 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2012-02-23 16:52:34 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2012 ProcessOne
|
2007-12-24 13:58:05 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License
|
|
|
|
%%% along with this program; if not, write to the Free Software
|
|
|
|
%%% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2002-11-20 21:19:20 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(xml).
|
2012-09-11 15:45:59 +02:00
|
|
|
|
2007-12-24 13:58:05 +01:00
|
|
|
-author('alexey@process-one.net').
|
2002-11-20 21:19:20 +01:00
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
-export([element_to_binary/1,
|
|
|
|
crypt/1, make_text_node/1, remove_cdata/1,
|
|
|
|
remove_subtags/3, get_cdata/1, get_tag_cdata/1,
|
|
|
|
get_attr/2, get_attr_s/2, get_tag_attr/2,
|
|
|
|
get_tag_attr_s/2, get_subtag/2, get_subtag_cdata/2,
|
|
|
|
append_subtags/2, get_path_s/2, start/0,
|
|
|
|
replace_tag_attr/3, to_xmlel/1]).
|
2002-11-20 21:19:20 +01:00
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
-include("jlib.hrl").
|
2010-07-01 12:54:01 +02:00
|
|
|
-include("ejabberd.hrl").
|
|
|
|
|
2007-07-31 14:05:00 +02:00
|
|
|
%% Select at compile time how to escape characters in binary text
|
|
|
|
%% nodes.
|
|
|
|
%% Can be choosen with ./configure --enable-full-xml
|
|
|
|
-ifdef(FULL_XML_SUPPORT).
|
2012-09-11 15:45:59 +02:00
|
|
|
|
2007-07-31 14:05:00 +02:00
|
|
|
-define(ESCAPE_BINARY(CData), make_text_node(CData)).
|
2012-09-11 15:45:59 +02:00
|
|
|
|
2007-07-31 14:05:00 +02:00
|
|
|
-else.
|
2012-09-11 15:45:59 +02:00
|
|
|
|
2007-07-31 14:05:00 +02:00
|
|
|
-define(ESCAPE_BINARY(CData), crypt(CData)).
|
2012-09-11 15:45:59 +02:00
|
|
|
|
2007-07-31 14:05:00 +02:00
|
|
|
-endif.
|
|
|
|
|
2010-07-01 12:54:01 +02:00
|
|
|
%% Replace element_to_binary/1 with NIF
|
|
|
|
%% Can be choosen with ./configure --enable-nif
|
|
|
|
-ifdef(NIF).
|
2012-09-11 15:45:59 +02:00
|
|
|
|
2010-07-01 12:54:01 +02:00
|
|
|
start() ->
|
|
|
|
SOPath = filename:join(ejabberd:get_so_path(), "xml"),
|
|
|
|
case catch erlang:load_nif(SOPath, 0) of
|
2012-09-11 15:45:59 +02:00
|
|
|
ok -> ok;
|
|
|
|
Err -> ?WARNING_MSG("unable to load xml NIF: ~p", [Err])
|
2010-07-01 12:54:01 +02:00
|
|
|
end.
|
2012-09-11 15:45:59 +02:00
|
|
|
|
2010-07-01 12:54:01 +02:00
|
|
|
-else.
|
2012-09-11 15:45:59 +02:00
|
|
|
|
|
|
|
start() -> ok.
|
|
|
|
|
2010-07-01 12:54:01 +02:00
|
|
|
-endif.
|
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
%%
|
|
|
|
-spec(element_to_binary/1 ::
|
|
|
|
(
|
|
|
|
El :: xmlel() | cdata())
|
|
|
|
-> binary()
|
|
|
|
).
|
|
|
|
|
2010-06-04 05:31:34 +02:00
|
|
|
element_to_binary(El) ->
|
|
|
|
iolist_to_binary(element_to_string(El)).
|
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
%%
|
|
|
|
-spec(element_to_string/1 ::
|
|
|
|
(
|
|
|
|
El :: xmlel() | cdata())
|
|
|
|
-> string()
|
|
|
|
).
|
|
|
|
|
2004-04-10 21:15:02 +02:00
|
|
|
element_to_string(El) ->
|
2009-04-27 23:36:41 +02:00
|
|
|
case catch element_to_string_nocatch(El) of
|
2012-09-11 15:45:59 +02:00
|
|
|
{'EXIT', Reason} -> erlang:error({badxml, El, Reason});
|
|
|
|
Result -> Result
|
2009-04-27 23:36:41 +02:00
|
|
|
end.
|
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
-spec(element_to_string_nocatch/1 ::
|
|
|
|
(
|
|
|
|
El :: xmlel() | cdata())
|
|
|
|
-> iolist()
|
|
|
|
).
|
|
|
|
|
2009-04-27 23:36:41 +02:00
|
|
|
element_to_string_nocatch(El) ->
|
2004-04-10 21:15:02 +02:00
|
|
|
case El of
|
2012-09-11 15:45:59 +02:00
|
|
|
#xmlel{name = Name, attrs = Attrs, children = Els} ->
|
|
|
|
if Els /= [] ->
|
|
|
|
[$<, Name, attrs_to_list(Attrs), $>,
|
|
|
|
[element_to_string_nocatch(E) || E <- Els], $<, $/,
|
|
|
|
Name, $>];
|
|
|
|
true -> [$<, Name, attrs_to_list(Attrs), $/, $>]
|
|
|
|
end;
|
|
|
|
%% We do not crypt CDATA binary, but we enclose it in XML CDATA
|
|
|
|
{xmlcdata, CData} ->
|
|
|
|
?ESCAPE_BINARY(CData)
|
2004-04-10 21:15:02 +02:00
|
|
|
end.
|
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
attrs_to_list(Attrs) -> [attr_to_list(A) || A <- Attrs].
|
2004-04-10 21:15:02 +02:00
|
|
|
|
|
|
|
attr_to_list({Name, Value}) ->
|
2009-10-19 12:30:41 +02:00
|
|
|
[$\s, Name, $=, $', crypt(Value), $'].
|
2003-05-18 18:41:15 +02:00
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
crypt(S) ->
|
|
|
|
<< <<(case C of
|
|
|
|
$& -> <<"&">>;
|
|
|
|
$< -> <<"<">>;
|
|
|
|
$> -> <<">">>;
|
|
|
|
$" -> <<""">>;
|
|
|
|
$' -> <<"'">>;
|
|
|
|
_ -> <<C>>
|
|
|
|
end)/binary>>
|
|
|
|
|| <<C>> <= S >>.
|
|
|
|
|
|
|
|
%%
|
|
|
|
-spec(make_text_node/1 ::
|
|
|
|
(
|
|
|
|
CData :: binary())
|
|
|
|
-> binary()
|
|
|
|
).
|
|
|
|
|
2007-07-30 19:35:00 +02:00
|
|
|
make_text_node(CData) ->
|
|
|
|
case cdata_need_escape(CData) of
|
2012-09-11 15:45:59 +02:00
|
|
|
cdata ->
|
|
|
|
CDATA1 = <<"<![CDATA[">>,
|
|
|
|
CDATA2 = <<"]]>">>,
|
|
|
|
iolist_to_binary([CDATA1, CData, CDATA2]);
|
|
|
|
none -> CData;
|
|
|
|
{cdata, EndTokens} ->
|
|
|
|
EscapedCData = escape_cdata(CData, EndTokens),
|
|
|
|
iolist_to_binary(EscapedCData)
|
2007-07-30 19:35:00 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
cdata_need_escape(CData) ->
|
|
|
|
cdata_need_escape(CData, 0, false, []).
|
2012-09-11 15:45:59 +02:00
|
|
|
|
|
|
|
cdata_need_escape(<<>>, _, false, _) -> none;
|
|
|
|
cdata_need_escape(<<>>, _, true, []) -> cdata;
|
2007-07-30 19:35:00 +02:00
|
|
|
cdata_need_escape(<<>>, _, true, CDataEndTokens) ->
|
|
|
|
{cdata, lists:reverse(CDataEndTokens)};
|
2012-09-11 15:45:59 +02:00
|
|
|
cdata_need_escape(<<$], $], $>, Rest/binary>>,
|
|
|
|
CurrentPosition, _XMLEscape, CDataEndTokens) ->
|
2007-07-30 19:35:00 +02:00
|
|
|
NewPosition = CurrentPosition + 3,
|
|
|
|
cdata_need_escape(Rest, NewPosition, true,
|
2012-09-11 15:45:59 +02:00
|
|
|
[CurrentPosition + 1 | CDataEndTokens]);
|
2007-07-30 19:35:00 +02:00
|
|
|
%% Only <, & need to be escaped in XML text node
|
|
|
|
%% See reference: http://www.w3.org/TR/xml11/#syntax
|
2012-09-11 15:45:59 +02:00
|
|
|
cdata_need_escape(<<$<, Rest/binary>>, CurrentPosition,
|
|
|
|
_XMLEscape, CDataEndTokens) ->
|
|
|
|
cdata_need_escape(Rest, CurrentPosition + 1, true,
|
|
|
|
CDataEndTokens);
|
|
|
|
cdata_need_escape(<<$&, Rest/binary>>, CurrentPosition,
|
|
|
|
_XMLEscape, CDataEndTokens) ->
|
|
|
|
cdata_need_escape(Rest, CurrentPosition + 1, true,
|
|
|
|
CDataEndTokens);
|
|
|
|
cdata_need_escape(<<_:8, Rest/binary>>, CurrentPosition,
|
2007-07-30 19:35:00 +02:00
|
|
|
XMLEscape, CDataEndTokens) ->
|
2012-09-11 15:45:59 +02:00
|
|
|
cdata_need_escape(Rest, CurrentPosition + 1, XMLEscape,
|
|
|
|
CDataEndTokens).
|
|
|
|
|
2007-07-30 19:35:00 +02:00
|
|
|
escape_cdata(CData, EndTokens) ->
|
|
|
|
escape_cdata(CData, 0, EndTokens, []).
|
2012-09-11 15:45:59 +02:00
|
|
|
|
2007-07-30 19:35:00 +02:00
|
|
|
escape_cdata(<<>>, _CurrentPosition, [], Acc) ->
|
|
|
|
lists:reverse(Acc);
|
|
|
|
escape_cdata(Rest, CurrentPosition, [], Acc) ->
|
|
|
|
CDATA1 = <<"<![CDATA[">>,
|
|
|
|
CDATA2 = <<"]]>">>,
|
2012-09-11 15:45:59 +02:00
|
|
|
escape_cdata(<<>>, CurrentPosition, [],
|
|
|
|
[CDATA2, Rest, CDATA1 | Acc]);
|
|
|
|
escape_cdata(CData, Index, [Pos | Positions], Acc) ->
|
2007-07-30 19:35:00 +02:00
|
|
|
CDATA1 = <<"<![CDATA[">>,
|
|
|
|
CDATA2 = <<"]]>">>,
|
2012-09-11 15:45:59 +02:00
|
|
|
Split = Pos - Index,
|
|
|
|
{Part, Rest} = split_binary(CData, Split + 1),
|
|
|
|
escape_cdata(Rest, Pos + 1, Positions,
|
|
|
|
[CDATA2, Part, CDATA1 | Acc]).
|
|
|
|
|
|
|
|
%%
|
|
|
|
-spec(remove_cdata_p/1 ::
|
|
|
|
(
|
|
|
|
El :: xmlel() | cdata())
|
|
|
|
-> boolean()
|
|
|
|
).
|
|
|
|
|
|
|
|
remove_cdata_p(#xmlel{}) -> true;
|
2003-01-09 20:59:16 +01:00
|
|
|
remove_cdata_p(_) -> false.
|
2002-11-20 21:19:20 +01:00
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
%%
|
|
|
|
-spec(remove_cdata/1 ::
|
|
|
|
(
|
|
|
|
L :: [xmlel() | cdata()])
|
|
|
|
-> [xmlel()]
|
|
|
|
).
|
|
|
|
|
2003-01-09 20:59:16 +01:00
|
|
|
remove_cdata(L) -> [E || E <- L, remove_cdata_p(E)].
|
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
-spec(remove_subtags/3 ::
|
|
|
|
(
|
|
|
|
Xmlel :: xmlel(),
|
|
|
|
Name :: binary(),
|
|
|
|
Attr :: attr())
|
|
|
|
-> Xmlel :: xmlel()
|
|
|
|
).
|
|
|
|
|
|
|
|
remove_subtags(#xmlel{name = TagName, attrs = TagAttrs, children = Els},
|
|
|
|
Name, Attr) ->
|
|
|
|
#xmlel{name = TagName, attrs = TagAttrs,
|
|
|
|
children = remove_subtags1(Els, [], Name, Attr)}.
|
|
|
|
|
|
|
|
%%
|
|
|
|
-spec(remove_subtags1/4 ::
|
|
|
|
(
|
|
|
|
Els :: [xmlel() | cdata()],
|
|
|
|
NewEls :: [xmlel()],
|
|
|
|
Name :: binary(),
|
|
|
|
Attr :: attr())
|
|
|
|
-> NewEls :: [xmlel()]
|
|
|
|
).
|
2010-09-02 12:40:45 +02:00
|
|
|
|
|
|
|
remove_subtags1([], NewEls, _Name, _Attr) ->
|
|
|
|
lists:reverse(NewEls);
|
2012-09-11 15:45:59 +02:00
|
|
|
remove_subtags1([El | Els], NewEls, Name,
|
|
|
|
{AttrName, AttrValue} = Attr) ->
|
2010-09-02 12:40:45 +02:00
|
|
|
case El of
|
2012-09-11 15:45:59 +02:00
|
|
|
#xmlel{name = Name, attrs = Attrs} ->
|
|
|
|
case get_attr(AttrName, Attrs) of
|
|
|
|
false ->
|
|
|
|
remove_subtags1(Els, [El | NewEls], Name, Attr);
|
|
|
|
{value, AttrValue} ->
|
|
|
|
remove_subtags1(Els, NewEls, Name, Attr);
|
|
|
|
_ -> remove_subtags1(Els, [El | NewEls], Name, Attr)
|
|
|
|
end;
|
|
|
|
_ -> remove_subtags1(Els, [El | NewEls], Name, Attr)
|
2010-09-02 12:40:45 +02:00
|
|
|
end.
|
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
-spec(get_cdata/1 ::
|
|
|
|
(
|
|
|
|
L :: [xmlel() | cdata()])
|
|
|
|
-> binary()
|
|
|
|
).
|
2010-09-02 12:40:45 +02:00
|
|
|
|
2002-11-20 21:19:20 +01:00
|
|
|
get_cdata(L) ->
|
2012-09-11 15:45:59 +02:00
|
|
|
(iolist_to_binary(get_cdata(L, <<"">>))).
|
2002-11-20 21:19:20 +01:00
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
-spec(get_cdata/2 ::
|
|
|
|
(
|
|
|
|
L :: [xmlel() | cdata()],
|
|
|
|
S :: binary() | iolist())
|
|
|
|
-> binary() | iolist()
|
|
|
|
).
|
2002-12-08 18:23:21 +01:00
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
get_cdata([{xmlcdata, CData} | L], S) ->
|
|
|
|
get_cdata(L, [S, CData]);
|
|
|
|
get_cdata([_ | L], S) -> get_cdata(L, S);
|
|
|
|
get_cdata([], S) -> S.
|
|
|
|
|
|
|
|
-spec(get_tag_cdata/1 ::
|
|
|
|
(
|
|
|
|
Xmlel :: xmlel())
|
|
|
|
-> binary()
|
|
|
|
).
|
|
|
|
|
|
|
|
get_tag_cdata(#xmlel{children = Els}) -> get_cdata(Els).
|
|
|
|
|
|
|
|
%%
|
|
|
|
-spec(get_attr/2 ::
|
|
|
|
(
|
|
|
|
AttrName :: binary(),
|
|
|
|
Attrs :: [attr()])
|
|
|
|
-> {value, binary()}
|
|
|
|
| false
|
|
|
|
).
|
2002-11-20 21:19:20 +01:00
|
|
|
|
|
|
|
get_attr(AttrName, Attrs) ->
|
|
|
|
case lists:keysearch(AttrName, 1, Attrs) of
|
2012-09-11 15:45:59 +02:00
|
|
|
{value, {_, Val}} -> {value, Val};
|
|
|
|
_ -> false
|
2002-11-20 21:19:20 +01:00
|
|
|
end.
|
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
%%
|
|
|
|
-spec(get_attr_s/2 ::
|
|
|
|
(
|
|
|
|
AttrName :: binary(),
|
|
|
|
Attrs :: [attr()])
|
|
|
|
-> Val :: binary()
|
|
|
|
).
|
|
|
|
|
2002-11-20 21:19:20 +01:00
|
|
|
get_attr_s(AttrName, Attrs) ->
|
|
|
|
case lists:keysearch(AttrName, 1, Attrs) of
|
2012-09-11 15:45:59 +02:00
|
|
|
{value, {_, Val}} -> Val;
|
|
|
|
_ -> <<"">>
|
2002-11-20 21:19:20 +01:00
|
|
|
end.
|
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
%%
|
|
|
|
-spec(get_tag_attr/2 ::
|
|
|
|
(
|
|
|
|
AttrName :: binary(),
|
|
|
|
Xmlel :: xmlel())
|
|
|
|
-> {value, binary()}
|
|
|
|
| false
|
|
|
|
).
|
|
|
|
|
|
|
|
get_tag_attr(AttrName, #xmlel{attrs = Attrs}) ->
|
2003-01-01 20:54:44 +01:00
|
|
|
get_attr(AttrName, Attrs).
|
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
%%
|
|
|
|
-spec(get_tag_attr_s/2 ::
|
|
|
|
(
|
|
|
|
AttrName :: binary(),
|
|
|
|
Xmlel :: xmlel())
|
|
|
|
-> binary()
|
|
|
|
).
|
|
|
|
|
|
|
|
get_tag_attr_s(AttrName, #xmlel{attrs = Attrs}) ->
|
2003-01-01 20:54:44 +01:00
|
|
|
get_attr_s(AttrName, Attrs).
|
2002-11-24 21:36:57 +01:00
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
%%
|
|
|
|
-spec(get_subtag/2 ::
|
|
|
|
(
|
|
|
|
Xmlel :: xmlel(),
|
|
|
|
Name :: binary())
|
|
|
|
-> xmlel() | false
|
|
|
|
).
|
2003-01-02 22:01:12 +01:00
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
get_subtag(#xmlel{children = Els}, Name) ->
|
2003-01-02 22:01:12 +01:00
|
|
|
get_subtag1(Els, Name).
|
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
%%
|
|
|
|
-spec(get_subtag1/2 ::
|
|
|
|
(
|
|
|
|
Els :: [xmlel() | cdata()],
|
|
|
|
Name :: binary())
|
|
|
|
-> xmlel() | false
|
|
|
|
).
|
|
|
|
|
2003-01-02 22:01:12 +01:00
|
|
|
get_subtag1([El | Els], Name) ->
|
|
|
|
case El of
|
2012-09-11 15:45:59 +02:00
|
|
|
#xmlel{name = Name} -> El;
|
|
|
|
_ -> get_subtag1(Els, Name)
|
2003-01-02 22:01:12 +01:00
|
|
|
end;
|
2012-09-11 15:45:59 +02:00
|
|
|
get_subtag1([], _) -> false.
|
|
|
|
|
|
|
|
%%
|
|
|
|
-spec(get_subtag_cdata/2 ::
|
|
|
|
(
|
|
|
|
Tag :: xmlel(),
|
|
|
|
Name :: binary())
|
|
|
|
-> binary()
|
|
|
|
).
|
2003-01-02 22:01:12 +01:00
|
|
|
|
2007-06-05 03:50:28 +02:00
|
|
|
get_subtag_cdata(Tag, Name) ->
|
|
|
|
case get_subtag(Tag, Name) of
|
2012-09-11 15:45:59 +02:00
|
|
|
false -> <<"">>;
|
|
|
|
Subtag -> get_tag_cdata(Subtag)
|
2007-06-05 03:50:28 +02:00
|
|
|
end.
|
2003-01-02 22:01:12 +01:00
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
%%
|
|
|
|
-spec(append_subtags/2 ::
|
|
|
|
(
|
|
|
|
Xmlel :: xmlel(),
|
|
|
|
SubTags2 :: [xmlel() | cdata()])
|
|
|
|
-> Xmlel :: xmlel()
|
|
|
|
).
|
|
|
|
|
|
|
|
append_subtags(#xmlel{name = Name, attrs = Attrs, children = SubTags1}, SubTags2) ->
|
|
|
|
#xmlel{name = Name, attrs = Attrs, children = SubTags1 ++ SubTags2}.
|
|
|
|
|
|
|
|
%%
|
|
|
|
-spec(get_path_s/2 ::
|
|
|
|
(
|
|
|
|
El :: xmlel(),
|
|
|
|
Path :: [{elem, Name::binary()}
|
|
|
|
|{attr, Name::binary()}
|
|
|
|
|cdata])
|
|
|
|
-> xmlel()
|
|
|
|
| binary()
|
|
|
|
).
|
|
|
|
|
|
|
|
get_path_s(El, []) -> El;
|
2003-01-02 22:01:12 +01:00
|
|
|
get_path_s(El, [{elem, Name} | Path]) ->
|
|
|
|
case get_subtag(El, Name) of
|
2012-09-11 15:45:59 +02:00
|
|
|
false -> <<"">>;
|
|
|
|
SubEl -> get_path_s(SubEl, Path)
|
2003-01-02 22:01:12 +01:00
|
|
|
end;
|
|
|
|
get_path_s(El, [{attr, Name}]) ->
|
|
|
|
get_tag_attr_s(Name, El);
|
2012-09-11 15:45:59 +02:00
|
|
|
get_path_s(El, [cdata]) -> get_tag_cdata(El).
|
|
|
|
|
|
|
|
%%
|
|
|
|
-spec(replace_tag_attr/3 ::
|
|
|
|
(
|
|
|
|
Name :: binary(),
|
|
|
|
Value :: binary(),
|
|
|
|
Xmlel :: xmlel())
|
|
|
|
-> Xmlel :: #xmlel{
|
|
|
|
name :: binary(),
|
|
|
|
attrs :: [attr(),...],
|
|
|
|
children :: [xmlel() | cdata()]
|
|
|
|
}
|
|
|
|
).
|
|
|
|
|
|
|
|
replace_tag_attr(Name, Value, Xmlel) ->
|
|
|
|
Xmlel#xmlel{
|
|
|
|
attrs = [{Name, Value} | lists:keydelete(Name, 1, Xmlel#xmlel.attrs)]
|
|
|
|
}.
|
|
|
|
|
|
|
|
-spec to_xmlel(xmlelement() | xmlel()) -> xmlel().
|
|
|
|
|
|
|
|
to_xmlel({_, Name, Attrs, Els}) ->
|
|
|
|
#xmlel{name = iolist_to_binary(Name),
|
|
|
|
attrs = [{iolist_to_binary(K), iolist_to_binary(V)}
|
|
|
|
|| {K, V} <- Attrs],
|
|
|
|
children = [to_xmlel(El) || El <- Els]};
|
|
|
|
to_xmlel({xmlcdata, CData}) ->
|
|
|
|
{xmlcdata, iolist_to_binary(CData)}.
|