#409·OfficeCLI

Any command that persists silently drops XML prolog content (comments / processing instructions) in docx, xlsx and pptx

Author: argszeroCreated Sep 20, 2026Updated Sep 22, 2026

Summary

Anything between the XML declaration and the root element of a part — processing instructions and comments — is silently dropped the first time that part is saved. A single ordinary edit is enough; nothing has to target the prolog.

Measured on 1.0.151 for all three formats:

Part Command prolog comment before after
word/document.xml set /body/p[1] --prop bold=true 1 0
xl/worksheets/sheet1.xml set /Sheet1/B1 --prop value=1 1 0
ppt/slides/slide1.xml add /slide[1] --type textbox --prop text=hi 1 0

Every command returns exit code 0 with its normal success message, validate reports Validation passed: no errors found., and nothing warns. The loss is only visible by unzipping the package before and after.

Environment

  • OfficeCLI 1.0.151, released binary, macOS arm64
  • Reproduced on files created by officecli create itself (no third-party producer involved)

Reproduction

Self-contained — needs only officecli and Python's stdlib. The only non-CLI step is placing the comment in the prolog, which is what any other producer (Word, LibreOffice, a mail gateway, a doc pipeline) may legitimately leave there:

bash
officecli create doc.docx
officecli add doc.docx /body --type paragraph --prop text=hello
officecli close doc.docx

# Put a comment in the prolog of word/document.xml, right after the XML
# declaration. This is a plain zip edit — nothing else in the part changes.
python3 - <<'PY'
import zipfile, os
p, m = "doc.docx", "word/document.xml"
MARKER = "<!-- KEEP-ME: review note from the document author -->"
with zipfile.ZipFile(p) as zin, zipfile.ZipFile(p + ".t", "w", zipfile.ZIP_DEFLATED) as zo:
    for it in zin.infolist():
        d = zin.read(it.filename)
        if it.filename == m:
            d = d.decode("utf-8").replace("?>", "?>" + MARKER, 1).encode("utf-8")
        zo.writestr(it, d)
os.replace(p + ".t", p)
PY

# Confirm it is there, then make one ordinary edit that does not touch it.
python3 -c "import zipfile;print(zipfile.ZipFile('doc.docx').read('word/document.xml').decode().count('KEEP-ME'))"
#   1
officecli set doc.docx /body/p[1] --prop bold=true
officecli close doc.docx
python3 -c "import zipfile;print(zipfile.ZipFile('doc.docx').read('word/document.xml').decode().count('KEEP-ME'))"
#   0        <- gone, with no warning and exit code 0

The same holds with a processing instruction instead of a comment, including the common <?xml-stylesheet ...?> form, and for xlsx and pptx — swap word/document.xml for the part of that format and use any ordinary edit:

Format Part to edit Ordinary edit used here
docx word/document.xml set d.docx /body/p[1] --prop bold=true
xlsx xl/worksheets/sheet1.xml set b.xlsx /Sheet1/B1 --prop value=1
pptx ppt/slides/slide1.xml add p.pptx /slide[1] --type textbox --prop text=two

For pptx, note create produces no slides — add one first (officecli add p.pptx / --type slide) or there is no slide1.xml to edit.

Read the part's text back to count a marker rather than comparing bytes: injected content is re-serialized (see the controls below), so byte comparison reports differences that are not losses.

Measured boundary: where the content sits, not what kind it is

The dividing line is prolog vs. inside the root element, not processing-instruction vs. comment:

Where the content is raw <file> /document shows it before the edit? Survives an edit?
Prolog processing instruction no no — dropped
Prolog <?xml-stylesheet ...?> no no — dropped
Prolog comment no no — dropped
Processing instruction inside <w:body> yes yes
Comment inside <w:body> yes yes

So an in-document comment or PI is modelled and kept; the prolog is not part of the model at all, and only the prolog is lost.

Trigger: it is the save, not the command

Sequence Prolog comment survives?
no command at all yes
raw <file> /document (read-only) yes
get <file> /body/p[1] (read-only) yes
close with no pending edits yes
set ... without a following close yes — still sitting in the resident
set + close no
add + close no
remove + close no

The last-but-two row is worth noting: because the edit lives in the resident's DOM until something persists it, the loss can surface later than the command that appeared to cause it, which makes it hard to attribute after the fact.

Controls: what still survives the same edit

This is not a general "re-serialization drops foreign content" complaint. On the same fixture and the same edit, all of the following are retained, each verified by counting the payload before and after:

