elemX

Create an XML element.

elemX builds the element with attributes and child content given as arguments. Their kind is distinguished by their type — child element should be passed as Element, attributes as Attribute and text content as string.

* You can pass the result of elemX to another elemX call to create child nodes: elemX!"parent"(elemX!"child"()). * Use attr to specify attributes: elemX!"element"(attr("key") = "value"). * Pass a string to specify text content: elemX!"text"("Hello").

elemX also provides the option to specify attributes from an associative array or source: elemX!"element"(["key": "value"]) or elemX!("element", key="value") but these are considered legacy and should be avoided in new code.

  1. Element elemX(Ts content)
    Element
    elemX
    (
    string name
    string[string] attributes
    Ts...
    )
    ()
  2. Element elemX(string[string] attributes, T content)
  3. Element elemX(T content)

Parameters

name

Name of the element.

attributes

Optional, legacy; attributes for the element as an associative array mapping attribute names to their values.

content Ts

Attributes (Attribute), children elements (Element) and text of the element, as a string.

Return Value

Type: Element

An Element type, implicitly castable to string. Instances of Element can be safely placed

Examples

enum xml = elemX!"xml"(
    elemX!"heading"("This is my sample document!"),
    elemX!("spacing /", q{ height="1em" }),
    elemX!"spacing /"(["height": "1em"]),
    elemX!"empty",
    elemX!"br",
    elemX!"container"
        .addX!"paragraph"("Foo")
        .addX!"paragraph"("Bar"),
);

assert(xml == "<xml>" ~ (
        "<heading>This is my sample document!</heading>"
        ~ `<spacing height="1em"/>`
        ~ `<spacing height="1em"/>`
        ~ `<empty></empty>`
        ~ `<br></br>`
        ~ "<container>" ~ (
            "<paragraph>Foo</paragraph>"
            ~ "<paragraph>Bar</paragraph>"
        ) ~ "</container>"
    ) ~ "</xml>");

Meta