91 lines
3.6 KiB
Markdown
91 lines
3.6 KiB
Markdown
# std::xml
|
|||
|
|
|
||
|
|
`std::xml` reads XML for Sakura, strictly, as XML 1.0 (Fifth Edition) writes it,
|
||
|
|
with its names in namespaces, as Namespaces in XML 1.0 writes them. Text must be
|
||
|
|
UTF-8, and is checked for everything XML asks of a document that is well formed:
|
||
|
|
markup, tags that match, an attribute given once, the characters XML allows,
|
||
|
|
and references to what is declared. `xml::parse` reads text into an
|
||
|
|
`xml::document`, which holds every element, attribute, text, comment, and
|
||
|
|
processing instruction of it, and `xml::reader` gives one token at a time.
|
||
|
|
|
||
|
|
## Using it
|
||
|
|
|
||
|
|
Require the standard library in your blossomfile, and name the module among a
|
||
|
|
target's `deps`, with the other modules of std it reads:
|
||
|
|
|
||
|
|
```yaml
|
||
|
|
requires:
|
||
|
|
- https://git.malunal.studio/sakura/std@v1.0.0
|
||
|
|
|
||
|
|
targets:
|
||
|
|
- name: app
|
||
|
|
type: program
|
||
|
|
deps: [std.xml, std.io]
|
||
|
|
```
|
||
|
|
|
||
|
|
```sakura
|
||
|
|
import std::io;
|
||
|
|
import std::xml;
|
||
|
|
|
||
|
|
def main(): void {
|
||
|
|
val read := xml::parse("<list><item id='1'>one</item><item id='2'>two</item></list>") else (e) {
|
||
|
|
io::println($"That isn't XML: {e.describe()}");
|
||
|
|
return;
|
||
|
|
};
|
||
|
|
for item in read.root.elements() {
|
||
|
|
val id := item.attribute("id") else "?";
|
||
|
|
io::println($"{id}: {item.text()}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
A reader gives the same piece by piece, without building anything:
|
||
|
|
|
||
|
|
```sakura
|
||
|
|
var tokens := xml::reader::new(text);
|
||
|
|
while tokens.next() is some(read) {
|
||
|
|
if read.kind == xml::token_kind::start {
|
||
|
|
for &each in tokens.attributes { io::println($"{read.local}@{each.local} = {each.value}"); }
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if tokens.error is some(e) { io::println(e.describe()); }
|
||
|
|
```
|
||
|
|
|
||
|
|
## What it reads
|
||
|
|
|
||
|
|
- **Text** is UTF-8, after an optional byte order mark. Text whose declaration
|
||
|
|
names another encoding is read while it is ASCII, which it is then the same
|
||
|
|
as, and stops with `unsupported_encoding` at the first byte past. Every line
|
||
|
|
break is read as `\n`.
|
||
|
|
- **Attributes' values** are normalized as XML says: each whitespace character
|
||
|
|
is a space, and references are replaced, so a tab written `	` stays one.
|
||
|
|
- **Text** is decoded: references are replaced, and a CDATA section's text is
|
||
|
|
text like the rest, so the document holds text beside a CDATA section as one
|
||
|
|
node. Whitespace within the root element is text too.
|
||
|
|
- **Namespaces**: each element and attribute has its name as written, its
|
||
|
|
prefix, its local name, and the namespace it is in, and attributes are found
|
||
|
|
by either. `xml::options { namespaces = false }` reads names only as written,
|
||
|
|
for XML that breaks Namespaces in XML.
|
||
|
|
- **The document type** is read: its internal subset's entities are expanded
|
||
|
|
where they are referred to, markup and all, and its attributes' defaults are
|
||
|
|
given to the elements that leave them out, with the spaces of those of types
|
||
|
|
other than CDATA collapsed. Nothing outside the text is read, so a reference
|
||
|
|
to an entity declared only there fails with `undeclared_entity`, and one to an
|
||
|
|
external entity with `external_entity`. Following the specification, after a
|
||
|
|
parameter entity it doesn't read, the declarations that follow are left out,
|
||
|
|
unless the document is standalone.
|
||
|
|
- **Limits**: `xml::options` says how deep elements and entities may nest, 512
|
||
|
|
by default, and how much text the entities a document refers to may stand for
|
||
|
|
in all, 16 MiB by default, so text written to exhaust a reader, as entities
|
||
|
|
that each refer to the last several times, stops with `too_much_expansion`.
|
||
|
|
|
||
|
|
An error says what went wrong and where, by its line and column, or where the
|
||
|
|
document refers to the entity it is within.
|
||
|
|
|
||
|
|
## Testing
|
||
|
|
|
||
|
|
From the standard library's root, `blossom test --only std.xml` runs them, or:
|
||
|
|
|
||
|
|
```sh
|
||
|
|
sakurac run --lib std=. tests/main.sakura tests/xml/*.sakura
|
||
|
|
```
|