11

I would like to ask a question about XML and S-expressions(-ish) notation. S-expressions are pretty old; they are also really simple. We could consider two forms that are equal in meaning, different in syntax:

(xml code taken from Polish wikipedia)

<?xml version="1.0" encoding="UTF-8"?>
<ksiazka-telefoniczna kategoria="bohaterowie książek">
 <!-- komentarz -->
  <osoba charakter="dobry">
    <imie>Ambroży</imie>
    <nazwisko>Kleks</nazwisko>
    <telefon>123-456-789</telefon>
  </osoba>
  <osoba charakter="zły">
    <imie>Alojzy</imie>
    <nazwisko>Bąbel</nazwisko>
    <telefon/>
  </osoba>
</ksiazka-telefoniczna>

S-Expression(-ish) version:

(:version "1.0" :encoding "utf-8")
(ksiazka-telefoniczna :category "bohaterowie książek"
  ; komentarz(a comment)
  (osoba :charakter "dobry"
    (imie Ambroży)
    (nazwisko Kleks)
    (telefon 123-456-789))
  (osoba :charakter "zły"
    (imie Alojzy)
    (nazwisko Bąbel)
    (telefon)))

The S-Expression version is much more concise. We avoid redundancy by using simple list notations, yet we still can define syntax to include things that we want to have(e.g. properties). Of course, this is just an example, and the actual standard could have been better or simply different; however, it's shorter and easier to parse. Why did XML win?

  • 3
    XML is not S-Expressions – Robert Harvey Jul 28 '16 at 17:26
  • 5
    To downvoters: don't downvote if you disagree with the question, but if you think it's of poor quality(and then, propose changes to improve the quality). @RobertHarvey If you think that it is an answer, please, answer my question instead of dropping a comment. – MatthewRock Jul 28 '16 at 17:31
  • 1
    The tooltip over the downvote button includes the phrase "this question doesn't show any research effort." – Robert Harvey Jul 28 '16 at 17:34
  • 1
    Try to remember that this is not a discussion forum. Real question have answers, and community members are expected to provide answers, not opinions. – Robert Harvey Jul 28 '16 at 17:39
  • 1
    The redundancy arguments for XML (like having closing brackets with the name of the opening bracket) can easily be emulated by S-expressions. Simply write (para "This is a paragraph " (footnote "(better than the one under there)" "." /footnote) /para). – Andrew Piliser Jul 28 '16 at 19:03
13

We know the designers of XML were familiar with S-expressions, since XML is based on SGML, and SGML has a style sheet language, DSSSL, which uses S-expression syntax (and scheme as embedded scripting language).

Nevertheless they chose a different syntax than S-expressions due to the use cases for XML. XML was initially designed to support both machine-generated structured data and markup languages like HTML, which are authored manually and contains mixed content (text intermingled with elements with metadata).

Redundancy

Markup text documents are often longer than a screenful. If you see a ) and you can't see the beginning of the structure, you are pretty lost; you don't know if the was a chapter or a sidebar which just ended. The redundancy of repeating the tagname in endtags in XML like </sidebar> makes this much easier for the human writer. It also makes it more robust: if you accidentally delete an end tag, you can often infer which end-tag is missing.

SGML (the predecessor to XML) allowed you to optionally shorten the end-tag to a single character, but this feature was left out of XML for simplicity.

So in short, XML is more verbose by design, because it is designed to support human-editable document. Today XML is used for a wide variety of purposes, also for pure machine-to-machine communication, where this redundancy is not needed.

Mixed content

Your suggested syntax would not support mixed content very well. Take this example in HTML:

<p>Hi! <a href="example.com">Click here</a>!</p>

How would you express this in your syntax? You would need some kind of additional delimiter to distinguish between attributes and text content. Suddenly it it not so concise anymore.

Special characters

Angle brackets are much rarer in ordinary text than parentheses and colon.

Compatibility

