→ Digital humanities


Maintained by: David J. Birnbaum (djbpitt@gmail.com) [Creative Commons BY-NC-SA 3.0 Unported License] Last modified: 2026-10-27T18:00:03+0000


Invisible XML (IXML) idioms

  1. Overview
  2. Simple productions
  3. Complex patterns
    1. Skeletal IXML grammar to convert JSON to XML
    2. Tagging named properties
    3. Tagging anonymous JSON items
    4. Tagging anonymous JSON arrays and strings
    5. Inserting an additional level of wrapping around a single input item
    6. Inserting an additional level of wrapping around a sequence of input intems
    7. Branching decisions
    8. Inserting elements, attributes, and text not present in the input
    9. Wrapping input and inserting an additional element or attribute into the wrapper

Overview

A basic Invisible XML (IXML) production (rule) consists of a rule name on the left-hand side (LHS), a separator (: or =), a pattern on the right-hand side (RHS), and an obligatory final dot (.). A production says that when parsing input, whatever matches the pattern on the RHS can be tagged as an XML node that has a name based on the LHS. For example:

capitalized: ["A"-"Z"], ["A"-"Z"; "a"-"z"]+.

will match any word that begins with any of the upper-case letters of the English alphabet, from A through Z, followed by any other letters of the English alphabet and tag it as an instance of <capitalized>, so that the input string Pluto would be tagged as:

<capitalized>Pluto</capitalized>

The goal of this tutorial is to introduce IXML idioms that involve more than simply matching and tagging individual patterns. Below, we first describe simple patterns, such as the match-and-tag example above, and then introduce more complicated ones, such as wrapping sequences of input items or inserting new XML nodes that do not match any input. Those who are new to IXML are likely to find the rest of this contribution easier to understand if they read some of the tutorial links at the Invisible XML portal before continuing below.

Some of the examples in this tutorial are reproduced from Haentjens Dekker, Ronald, David J. Birnbaum, and Bram Buitendijk, On beyond Invisible XML: parsing with implicit (and explicit) next-generation markup Presented at Balisage: The Markup Conference 2026, Washington, DC, August 3–7, 2026. In Proceedings of Balisage: The Markup Conference 2026. Balisage Series on Markup Technologies, vol. 31 (2026). https://doi.org/10.4242/BalisageVol31.HaentjensDekker01. For a brief tutorial about a common ambiguity issue see our Invisible XML and ambiguity.

Simple productions

Some productions straightforwardly match a pattern described on the RHS and wrap tags around it. For example, a production like:

digit: [N].

says that a digit in the input (which will be matched as a member of the Unicode character class N) will be tagged as a <digit> element in the XML output, so that a document that consists of only the single digit 1 will be tagged as <digit>1</digit>. Because an IXML grammar must match the entire input, this one-line grammar will fail if the input has more than one digit, and if we need to able to tag multiple digits individually we can do that with:

digits: digit+.
digit: [N].

With input like 11 the preceding grammar will emit:

<digits>
   <digit>1</digit>
   <digit>1</digit>
</digits>

If we want to tag the sequence of digits but not the individual digits in order to emit:

<digits>11</digits>

we can either match the sequence of digits but not the individual ones:

digits: [N]+.

or match the individual ones as well, but suppress their tags:

digits: digit+.
-digit: [N].

The RHS can be more complicated than in the examples above. For example, the production:

tel: [N], [N], [N], "-", [N], [N], [N], "-", [N], [N], [N], [N].

says that a sequence of three digits, a hyphen, three more digits, another hyphen, and then four digits will be tagged as a <tel>. Assuming input that reads 123-456-7890, the output looks like:

<tel>123-456-7890</tel>

Unicode character classes like [N] and string literals like "-" are called terminals, which means that they do not require additional productions to interpret them. A string literal matches … well … a literal string, and a Unicode character class matches any item that is a member of that class (in this case, the character class N matches any single digit).

A RHS can also contain non-terminals, that is, items that must be expanded in ways described in other productions. For example:

tel: area, hyphen, exch, hyphen, num.
area: [N], [N], [N].
exch: [N], [N], [N].
num: [N], [N], [N], [N].
hyphen: "-".

matches the same US telephone number pattern as above, but also tags, separately, the area code, the exchange (exchange code or central office code), the number (line number), and the hyphen separator. The output in this case is:

<tel>
   <area>123</area>
   <hyphen>-</hyphen>
   <exch>456</exch>
   <hyphen>-</hyphen>
   <num>7890</num>
</tel>

We can suppress the internal markup, as we did above, so that:

tel: area, hyphen, exch, hyphen, num.
-area: [N], [N], [N].
-exch: [N], [N], [N].
-num: [N], [N], [N], [N].
-hyphen: "-".

emits the same one-line output as the first example:

<tel>123-456-7890</tel>

We can also keep the internal markup but suppress the hyphens, so that:

tel: area, hyphen, exch, hyphen, num.
area: [N], [N], [N].
exch: [N], [N], [N].
num: [N], [N], [N], [N].
-hyphen: -"-".

emits:

<tel>
   <area>123</area>
   <exch>456</exch>
   <num>7890</num>
</tel>

Productions like the ones above are straightforward because they wrap tags (where the element name is based on the string value of the LHS) around the pattern matched by the RHS. The complex patterns discussed below go beyond simply matching some input and wrapping it in XML tags.

Complex patterns

Skeletal IXML grammar to convert JSON to XML

In several of the examples below we convert JSON input to XML using IXML, and this tutorial assumes a basic familiarity with JSON syntax, such as that provided at Lisa Tagliaferri’s Introduction to JSON. In Real Life we might prefer to perform this sort of JSON-to-XML transformation by starting from the standard XPath library functions fn:json-doc(), fn:parse-json(), or fn:json-to-xml() and using XSLT to transform the function result into our target XML markup. For the purpose of this IXML tutorial, though, we’ll let IXML parse the JSON input.

The following almost complete but skeletal IXML grammar will convert arbitrary JSON to XML. We use it as the starting point for many of our JSON-to-XML conversions in this tutorial, but, for reasons described below, it is unlikely to meet anyone’s needs without modification, and we’ll discuss those details below.

ixml version "1.1".
{ must define root first }
root: ws*, (obj; arr), ws*.

{ Part of skeletal structure, which we will customize to match input }
member: member-start, member-name, member-end.

{ From JSON spec, with attention to where whitespace is defined 
  See also rule for member, above }
