1-module(xserl_test).
2-include("xmerl.hrl").
3-import(xserl,[ xslapply/2, value_of/1, select/2, built_in_rules/2 ]).
4-export([process_xml/1,test/0]).
5
6doctype()->
7    "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\
8 \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd \">".
9
10test() ->
11    Str=    "<?xml version=\"1.0\"?>"
12	"<doc xmlns='urn:loc.gov:books' xmlns:isbn='urn:ISBN:0-395-36341-6'>"
13	"<title>Cheaper by the Dozen</title>"
14	"<isbn:number>1568491379</isbn:number>"
15	"<note>"
16	"<!-- make HTML the default namespace for some commentary -->"
17	"<p xmlns='urn:w3-org-ns:HTML'>"
18	"This is a <i>funny</i> book!"
19	"</p>"
20	"</note>"
21	"</doc>",
22    {Doc,_}=xmerl_scan:string(Str,[{fetch_fun, fun(DTDSpec,S) -> {ok,S} end}]),
23
24    process_xml(Doc).
25
26process_xml(Doc)->
27	template(Doc).
28
29template(E = #xmlElement{name='doc'})->
30    [ "<\?xml version=\"1.0\" encoding=\"iso-8859-1\"\?>",
31      doctype(),
32      "<html xmlns=\"http://www.w3.org/1999/xhtml\" >"
33      "<head>"
34      "<title>", xserl:value_of(select("title",E)), "</title>"
35      "</head>"
36      "<body>",
37      xslapply( fun template/1, E),
38      "</body>"
39      "</html>" ];
40
41
42template(E = #xmlElement{ parents=[{'doc',_}|_], name='title'}) ->
43    ["<h1>",
44%%     xslapply( fun template/1, E),
45%%   same as
46     lists:map( fun template/1, E#xmlElement.content ),
47     "</h1>"];
48
49template(E = #xmlElement{ parents=[{'chapter',_}|_], name='title'}) ->
50    ["<h2>",
51     xslapply( fun template/1, E),
52     "</h2>"];
53
54template(E = #xmlElement{ parents=[{'section',_}|_], name='title'}) ->
55    ["<h3>",
56     xslapply( fun template/1, E),
57     "</h3>"];
58
59template(E = #xmlElement{ name='para'}) ->
60    ["<p>",
61     xslapply( fun template/1, E),
62     "</p>"];
63
64template(E = #xmlElement{ name='note'}) ->
65    ["<p class=\"note\">"
66     "<b>NOTE: </b>",
67     xslapply( fun template/1, E),
68     "</p>"];
69
70template(E = #xmlElement{ name='emph'}) ->
71    ["<em>",
72     xslapply( fun template/1, E),
73     "</em>"];
74
75template(E)->
76    built_in_rules( fun template/1, E).
77
78%% It is important to end with a call to xserl:built_in_rules/2
79%% if you want any text to be written in "push" transforms.
80%% That are the ones using a lot xslapply( fun template/1, E )
81%% instead of value_of(select("xpath",E)), which is pull...
82%% Could maybe be caught as an exception in xslapply instead,
83%% but I think that could degrade performance - having an
84%% exception for every #xmlText element.
85
86