HTML was already wildly successful at the time XML were designed, and it made sense to choose a similar syntax.

Why did XML win?

S-expressions were never an alternative to XML. The XML spec is much more than angle brackets; It defines a syntax for elements and attributes and mixed content, escaping, character encoding, DTD-syntax and validation and so on. Nothing similar existed for s-expressions. Of course you can define a similar standard, as you propose here, but nobody had done this at the time. XML got blessed by the W3C and was therefore adopted by major players and became the defacto standard for data exchange.

  • 3
    In his example, aren't colons used for attributes? E. g. (p Hi! (a :href "example.com" Click here)!) ? (or did he just edit that in after your answer was posted?) – Headcrab Jul 29 '16 at 15:34
  • Although it takes nothing away from your (excellent) answer, who in their right mind manually creates XML documents? – Jared Smith Jul 29 '16 at 17:40
  • Hey Jacques, thanks for this excellent answer! I do agree with Headcrab that mixed content isn't an issue. I also agree with Jared, although I guess that XML is read/wrote manually sometimes anyway. – MatthewRock Jul 30 '16 at 12:22
  • @Headcrab: It is hard to say since there is no actual spec, just a hypothetical example. But it seems to me representing text as symbols rather than a quoted string would lead to ambiguities with whitespace. S-expressions does not support significant whitespace between atoms AFAIK, but you need this to support e.g the <PRE> element in HTML. So I assumed quotes would be needed. – JacquesB Jul 30 '16 at 12:54
  • 2
    So it really looks like XML was created with all these bells and whistles and familiar HTML-like syntax which helped it win over s-expressions at the time. By the time many developers decided that, in their use cases, all these features aren't really necessary for machine-to-machine communication, there was a different lightweight alternative in the form of JSON. – kamilk Jul 30 '16 at 13:08
9

Personally, I think the best part about XML is the well-defined schema capabilities, rather than its syntax. The schema mechanism allows users to publish their document format to share what they consider a valid document. There are also automated validators. Plus, types and schemas created by one user can be extended by other users.

As far as I know no one has made anywhere near the effort to standardize a general purpose schema mechanism for s-expression, except for the LISP language itself (which the sample in the OP's question isn't using).

1

Here are two reasons that I would choose XML over something "S-expression-ish":

A well-defined syntactic and semantic model

XML is not simply a tree of nodes, but a tree of categorized nodes that have different syntactical representation and different behavior. For example, an attribute with a given name may only appear once for a given node, while child nodes may appear multiple times.

You could define such a model on top of generic S-expressions. Your examples show a scheme for categorizing attributes and child elements. Add in semantics for text, comments, and processing instructions, and you'll have something that is isomorphic with XML.

Tooling

From the standard syntactic and semantic model, you can build tools -- and lots of people have. You can find some form of XML parser/serializer, XPath, and XSLT processor for every common language/platform. And you know that they will all behave the same way on every platform.


And here are a few other things to consider:

In the grand scheme, XML isn't that verbose

In your example, what have you actually eliminated? As I read it, you've:

  • Eliminated the closing tag for each expression.
  • Eliminated the > that would normally separate the opening tag from its children.
  • Replaced the = that separates attribute name and value with a : to indicate that the child is an attribute; no savings.

I think it's also important to recognize that the internal and external representations of XML are very different. Internally, an XML tree is very compact. And because the various elements are already categorized, it's very efficient to manipulate. Externally, well, yeah you get all those closing tags, but they compress well.

Is "verbosity" the real issue?

I think that the real question is not whether XML is "verbose", but whether it's more expressive than is needed for a given purpose. Some examples:

  • The ability for an element to hold attributes, which are semantically different from child elements. Useful for out-of-band information, such as describing the native data type, of the element's content. But maybe you don't need that, because your external spec defines the content.
  • Mixed content, in which an element can hold both child elements and text (as well as comments and processing instructions). Useful for markup, but maybe not for simple data representation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.