XML? Be cautious
blog.pragmatists.com
blog.pragmatists.com
Seriously, I urge you to look into SGML and to explore what a weird behemoth it is, and to then realize that SGML is what XML grew out of.
Unfortunately, XML was so bad that the developer community suffered some form of whiplash from it, and promptly decided to run into the opposite direction: JSON, which--superficially at least--seemed to be the simple and easy answer to the problems XML was causing.
However, I don't think that XML's problems were in its ideas, but that JSON is taking away too much that had only been perceived as bad, because it came coupled to such a flawed implementation overall.
And now we have efforts like JSON Schema, which emerge out of the realization that writing the same typecheckers over and over again for nearly every use case is tedious at best, and error-prone and unsafe at worst.
So maybe we will get our conveniently typed and namespaced semi-structured data in the end, but I blame XML that we had to take a detour over vanilla JSON to eventually get there.
But for structured data you don't need the verbosity and extra baggage of XML, JSON is simply a cleaner solution.
Problem is, people are looking for one-size-fits-all solution, but no format will be optimal for all use cases.
XML is a passable all-round format though, even if it is not always optimal.
The "too verbose" bit is overly broad cargo-cultism.
XML has valid, efficient applications. A prominent one: it's a more concise serialization format than JSON (the supposed alternative) for describing self-similar typed hierarchies, e.g. scene graphs or other UI trees. (see HTML, XAML, VRML, kind-of JSX.)
consider:
<div>
<span>Hello world</span>
<div>
vs: {
nodeType: "div",
children: [
{
nodeType: "span",
children: [
{
nodeType: "text",
value: "Hello world"
}
]
}
]
}
It's hard to take the rest of the article seriously when it starts with that unnecessary (and imprecise) jab at the technology it purports to be educating about.["div", ["span", "Hello world"]]
You can also add attributes:
["div", {"id": 123}, ["span", "Hello world"]]
Parsing is a bit trickier and the format is less obviously extensible, but in general, JSON arrays are more concise than JSON objects since you can leave out the field names.
Scheme has a decent, albeit small, standard [0].
SXML [1] is also standardized, as part of that process, which makes writing it nice and predictable. So, for the most part, SXML should work the same between implementations, but, if it doesn't, all you need to construct an SXML object is: quote or quasiquote and unquote.
SXML feels far more like a template language than a serialization format.
---
But the wider question:
> It's not clear I could find an s-expression parser in two different arbitrary languages and be sure they interoperate
For the most part, they should be.
But!
S-Expressions are a terribly inefficient structure in memory, or rather, can be. How you optimise that can have some impact on what the reader accepts or rejects, or how it compiles into memory.
Singly and doubly linked lists, vectors, arrays. S-Expressions can be all under the hood, or a mix.
Some parsers reject variadic s-expressions, others accept them happily, but others have a limited recursion depth, so technically valid s-expressions get rejected.
However, for the most part they're equal.
---
It also accepts things like ["div#123.class-name-1 class-name-2", ["span", "Hello world"]].
You can keep a reference of a node in a Javascript object if needed:
let refs = {};
let div = jso2dom(
["div",
["span", {"#":"sp"}, "Hello world"]
],
refs
);
console.log(refs.sp); // <span> Hello World </span>
I've found it very convenient so far and still use it today. This is simple, prodigiously stupid and works in vanilla JS, so this works for me for the time being.Parsing may be complex but in my case, this is done by the Javascript engine, so I'm not affected by this complexity.
When used as intended it is wonderful.