# XSLT-like transformations
### Examples
---
#### Example 1 Using xslapply
original XSLT:
becomes in Erlang:
template(E = #xmlElement{ parents=[{'doc',_}|_], name='title'}) ->
["
",
xslapply(fun template/1, E),
"
"];
---
---
#### Example 2 Using value_of and select
becomes:
template(E = #xmlElement{name='title'}) ->
["", value_of(select(".", E)), "
"];
---
---
#### Example 3 Simple xsl stylesheet
A complete example with the XSLT sheet in the xmerl distribution.
NOTE:
---
---
#### Example 4 Erlang version
Erlang transformation of previous example:
-include("xmerl.hrl").
-import(xmerl_xs,
[ xslapply/2, value_of/1, select/2, built_in_rules/2 ]).
doctype()->
"".
process_xml(Doc)->
template(Doc).
template(E = #xmlElement{name='doc'})->
[ "<\?xml version=\"1.0\" encoding=\"iso-8859-1\"\?>",
doctype(),
""
""
"", value_of(select("title",E)), ""
""
"",
xslapply( fun template/1, E),
""
"" ];
template(E = #xmlElement{ parents=[{'doc',_}|_], name='title'}) ->
["",
xslapply( fun template/1, E),
"
"];
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 `xmerl_xs: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...
---
The largest example is the stylesheet to transform this document from the
Simplified Docbook XML format to xhtml. The source file is sdocbook2xhtml.erl.
### Tips and tricks
#### for-each
The function for-each is quite common in XSLT stylesheets. It can often be
rewritten and replaced by select/1. Since select/1 returns a list of
#xmlElements and xslapply/2 traverses them it is more or less the same as to
loop over all the elements.
#### position()
The XSLT position() and #xmlElement.pos are not the same. One has to make an own
position in Erlang.
---
#### Example 5 Counting positions
Can be written as
template(E = #xmlElement{name='stanza'}) ->
{Lines,LineNo} = lists:mapfoldl(fun template_pos/2, 1, select("line", E)),
["", Lines, "
"].
template_pos(E = #xmlElement{name='line'}, P) ->
{[indent_line(P rem 2), value_of(E#xmlElement.content), "
"], P + 1 }.
indent_line(0)->" ";
indent_line(_)->"".
---
#### Global tree awareness
In XSLT you have "root" access to the top of the tree with XPath, even though
you are somewhere deep in your tree.
The xslapply/2 function only carries back the child part of the tree to the
template fun. But it is quite easy to write template funs that handles both the
child and top tree.
---
#### Example 6 Passing the root tree
The following example piece will prepend the article title to any section title
template(E = #xmlElement{name='title'}, ETop ) ->
["", value_of(select("title", ETop))," - ",
xslapply( fun(A) -> template(A, ETop) end, E),
"
"];
---