-module(xserl_test). -include("xmerl.hrl"). -import(xserl,[ xslapply/2, value_of/1, select/2, built_in_rules/2 ]). -export([process_xml/1,test/0]). doctype()-> "". test() -> Str= "" "" "Cheaper by the Dozen" "1568491379" "" "" "

" "This is a funny book!" "

" "
" "
", {Doc,_}=xmerl_scan:string(Str,[{fetch_fun, fun(DTDSpec,S) -> {ok,S} end}]), process_xml(Doc). process_xml(Doc)-> template(Doc). template(E = #xmlElement{name='doc'})-> [ "<\?xml version=\"1.0\" encoding=\"iso-8859-1\"\?>", doctype(), "" "" "", xserl:value_of(select("title",E)), "" "" "", xslapply( fun template/1, E), "" "" ]; template(E = #xmlElement{ parents=[{'doc',_}|_], name='title'}) -> ["

", %% xslapply( fun template/1, E), %% same as lists:map( fun template/1, E#xmlElement.content ), "

"]; template(E = #xmlElement{ parents=[{'chapter',_}|_], name='title'}) -> ["

", xslapply( fun template/1, E), "

"]; template(E = #xmlElement{ parents=[{'section',_}|_], name='title'}) -> ["

", xslapply( fun template/1, E), "

"]; template(E = #xmlElement{ name='para'}) -> ["

", xslapply( fun template/1, E), "

"]; template(E = #xmlElement{ name='note'}) -> ["

" "NOTE: ", xslapply( fun template/1, E), "

"]; template(E = #xmlElement{ name='emph'}) -> ["", xslapply( fun template/1, E), ""]; template(E)-> built_in_rules( fun template/1, E). %% It is important to end with a call to xserl:built_in_rules/2 %% if you want any text to be written in "push" transforms. %% That are the ones using a lot xslapply( fun template/1, E ) %% instead of value_of(select("xpath",E)), which is pull... %% Could maybe be caught as an exception in xslapply instead, %% but I think that could degrade performance - having an %% exception for every #xmlText element.