# `SefazNfe.XML`
[🔗](https://github.com/devaction-labs/sefaz_nfe/blob/v0.5.0/lib/sefaz_nfe/xml.ex#L1)

Reading side of the SEFAZ XML, on `:xmerl`.

## Safety

The body arrives over the network, so parsing is hardened before `:xmerl`
sees it. A DTD is rejected outright: SEFAZ never sends one, and accepting it
is what opens both XXE and entity expansion (billion laughs). `:xmerl` has no
budget for either, so refusing the construct is the defence rather than
limiting it. External fetches are stubbed as a second layer, and a malformed
body is an error rather than a crash — the network is allowed to be hostile.

## Encoding

The body is handed to `:xmerl` as raw bytes, not as a decoded charlist.
SEFAZ declares `encoding="utf-8"` and its messages are Portuguese — every
rejection carries accents — so `:xmerl` must do the decoding itself. Passing
already-decoded codepoints makes it decode twice and reject the document.

Scanning is namespace conformant, which is what populates the namespace axis
`SefazNfe.XML.C14N` needs to canonicalise a subtree for signing.

## Reading

`text/2` and `attribute/3` take a local element name and ignore namespace
prefixes. SEFAZ responses arrive prefixed differently per UF and per service,
and matching on the local name is what keeps one parser working across all of
them.

# `doc`

```elixir
@type doc() :: tuple()
```

# `all`

```elixir
@spec all(doc(), String.t()) :: [doc()]
```

Every element whose local name is `name`.

# `attribute`

```elixir
@spec attribute(doc(), String.t(), String.t()) :: String.t() | nil
```

Value of attribute `attr` on the first element named `name`.

# `attributes`

```elixir
@spec attributes(doc()) :: [{String.t(), String.t()}]
```

The attributes of `element` as `{name, value}`, in document order, without
namespace declarations (`xmlns`, `xmlns:p`).

# `children`

```elixir
@spec children(doc()) :: [doc()]
```

The element children of `element`, in document order (text is skipped).

# `element`

```elixir
@spec element(doc(), String.t()) :: doc() | nil
```

The first element whose local name is `name`, as an `:xmerl` record.

# `integer`

```elixir
@spec integer(doc(), String.t()) :: integer() | nil
```

Text of `name` as an integer, or `nil` when absent or not numeric.

# `own_attribute`

```elixir
@spec own_attribute(doc(), String.t()) :: String.t() | nil
```

Attribute `attr` on `element` itself, without searching its descendants.

# `own_text`

```elixir
@spec own_text(doc()) :: String.t() | nil
```

Text of `element` itself, without searching its descendants.

# `parse`

```elixir
@spec parse(binary()) :: {:ok, doc()} | {:error, {:xml, atom()}}
```

Parses `body` into an `:xmerl` document.

Returns `{:error, {:xml, :dtd_forbidden}}` for a body carrying a DTD and
`{:error, {:xml, :malformed}}` when `:xmerl` cannot read it.

# `qname`

```elixir
@spec qname(doc()) :: {String.t() | nil, String.t()}
```

The namespace URI (`nil` when there is none) and local name of `element`.

# `raw_text`

```elixir
@spec raw_text(doc()) :: String.t()
```

The text directly inside `element`, exactly as written: not trimmed, entities
resolved. Descendant elements are not read.

# `text`

```elixir
@spec text(doc(), String.t()) :: String.t() | nil
```

Text content of the first element whose local name is `name`.

---

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