Section numbering using xmerl:export

Mikael Karlsson <>
Fri Sep 13 22:00:07 CEST 2002


fredag 30 augusti 2002 11:18 skrev Ulf Wiger:
> Yes, given the choice, one would probably use Erlang directly
> instead of XSLT. It would still be nice to be able to use an
> existing XSLT framework, if someone else has already done the
> hard work, but there are fortunately tools to do that already,
> available to the Erlang programmer. I don't know how well they
> fit with xmerl, though. Perhaps someone else knows?
>
> /Uffe (who has about a thousand things he would rather do than
> add XSLT support to XMErl)

Hi Ulf,

I was able to implement some kind of XSLT lookalike in Erlang 
using xmerl_xpath, and working on xmlElements similar to Vlads
traverse/2 function. At least I can select were I want to have the 
new transformed elements. 
For some reason I have to reverse the list returned by 
xmerl_xpath:string/2. 
I also wonder how performance is affected, considering that you 
have to parse the strings passed to xmerl_path? 

/Mikael

process_xml(E)->
    lists:flatten(template(E)).

%% article is the root element
template(E0 = #xmlElement{name=article})->

    E1 = changetitle(E0), %% Yes, I have to add section numbering to titles in
                                           %% separate pass.

    [ "<\?xml version=\"1.0\" encoding=\"iso-8859-1\"\?>"
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\
 \"http://www.w3.org/TR/xhtml1/DTDxhtml1-transitional.dtd \">"
     "<html xmlns=\"http://www.w3.org/1999/xhtml\" >"
      "<head>"
      "<title>", 
      value_of(select("articleinfo/title",E1)),
      "</title>" 
      "<style type=\"text/css\"> body {margin-left:10%; margin-right:5%;} \
.logo{float:right} </style>" 
      "</head>"
     "<body>",

     template(xmerl_xpath:string("articleinfo",E1)),

     process_toc(E1), %% Insert toc between info and main part of article

     template(lists:reverse(xmerl_xpath:string("section",E1))),

     "</body></html>"];

%% All callback functions used by xmerl:export can be
%% reused for straight transformations
template(E = #xmlElement{name=Name,content=C})->
    apply(sdocbook2xhtml,Name,
	  [template(C), E#xmlElement.attributes,
	   E#xmlElement.parents,E]);

template(EList) when list(EList) ->
    lists:map(fun(E) -> template(E) end, EList);
template(E = #xmlText{}) ->
    E#xmlText.value;
template(E)->
    io:fwrite("Got unexpected element ~p~n",[E]),
    [].

value_of(E = #xmlElement{content=[#xmlText{}=T1|_]})->
    T1#xmlText.value;
value_of(E) ->[].

select(Str,E)->
    [H|_] = xmerl_xpath:string(Str,E),
    H.




More information about the erlang-questions mailing list