Payload placed in the part Before → after
unknown-namespaced element, prefix declared at the root 1 → 1
mc:AlternateContent / mc:Fallback wrapper 2 → 2
unknown attribute on w:p, prefix declared at the root 1 → 1
unknown attribute on w:body 1 → 1
element placed after <w:sectPr> 1 → 1
xml:space="preserve" injected on w:t 1 → 1
pre-existing mc:Ignorable="w14 w15 wp14" on the root 1 → 1
processing instruction inside w:body 1 → 1
comment inside w:body 1 → 1

So the loss is specific to the prolog, not "anything the SDK doesn't model".

Diffing the tag stream of the part before and after the edit, the non-edit changes I see are:

  • the prolog content is deleted — this is the defect;
  • w14:textId on the edited paragraph is renumbered (00100001 → 00100002) — this also happens on a file with no prolog, so it is unrelated to this report (noting it only so the diff is not misread);
  • injected foreign elements are re-serialized: <w:p/> becomes <w:p /> and <foo:bar></foo:bar> becomes <foo:bar /> — cosmetic, content retained.

I did not observe the root element's namespace declarations being rewritten in these fixtures (the root tag stayed byte-identical). An earlier run of my own probe suggested otherwise, but that was my fixture's fault: I had used a foo: prefix in the body without declaring it at the root, which makes the part invalid and causes the serializer to hoist a declaration when it re-emits the root. With the prefix properly declared, the root is untouched. Flagging it because the invalid-fixture run silently produced a false "content preserved" result (nothing persisted at all) — malformed injected XML can make this defect look absent, which is worth knowing if you write a regression test for it.

Why it matters

  • It is unrecoverable and silent. There is no warning, no diagnostic, and no exit-code difference. validate passes; view issues reports only its usual content lint.
  • The read-only channel cannot disclose it. raw <file> /document returns the root element only, so a caller cannot inspect the prolog before deciding to edit — the first thing that reveals the pending loss is the loss itself. An agent that reads a part and then writes it has no way to detect that it deleted something.
  • Round-trip is an advertised capability. dump → batch is documented as the way to "learn from existing docs"; a part that loses content on save makes that channel lossy for any file that carried a prolog.

Relation to #267 / #338

This is the same failure class, on a different carrier, and I want to be explicit about that rather than file it as a new kind of bug. In #267 you described the mode as:

opening a deck editable indexed shape ids by loading every slide's DOM, so a later save re-serialized all slide parts — normalizing untouched slides (and dropping any XML the SDK doesn't model on them)

That instance — untouched parts — is fixed on main (76a5aa3b, 18252ac6). The instance here is the remaining half: the same loss, inside the part that was edited, so no amount of "don't materialize untouched parts" avoids it. The repro above uses a file created by officecli create and a single trivial edit, so it is not a third-party-producer or multi-slide-identity effect.

The repository also contains both directions of the intended behaviour, which is why I read this as an oversight rather than a deliberate normalization:

  • src/officecli/Core/WorksheetBloatFilter.cs:265-266 explicitly copies XmlNodeType.ProcessingInstruction through its raw reader/writer.
  • src/officecli/Core/RawXmlHelper.cs:1411 (StripXmlProlog) strips only the XML declaration, and the comment at :1425 states that a PI is "a legal processing instruction we must preserve".

The read side is careful about exactly this content; the write side drops it wholesale.

Source pointers

(read from source, not measured — this host has no dotnet SDK, so I could not build or run a patched binary to confirm the fix location)

  • src/officecli/Handlers/WordHandler.cs:191 — if (!DeferSave) _doc.MainDocumentPart?.Document?.Save(); The typed part save writes the root element, which is where the prolog stops being represented.
  • src/officecli/Core/RawXmlHelper.cs ReadPartXml returns part.RootElement.OuterXml when a typed root exists — the observable consequence (the declaration and prolog are absent from raw output) is measured; the code path is read.
  • The prolog is already stripped and re-added in places (src/officecli/Handlers/PowerPointHandler.cs:2270, RawXmlHelper.cs:1245), so a single place that re-attaches the original prolog on save may be enough; I have not verified that.

Because there is no SDK on this machine I am deliberately not attaching a patch. I would rather hand over a repro that is confirmed on the released binary than a diff I cannot compile.

Honest limits

  • I did not measure how common prolog content is in real-world OOXML parts, so I cannot tell you how often this bites in practice — only that when it does, the content is gone and nothing says so.
  • I verified docx, xlsx and pptx part bodies; I did not sweep every possible part (headers, footers, charts, styles, docProps/*) to confirm they all behave the same way, though the mechanism points that way.
  • The mechanism explanation above is labelled read-not-measured where it is.

Happy to re-verify against any build, or to test a candidate fix on a matrix of prolog-carrying parts if that would help.