XMLWriter.writePI

Writes a parsing instruction to the output range.

  1. void writePI(R name, Newline newline)
    struct XMLWriter(OR)
    void
    writePI
    (
    R
    )
    if (
    isForwardRange!R &&
    isSomeChar!(ElementType!R)
    )
    if (
    isOutputRange!(OR, char)
    )
  2. void writePI(R1 name, R2 text, Newline newline, InsertIndent insertIndent)
  3. void writePI(R1 name, R2 text, InsertIndent insertIndent, Newline newline)

Parameters

name R

The name of the parsing instruction.

newline Newline

Whether a _newline followed by an indent will be written to the output range before the processing instruction.

Throws

XMLWritingException if the given _name or _text is not legal in an XML processing instruction.

Examples

1 import std.array : appender;
2 import std.exception : assertThrown;
3 
4 auto writer = xmlWriter(appender!string());
5 
6 writer.writePI("pi", Newline.no);
7 writer.writeStartTag("root");
8 writer.writePI("Poirot", "has a cane");
9 writer.writePI("Sherlock");
10 writer.writePI("No", "preceding newline", Newline.no);
11 writer.writePI("Ditto", Newline.no);
12 writer.writePI("target", "some data\nwith a newline");
13 writer.writePI("name", "Another newline\nbut no indent",
14                InsertIndent.no);
15 writer.writeStartTag("tag");
16 writer.writePI("Deep", "Thought");
17 writer.writeEndTag("tag");
18 writer.writeEndTag("root");
19 
20 assert(writer.output.data ==
21        "<?pi?>\n" ~
22        "<root>\n" ~
23        "    <?Poirot has a cane?>\n" ~
24        "    <?Sherlock?><?No preceding newline?><?Ditto?>\n" ~
25        "    <?target some data\n" ~
26        "        with a newline?>\n" ~
27        "    <?name Another newline\n" ~
28        "but no indent?>\n" ~
29        "    <tag>\n" ~
30        "        <?Deep Thought?>\n" ~
31        "    </tag>\n" ~
32        "</root>");
33 
34 // The name xml (no matter the casing) is illegal as a name for
35 // processing instructions (so that it can't be confused for the
36 // optional <?xml...> declaration at the top of an XML document).
37 assertThrown!XMLWritingException(writer.writePI("xml", "bar"));
38 
39 // ! is not legal in a processing instruction's name.
40 assertThrown!XMLWritingException(writer.writePI("!", "bar"));
41 
42 // ?> is not legal in a processing instruction.
43 assertThrown!XMLWritingException(writer.writePI("foo", "?>"));
44 
45 // Unchanged after an XMLWritingException is thrown.
46 assert(writer.output.data ==
47        "<?pi?>\n" ~
48        "<root>\n" ~
49        "    <?Poirot has a cane?>\n" ~
50        "    <?Sherlock?><?No preceding newline?><?Ditto?>\n" ~
51        "    <?target some data\n" ~
52        "        with a newline?>\n" ~
53        "    <?name Another newline\n" ~
54        "but no indent?>\n" ~
55        "    <tag>\n" ~
56        "        <?Deep Thought?>\n" ~
57        "    </tag>\n" ~
58        "</root>");

See Also

Meta