1xmerl_xs(3) Erlang Module Definition xmerl_xs(3)
2
3
4
6 xmerl_xs -
7 Erlang has similarities to XSLT since both languages
8 have a functional programming approach.
9
11 Erlang has similarities to XSLT since both languages have a functional
12 programming approach. Using xmerl_xpath it is possible to write XSLT
13 like transforms in Erlang.
14
15 XSLT stylesheets are often used when transforming XML documents, to
16 other XML documents or (X)HTML for presentation. XSLT contains quite
17 many functions and learning them all may take some effort. This docu‐
18 ment assumes a basic level of understanding of XSLT.
19
20 Since XSLT is based on a functional programming approach with pattern
21 matching and recursion it is possible to write similar style sheets in
22 Erlang. At least for basic transforms. This document describes how to
23 use the XPath implementation together with Erlangs pattern matching and
24 a couple of functions to write XSLT like transforms.
25
26 This approach is probably easier for an Erlanger but if you need to use
27 real XSLT stylesheets in order to "comply to the standard" there is an
28 adapter available to the Sablotron XSLT package which is written i C++.
29 See also the Tutorial.
30
32 built_in_rules(Fun, E) -> List
33
34 The default fallback behaviour. Template funs should end with:
35 template(E) -> built_in_rules(fun template/1, E).
36
37 select(String::string(), E) -> E
38
39 Extracts the nodes from the xml tree according to XPath.
40
41 See also: value_of/1.
42
43 value_of(E) -> List
44
45 Types:
46
47 E = term()
48
49 Concatenates all text nodes within the tree.
50
51 Example:
52
53
54 <xsl:template match="title">
55 <div align="center">
56 <h1><xsl:value-of select="." /></h1>
57 </div>
58 </xsl:template>
59
60
61 becomes:
62
63
64 template(E = #xmlElement{name='title'}) ->
65 ["<div align="center"><h1>",
66 value_of(select(".", E)), "</h1></div>"]
67
68
69 xslapply(Fun::Function, EList::list()) -> List
70
71 Types:
72
73 Function = () -> list()
74
75 xslapply is a wrapper to make things look similar to xsl:apply-
76 templates.
77
78 Example, original XSLT:
79
80
81 <xsl:template match="doc/title">
82 <h1>
83 <xsl:apply-templates/>
84 </h1>
85 </xsl:template>
86
87
88 becomes in Erlang:
89
90
91 template(E = #xmlElement{ parents=[{'doc',_}|_], name='title'}) ->
92 ["<h1>",
93 xslapply(fun template/1, E),
94 "</h1>"];
95
96
98 <>
99
100
101
102 xmerl 1.3.31.1 xmerl_xs(3)