User's Guide
Reference Manual
Release Notes
PDF
Top

xmerl
Reference Manual
Version 1.3.9

Expand All
Contract All

Table of Contents

- xmerl_scan
  - Top of manual page
  - accumulate_whitespace/4
  - cont_state/1
  - cont_state/2
  - event_state/1
  - event_state/2
  - fetch_state/1
  - fetch_state/2
  - file/1
  - file/2
  - hook_state/1
  - hook_state/2
  - rules_state/1
  - rules_state/2
  - string/1
  - string/2
  - user_state/1
  - user_state/2
- xmerl
  - Top of manual page
  - callbacks/1
  - export/2
  - export/3
  - export_content/2
  - export_element/2
  - export_element/3
  - export_simple/2
  - export_simple/3
  - export_simple_content/2
  - export_simple_element/2
- xmerl_xs
  - Top of manual page
  - built_in_rules/2
  - select/2
  - value_of/1
  - xslapply/2
- xmerl_eventp
  - Top of manual page
  - file_sax/4
  - stream/2
  - stream_sax/4
  - string_sax/4
- xmerl_xpath
  - Top of manual page
  - string/2
  - string/3
  - string/5
- xmerl_xsd
  - Top of manual page
  - file2state/1
  - format_error/1
  - process_schema/1
  - process_schema/2
  - process_schemas/1
  - process_schemas/2
  - process_validate/2
  - process_validate/3
  - state2file/1
  - state2file/2
  - validate/2
  - validate/3
- xmerl_sax_parser
  - Top of manual page
  - file/2
  - stream/2
  - ContinuationFun/1
  - EventFun/3

# xmerl_xs

### MODULE

xmerl_xs

### MODULE SUMMARY

Erlang has similarities to XSLT since both languages have a functional programming approach.

### DESCRIPTION

Erlang has similarities to XSLT since both languages have a functional programming approach. Using xmerl_xpath it is possible to write XSLT like transforms in Erlang.

XSLT stylesheets are often used when transforming XML documents, to other XML documents or (X)HTML for presentation. XSLT contains quite many functions and learning them all may take some effort. This document assumes a basic level of understanding of XSLT.

Since XSLT is based on a functional programming approach with pattern matching and recursion it is possible to write similar style sheets in Erlang. At least for basic transforms. This document describes how to use the XPath implementation together with Erlangs pattern matching and a couple of functions to write XSLT like transforms.

This approach is probably easier for an Erlanger but if you need to use real XSLT stylesheets in order to "comply to the standard" there is an adapter available to the Sablotron XSLT package which is written i C++. See also the Tutorial.

### EXPORTS

built_in_rules(Fun, E) -> List

The default fallback behaviour. Template funs should end with:
template(E) -> built_in_rules(fun template/1, E).

select(String::string(), E) -> E

Extracts the nodes from the xml tree according to XPath.

See also: value_of/1.

value_of(E) -> List

Types:

E = unknown()

Concatenates all text nodes within the tree.

Example:

      <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>"]

xslapply(Fun::Function, EList::list()) -> List

Types:

Function = () -> list()

xslapply is a wrapper to make things look similar to xsl:apply-templates.

Example, 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>"];