Reading and preserving tagged XML
What an application needs to read IDML's tagged-XML layer, and what it must keep intact when it edits and saves a document without reading it.
An application can ignore the tagged-XML layer for layout, but one that saves a document has to carry it through unchanged.
In short Reading the layer means following the design map's idPkg:Tags and
idPkg:BackingStory references, reading the XMLTag list, and walking the XmlStory
for XMLElement nodes and the placed content they point at. Most documents need little
of this: an untagged InDesign document has one Root element and nothing else.
Preserving the layer is simpler and matters more: copy the XML/ parts as they are,
keep the design map's references to them, and keep the ids of the stories and frames
the tree points at.
Reading the layer
The layer has no effect on where anything lands on a page, so a renderer or a text extractor can skip it. An application that wants the structure, for example to export content by tag, reads it in four steps:
- Find the parts. The design map names
XML/Tags.xmlinidPkg:TagsandXML/BackingStory.xmlinidPkg:BackingStory. It does not referenceXML/Mapping.xml, which InDesign does not write. - Read the tags. Each
XMLTaginTags.xmlis aSelfand aName. - Walk the tree. The
XmlStoryin the backing story is shaped like a story;XMLElementnodes sit among its character ranges and nest inside each other. Each names its tag inMarkupTag. - Resolve placed content. An
XMLElementthat labels content already on a page names it bySelfinXMLContent; the text itself stays in the layout story.
If the backing story holds only the Root element, the document is untagged and there
is nothing more to read. That is the case for almost every document in the census.
Preserving the layer
An application that opens a document, changes something and saves it is responsible for everything it did not change, including a layer it never read:
- Copy the
XML/parts unchanged. Regenerating them from nothing would drop the author's structure; the zero-widthU+FEFFcharacters in the backing story are part of it. - Keep the design map references
idPkg:TagsandidPkg:BackingStory. - Keep the ids the tree points at. An
XMLElementrefers to placed content by itsSelf, so renaming a story or a frame on save breaks that link.
"Not read" and "not kept" are different things. A tool can leave the layer alone and still return it intact.
In Paged: no part of the XML layer is read: there is no reader for
XMLTag,XmlStoryorXMLElement, and the design map'sidPkg:BackingStoryreference is not followed. The XML parts are kept as raw bytes and copied byte for byte on save, like every part Paged does not model; an unedited round trip of the corpus reproduces all 99 tested packages exactly. The.pagedcontainer keeps the same parts. Planned
Frequently asked questions
Does a renderer need the tagged-XML layer? No. The layout comes entirely from the spreads, stories and styles; the XML layer labels that content without changing it.
How do I tell whether a document is tagged?
Look at XML/BackingStory.xml. An untagged document's XmlStory holds a single
XMLElement with MarkupTag="XMLTag/Root"; a tagged one has more elements.
What must a tool keep when it saves an IDML file?
The XML/ parts as they were, the design map's idPkg:Tags and idPkg:BackingStory
references, and the Self ids of stories and frames that the XML tree refers to.
Does Paged keep the XML layer when it saves? Yes. It does not read the layer, but it copies the XML parts byte for byte on save.
The structured-content layer
The element vocabulary of IDML's tagged-XML layer — XMLTag in Tags.xml, the XmlStory and its XMLElement tree in BackingStory.xml, and the rarer nodes and maps.
Cookbook
Task recipes for working with IDML files — extract text, generate a package, fill a template, read the version, build tables, gradients and images.