# `RDF.XML.Encoder`
[🔗](https://github.com/rdf-elixir/rdf-xml-ex/blob/v1.2.1/lib/rdf/xml/encoder.ex#L1)

An encoder for RDF/XML serializations of RDF.ex data structures.

As for all encoders of `RDF.Serialization.Format`s, you normally won't use these
functions directly, but via one of the `write_` functions on the `RDF.XML` format
module or the generic `RDF.Serialization` module.

## Options

- `:base`: : Allows to specify the base URI to be used for a `xml:base` declaration.
  If not specified the one from the given graph is used or if there is also none
  specified for the graph the `RDF.default_base_iri/0`.
- `:prefixes`: Allows to specify the prefixes to be used as a `RDF.PrefixMap` or
  anything from which a `RDF.PrefixMap` can be created with `RDF.PrefixMap.new/1`.
  If not specified the ones from the given graph are used or if these are also not
  present the `RDF.default_prefixes/0`.
- `:implicit_base`: Allows to specify that the used base URI should not be encoded
  in the generated serialization (default: `false`).
- `:use_rdf_id`: Allows to determine if `rdf:ID` should be used when possible.
   You can either provide a boolean value or a function which should return a boolean
   value for a given `RDF.Description`. (default: `false`)
- `:xml_declaration`: Allows to specify the XML declaration. Possible values:
  - `true` (default): produces the default `<?xml version="1.0" encoding="utf-8"?>` declaration
  - `false`: omits the XML declaration
  - any value supported for `prolog` argument of `Saxy.encode!/2`
    (only available on `encode/2`, not on `stream/2`)
- `:producer`: This option allows you to provide a producer function, which will get
  the input data (usually a `RDF.Graph`) and should produce a stream of the descriptions
  to be encoded. This allows you to control the order of the descriptions, apply filters
  etc.

        iex> RDF.Graph.new([
        ...>   EX.S1 |> EX.p1(EX.O1),
        ...>   EX.S2 |> EX.p2(EX.O2),
        ...> ])
        ...> |> RDF.XML.write_string!(
        ...>     prefixes: [ex: EX],
        ...>     producer: fn graph ->
        ...>       {first, rest} = RDF.Graph.pop(graph, EX.S2)
        ...>       Stream.concat([first], RDF.Graph.descriptions(rest))
        ...>     end)
        ~S(<?xml version="1.0" encoding="utf-8"?><rdf:RDF xmlns:ex="http://example.com/"><rdf:Description rdf:about="http://example.com/S2"><ex:p2 rdf:resource="http://example.com/O2"/></rdf:Description><rdf:Description rdf:about="http://example.com/S1"><ex:p1 rdf:resource="http://example.com/O1"/></rdf:Description></rdf:RDF>)

# `encode`

```elixir
@spec encode(
  RDF.Graph.t(),
  keyword()
) :: {:ok, String.t()} | {:error, any()}
```

Encodes the given RDF `data` structure to an RDF/XML string.

The result is returned in an `:ok` tuple or an `:error` tuple in case of an error.

For a description of the available options see the [module documentation](`RDF.XML.Encoder`).

# `stream`

```elixir
@spec stream(
  RDF.Graph.t(),
  keyword()
) :: Enumerable.t()
```

Encodes the given RDF `data` structure to an RDF/XML stream.

By default, the RDF/XML stream will emit single line strings for each of the
descriptions in the given `data`. But you can also receive the serialized RDF/XML
description as IO lists aka iodata by setting the `:mode` option to `:iodata`.

For a description of the other available options see the [module documentation](`RDF.XML.Encoder`).

---

*Consult [api-reference.md](api-reference.md) for complete listing*
