xmerl_xs(3)
π xmerl_xs π
okay bestie, welcome to the xmerl_xs page from the Erlang Module Definition manual β this is the one about XSLT-like XML document transformations. grab a strawberry drink π and let's go.
β¨ NAME β¨
xmerl_xs β XSLT-like XML document transformations. periodt.
π DESCRIPTION π
okay so. Erlang and XSLT are, like, lowkey cousins π
β both of them are doing the functional-programming thing. and because of that, once you've got xmerl_xpath in your toolbox, you can literally write XSLT-style transforms in Erlang. no cap.
XSLT stylesheets are the classic move when you're transforming XML documents into other XML documents, or into (X)HTML for showing off to people. the catch? XSLT has a lot of functions and learning all of them is genuinely a whole situation π. and heads up β this doc assumes you already have a basic level of understanding of XSLT. not the vibe to start from zero, bestie.
but here's the tea π: since XSLT is built on functional programming with pattern matching and recursion, you can write basically the same style sheets in Erlang. at least for the basic transforms! this document walks you through using the XPath implementation together with Erlang's pattern matching plus a couple of functions, so you can write XSLT-like transforms.
this approach is probably easier for an Erlanger π«Ά β but if you need real XSLT stylesheets because you have to "comply to the standard", there's an adapter available to the Sablotron XSLT package, which is written in C++. See also the XSLT-like Transformations tutorial. iykyk.
π FUNCTIONS π
πΈ built_in_rules(Fun, E :: xmerl:element()) -> io_lib:chars()
built_in_rules(Fun, E :: xmerl:element()) -> io_lib:chars()
when
Fun :: fun((xmerl:element()) -> io_lib:chars()).
the default fallback behaviour, fr fr. your template funs should end with:
template(E) -> built_in_rules(fun template/1, E).
like, that's the closer. every time. π€
π¦ select(String, E) -> Result
select(String, E) -> Result
when
String :: term(),
E :: xmerl:element(),
Result ::
[xmerl:xmlElement() |
xmerl:xmlAttribute() |
xmerl:xmlText() |
xmerl:xmlPI() |
xmerl:xmlComment() |
xmerl:xmlNsNode() |
xmerl:xmlDocument()] |
Scalar,
Scalar :: #xmlObj{}.
extract the nodes from the xml tree according to XPath. that's the whole job, iconic behaviour. β¨
equivalent to xmerl_xpath:string(Str, E).
see also: value_of/1.
π§ value_of(E) -> io_lib:chars()
value_of(E) -> io_lib:chars()
when E :: xmerl:element() | [xmerl:element()].
concatenate all text nodes within the tree. all of them. squished together. π
example β this:
<xsl:template match="title">
<div align="center">
<h1><xsl:value-of select="." /></h1>
</div>
</xsl:template>
becomes:
template(E = #xmlElement{name='title'}) ->
["<div align="center"><h1>",
value_of(select(".", E)), "</h1></div>"]
ate. no notes. π½οΈ
π xslapply(Fun, ElementList) -> io_lib:chars()
xslapply(Fun, ElementList) -> io_lib:chars()
when
Fun :: fun((xmerl:element()) -> io_lib:chars()),
ElementList :: [xmerl:element()] | xmerl:element().
lookalike to xsl:apply-templates. literally a wrapper whose entire purpose is to make things look similar to xsl:apply-templates β main character energy, honestly. π
example, the original XSLT:
<xsl:template match="doc/title">
<h1>
<xsl:apply-templates/>
</h1>
</xsl:template>
becomes in Erlang:
template(E = #xmlElement{ parents=[{'doc',_}|_], name='title'}) ->
["<h1>",
xslapply(fun template/1, E),
"</h1>"];
π bestie reminder: the vibes are immaculate but the syntax is not β copy those examples character for character, okay? π
that's the page!! brought to you by Ericsson AB, sitting pretty at xmerl 2.2, filed under xmerl_xs(3). πβ¨ go write some transforms, you've got this π«Άπ
π¦ where this page comes from
- ubuntu stonking: erlang-doc 1:29.0.6+dfsg-1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms