[erlang-questions] Which is best? string:concat or ++?

Richard Carlsson <>
Tue May 8 09:44:20 CEST 2012


On 05/08/2012 12:25 AM, Richard O'Keefe wrote:
> One way to represent SGML data, including HTML, in Erlang
> looks like
> 	{Element_Name, [{Attribute_Name,Value}...], [Child...]}
> or	<<text as a binary>>
> or	"text as a list"
>
> in which while generating the tree you never ever have to worry
> about escaping any data.  You write a function that walks over
> a tree like this, perhaps sending it to a port, or perhaps
> creating an IO list, and *that* function does whatever escaping
> is necessary.
>
> 	{p,[],["This<is>  safe&so; is<this>!"]}
>
> is perfectly safe, as long as your output function knows what it
> is doing.  Also, if you know you are generating HTML, you could
> have your output function take care of omitting end tags for
> empty-by-definition elements.
>
> You wouldn't believe how easy it is to manipulate SGML data this
> way until you've tried it.
>
> (I _could_ have pointed to xmerl, but that's rather more complicated.)

What you describe is what is called "simple form" in xmerl:

http://www.erlang.org/doc/man/xmerl.html#export_simple-3
http://www.erlang.org/doc/apps/xmerl/xmerl_ug.html#id56386

Using the xmerl_html callback module:

1> io:put_chars(xmerl:export_simple([{p,[],["This<is>  safe&so; 
is<this>!"]}], xmerl_html)).
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<p>This<is>  safe&so; is<this>!</p>

and the xmerl_xml callback module:

2> io:put_chars(xmerl:export_simple([{p,[],["This<is>  safe&so; 
is<this>!"]}], xmerl_xml)).
<?xml version="1.0"?><p>This<is>  safe&so; is<this>!</p>


     /Richard Carlsson



More information about the erlang-questions mailing list