key: ws*, string, ws*.
-members: member++prop-sep. 
-member-start: ws*, quote.
member-name: chars.
-member-end: quote, ws*, kv-sep, element.
-element: ws*, value, ws*.
-elements: element++prop-sep.
-value: obj; arr; string; number. { JSON also allows true, false, and null }
obj: obj-start, members, obj-end.
arr: arr-start, elements, arr-end.
-chars: ~['"\']*. { JSON also prohibits control characters }
string: quote, chars, quote.
number: [N]+.

{ JSON delimiters }
-obj-start: -"{".
-obj-end: -"}".
-arr-start: -"[".
-arr-end: -"]".

{ JSON separators }
-prop-sep: -comma.
-kv-sep: -colon.
-comma: -",".
-colon: -":".

{ JSON whitespace and quotes }
-space: -" ".
-eol: -#d?, -#a.
-ws: space; eol.
-quote: -'"'.

One limitation of IXML (noted in the What you can’t do section of Norm Tovey-Walsh’s Writing Invisible XML grammars) is that it is not possible to create markup (element or attribute names) based on matched input values. That is, the IXML grammar above cannot read JSON input like:

{ "hi" : "bye" }

and emit XML like:

<root><hi>bye</hi></root>

What the grammar above emits is:

<root>
   <obj>
      <member>
         <member-name>hi</member-name>
         <string>bye</string>
      </member>
   </obj>
</root>

In Real Life we could accept that limitation from our IXML process and create elements with the desired names in a subsequent XSLT transformation, but below we’ll explore how to work around the limitation using just IXML.

Tagging named properties

Tagging labeled properties

Imagine that our input looks like the following:

id: haentjensdekker2026
paper-title: On beyond Invisible XML: parsing with implicit (and explicit) next-generation markup
author: Haentjens Dekker, Ronald
author: Birnbaum, David J.
author: Buitendijk, Bram
collection-title: Proceedings of Balisage: The Markup Conference 2026
series-title: Balisage Series on Markup Technologies
volume: 31
publication-date: 2026
doi: 10.4242/BalisageVol31.HaentjensDekker010
url: https://doi.org/10.4242/BalisageVol31.HaentjensDekker0

and the output we want to produce is:

<record>
   <id>haentjensdekker2026</id>
   <paper-title>On beyond Invisible XML: parsing with implicit (and explicit) next-generation markup</paper-title>
   <author>Haentjens Dekker, Ronald</author>
   <author>Birnbaum, David J.</author>
   <author>Buitendijk, Bram</author>
   <collection-title>Proceedings of Balisage: The Markup Conference 2026</collection-title>
   <series-title>Balisage Series on Markup Technologies</series-title>
   <volume>31</volume>
   <publication-date>2026</publication-date>
   <doi>10.4242/BalisageVol31.HaentjensDekker010</doi>
   <url>https://doi.org/10.4242/BalisageVol31.HaentjensDekker0</url>
</record>

If we don’t require IXML to do all of the work, input like the example above could first be parsed into a graph with a parent item node for each property and child nodes for the each of original key and value parts of the property. An IXML grammar that creates this type of parse result might be:

record: item++eol, eol?.
item: key, -kv-sep, value.
key: ~[":"]+.
value: ~[#d; #a]+.
-kv-sep: -": ".
-eol: #d?, #a.

The output of that IXML process looks like:

<record>
    <item>
        <key>id</key>
        <value>haentjensdekker2026</value>
    </item>
    <item>
        <key>paper-title</key>
        <value>On beyond Invisible XML: parsing with implicit (and explicit) next-generation markup</value>
    </item>
    <!-- other items -->
</record>

We could then pass the preceding output through an XSLT transformation that turns each <item> element into the output we want. That XSLT might look like:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math" exclude-result-prefixes="#all"
    version="3.0">
    <xsl:template match="record">
        <!-- Don't copy possible ixml attributes -->
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="item">
        <xsl:element name="{key}">
            <xsl:value-of select="value"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

The result of applying the XSLT transformation to the output of our IXML would match our target XML, above.

If we instead want to create the final output directly with IXML, without requiring subsequent transformation with XSLT, we can use a grammar like the following:

{ root }
record: item++eol, eol?.

{ or-list of all named fields}
-item: id; paper-title; author; collection-title; series-title; 
       volume; publication-date; doi; url.

{ productions for named fields }
id: -"id", kv-sep, value.
paper-title: -"paper-title", kv-sep, value.
author: -"author", kv-sep, value.
collection-title: -"collection-title", kv-sep, value.
series-title: -"series-title", kv-sep, value.
volume: -"volume", kv-sep, value.
volume-title: -"volume-title", kv-sep, value.
publication-date: -"publication-date", kv-sep, value.
doi: -"doi", kv-sep, value.
url: -"url", kv-sep, value.

{ input formatting }
-kv-sep: -": ".
-value: ~[#d; #a]+.
-eol: #d?, #a.

The XML output of this process also matches our target. Here’s how it works:

  • Line 2: Split the input into a sequence of lines, each of which we call an item.

  • Lines 5–6: Handle each line according to the matching production on lines 9–18.

  • Lines 9–18: For each input line, create an XML element in the output where the element name matches the key part of the input line and the element content is copied from the part of the input line after the separator.

If we require IXML to do all of the work there is no escaping the need to create individual productions for each XML element type that we want to include in the output. We can, though, make the logic easier to understand by answering what types of elements do we need to create? in the or-list associated with item (lines 5–6) and answering how do we create those elements? by writing the productions according a simple and uniform template (lines 9–18).

Tagging named JSON properties

We can adapt the strategy above to parse the same type of input when it is formatted as JSON, along the lines of:

{
    "id": "haentjensdekker2026",
    "paper-title": "On beyond Invisible XML: parsing with implicit (and explicit) next-generation markup",
    "author1": "Haentjens Dekker, Ronald",
    "author2": "Birnbaum, David J.",
    "author3": "Buitendijk, Bram",
    "collection-title": "Proceedings of Balisage: The Markup Conference 2026",
    "series-title": "Balisage Series on Markup Technologies",
    "volume": "31",
    "publication-date": "2026",
    "doi": "10.4242/BalisageVol31.HaentjensDekker010",
    "url": "https://doi.org/10.4242/BalisageVol31.HaentjensDekker0"
}

JSON does not permit duplicate keys, so we’ve given the three authors different keys (author1, author2, author3), and we’ll unify them in our IXML. There are better ways to avoid duplicate keys of this sort in JSON, and we’ll explore how to engage with those using IXML later.

We can process this input with the skeletal JSON-to-XML IXML grammar above, and it will match every member (key-value pair) in the same way. If we know all possible member property names (keys) in advance, though, we can implement a variation of the strategy we used with the immediately preceding example, that is:

  • Rewrite the production for member to refer to an or-list of all input JSON keys, with the modification for the three author-related keys that we describe below.

  • Create a separate production for each matched key, using the strategy above, but with the modification for the authors described below.

  • Use a pattern that matches all three author keys in both the or-list of match values and the production that describes that nonterminal.

The complete revised grammar is:

ixml version "1.1".
{ must define root first }
record: ws*, (obj; arr), ws*.

{ or-list of all output types; 
  includes "author", but not "author1", etc. }
-member: id; paper-title; author; collection-title; series-title; 
         volume; publication-date; doi; url.

{ productions for each output type; the three types 
  of author are handled by the same rule }
id: member-start, -"id", member-end.
paper-title: member-start, -"paper-title", member-end.
author: member-start, -"author", -[N], member-end. { "author" plus one digit }
collection-title: member-start, -"collection-title", member-end.
series-title: member-start, -"series-title", member-end.
volume: member-start, -"volume", member-end.
volume-title: member-start, -"volume-title", member-end.
publication-date: member-start, -"publication-date", member-end.
doi: member-start, -"doi", member-end.
url: member-start, -"url", member-end.

{ From JSON spec, with attention to where whitespace is defined 
  See also rule for member, above }
-members: member++prop-sep. 
-member-start: ws*, quote.
-member-end: quote, ws*, kv-sep, element.
-element: ws*, value, ws*.
-elements: element++prop-sep.
-value: obj; arr; string; number. { JSON also allows true, false, and null }
-obj: obj-start, members, obj-end.
-arr: arr-start, elements, arr-end.
-chars: ~['"\']*. { JSON also prohibits control characters }
-string: quote, chars, quote.
-number: [N]+.

{ JSON delimiters }
-obj-start: -"{".
-obj-end: -"}".
-arr-start: -"[".
-arr-end: -"]".

{ JSON separators }
-prop-sep: -comma.
-kv-sep: -colon.
-comma: -",".
-colon: -":".

{ JSON whitespace and quotes }
-space: -" ".
-eol: -#d?, -#a.
-ws: space; eol.
-quote: -'"'.

Here is how it works:

  1. The LHS of line 3 is record because that the name of the XML root element that we’re creating.

  2. Lines 7–8 constitute an or-list of all output element types. Note that the list includes author, but not author1, etc.

  3. Lines 12–21 contain one production for each output element type, including one for author. The productions all follow the same pattern: the LHS is the name of the output element and the RHS is the pattern member-start, then the string value of the specific key of the JSON property that the production matches, and then the pattern member-end.

    The only special case is that because we want to output all three slightly different author inputs as the same XML element type, we have a single production with a LHS of author that applies to input lines that match the string author followed by any single digit.

  4. Our grammar already includes productions that handle generic JSON structures, and the only difference we need to care about in the input properties is that each different output element type requires its own production. Those productions follow a shared template, which begins with member-start and ends with member-end; the only difference is the string value of the matched input property in the middle. The shared components (member-start, member-end are described on lines 26–27. There’s no way to avoid the repetition of the shared components, but factoring out the common beginnings and ends makes the property-specific productions simpler and easier to write and read.

  5. We put minus signs before the LHS of all of the general JSON patterns (lines 25 through the end). This ensures that no elements with those names are created in the output. We remove the line for key (which was part of our original skeleton, above) entirely because we are matching each JSON key explicitly, by name, onlines 12–21.

Tagging anonymous JSON items

We’ve already processed anonymous JSON items; when we created a root element <record> in the preceding section, we created it as the name of the entire JSON input, which was an anonymous object. In this section we’ll discuss how to tag anonymous JSON objects other than the root, and in following sections we’ll also explore how to tag anonymous JSON arrays and strings.

We mentioned above that JSON does not permit duplicate property keys, and in the preceding section we worked around that by labeling the three authors in the JSON input as author1, author2, and author3. Bibliographic modeling considers co-authorship to involve ordering, which is why we talk about, for example, first author as a distinct subcategory of author. With that said, using different property keys for each author in a JSON corpus is an anti-pattern because it conceals what all authors have in common, that is, simple authorship.

The properties of JSON objects are unordered, but the contents of JSON arrays are considered informationally ordered. We could, then, have started from JSON that had a single authors key, the value of which was an array of the three authors. Here is how that might look (we remove the rest of the JSON structure from the example so that we can focus on just the authors):

{
  "authors": [
    {
      "surname": "Haentjens Dekker",
      "forename": "Ronald"
    },
    {
      "surname": "Birnbaum",
      "forename": "David"
    },
    {
      "surname": "Buitendijk",
      "forename": "Bram"
    }
  ]
}

We might want to use IXML to convert this input to:

<authors>
  <author>
    <surname>Haentjens Dekker</surname>
    <forename>Ronald</forename>
  </author>
  <author>
    <surname>Birnbaum</surname>
    <forename>David</forename>
  </author>
  <author>
    <surname>Buitendijk</surname>
    <forename>Bram</forename>
</authors>

The input is an object with a single property that has authors as its key and an array of anonymous objects as its value. We can use the strategy we describe in the preceding section to match that property and create an XML element with the name <authors>. Because the only way to create an XML element in the output requires a production that names that element in the LHS, our grammar needs to match each individual anonymous author object from the input, give it the name author, and let a production for author create the <author> element in the XML output.

Here is an IXML grammar that creates the desired output:

ixml version "1.1".
{ must define root first }
record: ws*, (obj; arr), ws*.

{ or-list of all output types matched by named inputs; "author" is not included 
  because the individual authors are anonymous in the input }
-member: authors; surname; forename.

{ productions for each simple type named in the input }
forename: member-start, -"forename", member-end.
surname: member-start, -"surname", member-end.

{ productions for author array }
authors: member-start, -"authors", quote, ws*, kv-sep, author-array.
-author-array: ws*, arr-start, author++prop-sep, arr-end, ws*.
author: element.

{ From JSON spec, with attention to where whitespace is defined 
  See also rule for member, above }
-members: member++prop-sep. 
-member-start: ws*, quote.
-member-end: quote, ws*, kv-sep, element.
-element: ws*, value, ws*.
-elements: element++prop-sep.
-value: obj; arr; string; number. { JSON also allows true, false, and null }
-obj: obj-start, members, obj-end.
-arr: arr-start, elements, arr-end.
-chars: ~['"\']*. { JSON also prohibits control characters }
-string: quote, chars, quote.
-number: [N]+.

{ JSON delimiters }
-obj-start: -"{".
-obj-end: -"}".
-arr-start: -"[".
-arr-end: -"]".

{ JSON separators }
-prop-sep: -comma.
-kv-sep: -colon.
-comma: -",".
-colon: -":".

{ JSON whitespace and quotes }
-space: -" ".
-eol: -#d?, -#a.
-ws: space; eol.
-quote: -'"'.

Here is how it works:

Lines 14–16 look more complicated than they are. What they do, in combination, is 1) assign a distinct name (author-array) to the array of authors so that we can distinguish it from generic arrays, and 2) assign names (author) to the anonymous JSON objects that make up the content of that array. We put a minus sign in front of the production for author-array (line 15) because we don’t want <author-array> tags in the output. We don’t put a minus before the production for author (line 16) because we do want <author> tags in the output.

Tagging anonymous JSON arrays and strings

In the preceding sections we tagged anonymous objects, and in this section we’ll tag anonymous arrays and strings. We’ll work with an abridged JSON representation of some bibliographic information. The full record might look like:

{
  "title": "The Gilded Age: A Tale of Today",
  "authors": [
    {
      "name": "Mark Twain",
      "name_parts": [
        "Mark",
        "Twain"
      ],
      "roles": [
        "author",
        "satirist"
      ]
    },
    {
      "name": "Charles Dudley Warner",
      "name_parts": [
        "Charles",
        "Dudley",
        "Warner"
      ],
      "roles": [
        "author",
        "editor"
      ]
    }
  ],
  "publication": {
    "year": 1873,
    "place": {
      "city": "Hartford",
      "state": "Connecticut",
      "country": "United States"
    },
    "publisher": {
      "name": "American Publishing Company",
      "locations": [
        "Hartford",
        "Chicago",
        "Cincinnati"
      ]
    }
  },
  "contents": {
    "volumes": [
      [
        "Chapter 1",
        "Chapter 2",
        "Chapter 3"
      ],
      [
        "Chapter 4",
        "Chapter 5",
        "Chapter 6"
      ]
    ]
  }
}

To concentrate on array and string processing we remove the authors and publication properties and simplify the content to just:

{
  "title": "The Gilded Age: A Tale of Today",
  "contents": {
    "volumes": [
      [
        "Chapter 1",
        "Chapter 2",
        "Chapter 3"
      ],
      [
        "Chapter 4",
        "Chapter 5",
        "Chapter 6"
      ]
    ]
  }
}

This example now contains both a named array (volumes) and two anonymous arrays (the members of the volumes array). This is similar to the way the authors array in the preceding section contained anonymous objects, except here the volumes array contains anonymous arrays instead of anonymous objects. Those inner anonymous arrays contain anonymous strings.

The JSON input in the preceding section was an object with a single key-value pair (with authors as the key), and we allowed the key to serve as the root element of the XML result. The JSON input in this section has, after abridgement, two key-value pairs (with title and contents as keys), and because XML requires a single root element, we’ll have to create a root element (we’ll call it <book>) to contain child elements for the two top-level input properties. The result we’re aiming for is:

<book>
  <title>The Gilded Age: A Tale of Today</title>
  <contents>
    <volume>
      <part>Chapter 1</part>
      <part>Chapter 2</part>
      <part>Chapter 3</part>
    </volume>
    <volume>
      <part>Chapter 4</part>
      <part>Chapter 5</part>
      <part>Chapter 6</part>
    </volume>
  </contents>
</book>

The only property names from the JSON input that become named elements in the XML output are title and contents. The key volumes in the input does not appear in the output, and XML elements must be created for each volume (anonymous arrays in the JSON input) and volume part, or chapter label (anonymous strings in the JSON input).

An IXML grammar that will implement this markup is:

ixml version "1.1".
{ must define root first }
book: ws*, (obj; arr), ws*.

-member: title; contents; volumes.
title: member-start, -"title", member-end. 
contents: member-start, -"contents", member-end. 
-volumes: member-start, -"volumes", quote, ws*, kv-sep, ws*, 
  arr-start, volume++prop-sep, arr-end, 
  ws*. 
volume: ws*, 
  arr-start, part++prop-sep, arr-end, 
  ws*.
part: element.

{ From JSON spec, with attention to where whitespace is defined }
-members: member++prop-sep. 
-member-start: ws*, quote.
-member-end: quote, ws*, kv-sep, element.
-element: ws*, value, ws*.
-elements: element++prop-sep.
-value: obj; arr; string; number. { JSON also allows true, false, and null }
-obj: obj-start, members, obj-end.
-arr: arr-start, elements, arr-end.
-chars: ~['"\']*. { JSON also prohibits control characters }
-string: quote, chars, quote.
-number: [N]+.

{ JSON delimiters }
-obj-start: -"{".
-obj-end: -"}".
-arr-start: -"[".
-arr-end: -"]".

{ JSON separators }
-prop-sep: -comma.
-kv-sep: -colon.
-comma: -",".
-colon: -":".

{ JSON whitespace and quotes }
-space: -" ".
-eol: -#d?, -#a.
-ws: space; eol.
-quote: -'"'.

Here are the important details:

The take-aways are that:

Inserting an additional level of wrapping around a single input item

A production for a pattern that we match in the input can wrap tags around the pattern in the output, so that if our input is:

Mobile: 123-456-7890

we can tag it as:

<Mobile>123-456-7890</Mobile>

with

Mobile: -"Mobile: ", [N; "-"]+.

What if we want to wrap two layers of markup around an input item? Suppose, for example, we want to tag the same input as:

<tel>
  <Mobile>123-456-7890</Mobile>
</tel>

To create this additional layer of markup we can use:

tel: Mobile.
Mobile: -"Mobile: ", [N; "-"]+.

The rule for tel, which outputs the outer XML tags, has, as its RHS, the non-terminal (Mobile) for which we wrote a rule earlier. The take-away is that if we want to emit two levels of markup, we need to create two levels of rules in the grammar. Every LHS (not preceded by a minus sign or at-sign) creates exactly one XML element, so if we want nested <tel> and <Mobile> elements in the output, we need rules for both in the grammar. Since we want to wrap the <tel> tags around the <Mobile> tags, our IXML says that the content of a tel is a Mobile and the content of a Mobile is whatever matches the pattern that describes our input.

Inserting an additional level of wrapping around a sequence of input intems

The preceding section wrapped an additional layer of markup around an input pattern, but what if want to wrap multiple patterns in a wrapper? Suppose our input is:

Name: Tony Stark
Home: 123-456-7777
Work: 123-456-8888
Mobile: 123-456-9999
Office: My Awesome Office Complex, Suite 000

and the output we want is:

<record>
  <Name>Tony Stark</Name>
  <Tel>
    <Home>123-456-7777</Home>
    <Work>123-456-8888</Work>
    <Mobile>123-456-9999</Mobile>
  </Tel>
  <Office>My Awesome Office Complex, Suite 000</Office>
</record>

In this case we want to wrap a <Tel> element around the group of three telephone numbers in the input. A useful way to think of this type of task is that although the three numbers do not constitute a structural group in the input, they will in the output, which means that the wrapper name has to appear as the LHS of a grammar production, with a mention in the RHS that matches the content that we want to wrap. Here is an IXML grammar that will apply the markup we want:

record: Name, eol, Tel, eol, Office.
Name: -"Name: ", chars.
Tel: Home, eol, Work, eol, Mobile.
Home: -"Home: ", chars.
Work: -"Work: ", chars.
Mobile: -"Mobile: ", chars.
Office: -"Office: ", chars.
-chars: ~[#d; #a]+.
-eol: #d?, #a.

The RHS of the rule for record, which is the root element we are creating, mentions only the immediate children of the XML <record> element that we are creating: <Name>, <Tel>, and <Office>. Because the three telephone numbers are not children of the XML root, they are not (directly) part of the RHS of the rule for record. The inclusion of Tel in the RHS online 1 means that the output will have a <Tel> element between the <Name> and <Office> elements. The production for Tel on line 3 will match the sequence of three telephone numbers and create corresponding XML elements for each of them as children of <Tel>.

One limitation of this method is that because IXML cannot read input and save it for use at later time, content cannot be reordered. This means that IXML can wrap groups of input items, as we do above, only as long as they are adjacent, since it is the adjacency that lets us describe the entire sequence as a single pattern to be matched and tagged.

Branching decisions

Suppose we want to ingest numerical date information and left-pad one-digit month or date numbers with zero, so that, for example, September 30, 2026 would become 2026-09-30, with a zero prepended to the month number. Our input might look like:

{
  "date-parts": [
    2026,
    9,
    30
  ]
}

The input includes an array of numbers, which our IXML grammar will match as strings of digits. The challenge is to distinguish one-digit numbers (in this case, the month) from longer numbers (in this case, the year and day) so that we can left-pad the former, but not the latter, with a zero. The following IXML grammar does that:

ixml version "1.1".
{ must define root first }
date: obj.

{ From JSON spec, with attention to where whitespace is defined }
-members: member++prop-sep. 
-member: date-parts.
-date-parts: member-start, -"date-parts", quote, ws*, kv-sep, ws*,
  arr-start, 
  year,
  month,
  day, 
  arr-end,
  ws*.
-year: ws*, number, ws*, prop-sep, hyphen.
-month: ws*, number, ws*, prop-sep, hyphen.
-day: ws*, number, ws*.
-hyphen: +"-".
-member-start: ws*, quote.
-member-end: quote, ws*, kv-sep, element.
-element: ws*, value, ws*.
-elements: element++prop-sep.
-value: obj; arr; string; number. { JSON also allows true, false, and null }
-obj: obj-start, members, obj-end.
-arr: arr-start, elements, arr-end.
-chars: ~['"\']*. { JSON also prohibits control characters }
-string: quote, chars, quote.
-number: one-digit; multi-digit.
-one-digit: +"0", [N].
-multi-digit: [N], [N]+.

{ JSON delimiters }
-obj-start: -"{".
-obj-end: -"}".
-arr-start: -"[".
-arr-end: -"]".

{ JSON separators }
-prop-sep: -comma.
-kv-sep: -colon.
-comma: -",".
-colon: -":".

{ JSON whitespace and quotes }
-space: -" ".
-eol: -#d?, -#a.
-ws: space; eol.
-quote: -'"'.

and the output it:

<date>2026-09-30</date>

In preceding sections we recursively unpacked and expanded productions so that we could assign names to anonymous subcomponents that we needed to tag (e.g., anonymous JSON objects for multiple authors, anonymous strings for numbered chapters). Here we perform the same sort of expansion not to tag the subcomponents (year, month, date), but to make decisions about whether or not to left-zero-pad each of them. We dissect the input into year, month, and day in lines 8–14 and dissect each of those into a number in lines 15–17. We then define number as either one-digit or multi-digit in line 28, and let lines 29–30 prepend a zero to a one-digit number but not to a multi-digit one. We could have inlined some of the patterns, such as the ones for year, month, and day, but we find the code easier to understand if we extract identifiable subpatterns and give them recognizable names.

IXML does not support if / then /else syntax (like XPath) or when / otherwise logic (like XSLT), so here we use declarative pattern-matching to implement the same logic. The important details are that 1) the patterns are mutually exclusive and 2) the matching is exhaustive. That is, every number that the IXML grammar will need to handle will match either the one-digit pattern but not the two-digit pattern or vice versa.

Inserting elements, attributes, and text not present in the input

Suppose we want to create an element or attribute in the output that does not correspond to anything in the input. In some of the examples above we created XML output for anonymous JSON objects (such as authors), arrays (such as volumes), and strings (such as chapters), but in those cases we created names for items that were already present, even if only anonymously, in the input. In other examples above we created an inserted level of wrapper for content already present in the input, such as wrapping the three telephone numbers in a shared parent. Below we explore how to create output that is not represented at all in the input.

The IXML idiom of creating new textual content is an insertion, and we can insert either nodes (elements or attribute) or textual values. To insert an element that does not tag anything in the input we include the element name in the RHS of a matched pattern where we want it to appear in the output even though it doesn’t match anything in the input, and we then create a rule for it. Suppose, for example, that we want to create an element to hold the date that we created a record as part of an IXML process. IXML cannot access or compute a date, so we if insist on adding this information using IXML, we would need to supply the date in our grammr.

Assume that we start with the the following plain-text input, which we also used in a previous example:

Name: Tony Stark
Home: 123-456-7777
Work: 123-456-8888
Mobile: 123-456-9999
Office: My Awesome Office Complex, Suite 000

but this time the output we want is:

<record>
  <Name>Tony Stark</Name>
  <Tel>
    <Home>123-456-7777</Home>
    <Work>123-456-8888</Work>
    <Mobile>123-456-9999</Mobile>
  </Tel>
  <Office>My Awesome Office Complex, Suite 000</Office>
  <last-modified>2026-09-30</last-modified>
</record>

The only difference between this and the earlier example is that this time we insert a <last-modified> element with a specified date before the end-tag for the root element. We obtain that result with the following modification of our original IXML grammar:

record: Name, eol, Tel, eol, Office, last-modified.
Name: -"Name: ", chars.
Tel: Home, eol, Work, eol, Mobile.
Home: -"Home: ", chars.
Work: -"Work: ", chars.
Mobile: -"Mobile: ", chars.
Office: -"Office: ", chars.
last-modified: +"2026-09-30".
-chars: ~[#d; #a]+.
-eol: #d?, #a.

We've modified line 1 and inserted line 8, as follows:

If we want to insert the date-stamp as an attribute, we can use the following IXML:

record: Name, eol, Tel, eol, Office, last-modified.
Name: -"Name: ", chars.
Tel: Home, eol, Work, eol, Mobile.
Home: -"Home: ", chars.
Work: -"Work: ", chars.
Mobile: -"Mobile: ", chars.
Office: -"Office: ", chars.
@last-modified: +"2026-09-30".
-chars: ~[#d; #a]+.
-eol: #d?, #a.

The only change is that we’ve added an at-sign before the LHS of the last-modified rule on line 8. The output is now;

<record last-modified="2026-09-30">
   <Name>Tony Stark</Name>
   <Tel>
      <Home>123-456-7777</Home>
      <Work>123-456-8888</Work>
      <Mobile>123-456-9999</Mobile>
   </Tel>
   <Office>My Awesome Office Complex, Suite 000</Office>
</record>

Those familiar with XSLT will recall that if we add attributes to an element as part of an XSLT transformation we need to create the attribute nodes before we create any content, but IXML doesn’t work that way. In IXML we can specify attributes anywhere we want in the RHS and they will be added to the element named on the LHS. This feature provides a special type of content rearrangement, which, as we mention elsewhere, IXML otherwise does not permit. Suppose our input is:

Name: Tony Stark
Home: 123-456-7777
Work: 123-456-8888
Mobile: 123-456-9999
Office: My Awesome Office Complex, Suite 000
last-modified: 2026-09-30

As far as the input is concerned the date-stamp is already present, but it’s at the end, and we want to include it as an attribute on the root element, which might feel as if it’s at the beginning because when XML is serialized, any attributes on an element are rendered inside the start-tag. We can do that with the following IXML grammar:

record: Name, eol, Tel, eol, Office, eol, last-modified.
Name: -"Name: ", chars.
Tel: Home, eol, Work, eol, Mobile.
Home: -"Home: ", chars.
Work: -"Work: ", chars.
Mobile: -"Mobile: ", chars.
Office: -"Office: ", chars.
@last-modified: -"last-modified: ", chars.
-chars: ~[#d; #a]+.
-eol: #d?, #a.

The output with this grammar is:

<record last-modified="2026-09-30">
   <Name>Tony Stark</Name>
   <Tel>
      <Home>123-456-7777</Home>
      <Work>123-456-8888</Work>
      <Mobile>123-456-9999</Mobile>
   </Tel>
   <Office>My Awesome Office Complex, Suite 000</Office>
</record>

Although the date-stamp is at the end of the input, this grammar is able to move it into the start-tag of the emitted <record> element. We’ve written move it in quotation marks because it isn’t really being moved; XML attributes are properties of elements, rather than element content, and they are not informationally ordered with respect to either the element content or its other attributes. This feature of IXML is convenient because IXML can match content only in the order in which it occurs, and if we identify part of the content as a property to be represented as an attribute, its exact original location matters less, since it will always appear on the element created by that rule and named on the LHS.

If we want to insert an empty element with neither content nor attributes we can do that with an empty rule. For example, if we go back to our original plain-text input, without a last-modified line, and process it with the following IXML grammar:

record: Name, eol, Tel, eol, Office, +#a, checked.
Name: -"Name: ", chars.
Tel: Home, eol, Work, eol, Mobile.
Home: -"Home: ", chars.
Work: -"Work: ", chars.
Mobile: -"Mobile: ", chars.
Office: -"Office: ", chars.
checked: .
-chars: ~[#d; #a]+.
-eol: #d?, #a.

the output acquires an empty <checked/> element before the root end-tag:

<record>
   <Name>Tony Stark</Name>
   <Tel>
      <Home>123-456-7777</Home>
      <Work>123-456-8888</Work>
      <Mobile>123-456-9999</Mobile>
   </Tel>
   <Office>My Awesome Office Complex, Suite 000</Office>
   <checked/>
</record>

The RHS of line 1 says to expect an instance of checked, and the rule on line 8 creates a <checked> element in the output because that’s what activating a rule with the LHS checked does. A production that creates an element named in the LHS uses its RHS to match part of the input, which supplies the content and attributes of that element. Since the RHS in this case is empty, the rule matches nothing in the input (which is appropriate for an inserted element that is not supposed to match anything in the input) and the output contains an empty element.

Wrapping input and inserting an additional element or attribute into the wrapper

Imagine the following JSON input:

{
  "title": "My awesome conference paper",
  "surname": "Doe",
  "forename": "John"
}

which we want to tag as:

<record>
   <title>My awesome conference paper</title>
   <person>
      <namePart type="surname">Doe</namePart>
      <namePart type="forename">John</namePart>
      <role>author</role>
   </person>
</record>

In this case we combine several things that we did separately in earlier sections:

An IXML grammar that does that is:

ixml version "1.1".
{ must define root first }
record: obj-start, title, prop-sep, person, obj-end.

{ From JSON spec, with attention to where whitespace is defined }
-members: member++prop-sep. 
-member: title; namePart.
person: namePart++prop-sep, role.
title: member-start, -"title", member-end.
namePart: member-start, type, member-end.
@type: "surname"; "forename".
role: +"author".
-member-start: ws*, quote.
-member-end: quote, ws*, kv-sep, element.
-element: ws*, value, ws*.
-elements: element++prop-sep.
-value: obj; arr; string; number. { JSON also allows true, false, and null }
-obj: obj-start, members, obj-end.
-arr: arr-start, elements, arr-end.
-chars: ~['"\']*. { JSON also prohibits control characters }
-string: quote, chars, quote.
-number: [N]+.

{ JSON delimiters }
-obj-start: -"{".
-obj-end: -"}".
-arr-start: -"[".
-arr-end: -"]".

{ JSON separators }
-prop-sep: -comma.
-kv-sep: -colon.
-comma: -",".
-colon: -":".

{ JSON whitespace and quotes }
-space: -" ".
-eol: -#d?, -#a.
-ws: space; eol.
-quote: -'"'.

Here is how it works: