From 1a5796cd12061ebb21e7e51a0b7bdf05ed4786a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= Date: Thu, 2 Sep 2010 16:56:23 +0200 Subject: xmerl: Add doc/examples directory Needed by the test suite. --- lib/xmerl/doc/examples/xserl_test.erl | 85 +++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 lib/xmerl/doc/examples/xserl_test.erl (limited to 'lib/xmerl/doc/examples/xserl_test.erl') diff --git a/lib/xmerl/doc/examples/xserl_test.erl b/lib/xmerl/doc/examples/xserl_test.erl new file mode 100644 index 0000000000..69db75cfe8 --- /dev/null +++ b/lib/xmerl/doc/examples/xserl_test.erl @@ -0,0 +1,85 @@ +-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. + -- cgit v1.2.3