mathiasbynens / small

Smallest possible syntactically valid files of different types
https://mathiasbynens.be/notes/minimal-html
1.93k stars 184 forks source link

[Requesting comment] Smallest possible XML file #32

Closed Zegnat closed 9 years ago

Zegnat commented 11 years ago

By strict reading of the XML 1.0 standard am I right to assume the smallest possible XML file is the following?

<?xml version='1.0'?><_/>

This does validate as ‘well-formed XML’ on the W3C Markup Validator. The only odd part is that the root element is seen as _/ rather than _. Looks like a bug, because it does treat the slash as a closing slash.

Not sure if I should pull, requesting comment.


A XML document is:

document      ::=   prolog element Misc*

Where:

prolog        ::=   XMLDecl? Misc* (doctypedecl Misc*)?
XMLDecl       ::=   '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'
VersionInfo   ::=   S 'version' Eq ("'" VersionNum "'" | '"' VersionNum '"')
Eq            ::=   S? '=' S?
VersionNum    ::=   '1.' [0-9]+

And:

element       ::=   EmptyElemTag
                    | STag content ETag

With:

EmptyElemTag  ::=   '<' Name (S Attribute)* S? '/>'

While:

Name          ::=   NameStartChar (NameChar)*

In short we get this when all optional (marked by ? or *) parts are left out:

document      ::= '<?xml' S 'version' '=' ("'" '1.' [0-9]+ "'" | '"' '1.' [0-9]+ '"') '?>' '<' NameStartChar '/>'
mathiasbynens commented 11 years ago

Your explanation sounds good enough to me.

Note that the HTML/XHTML files in this repository have the same issue: it’s all a matter of interpretation. Except in HTML5, there is a difference between what browsers/programs support, what the spec allows, and what the validator says.

Zegnat commented 11 years ago

XML 1.0, well-formed (xml-1.0.xml):

<_/>

XML 1.0, well-formed and valid (xml-1.0-valid.xml):

<!DOCTYPE _[<!ELEMENT _ EMPTY>]><_/>

First time reading I thought the XML declaration was required, but on a second reading I realised only prolog is required for a well-formed document and it can be empty (i.e. without XML declaration). I’ll check out XML 1.1 later and will make a pull request when I have them. Early Saturday, probably.

mathiasbynens commented 11 years ago

Nice work! Looking forward to the PR :)

waldyrious commented 9 years ago

@mathiasbynens shouldn't this be closed after the resolution of #34?

mathiasbynens commented 9 years ago

@waldyrious You’re right. Thanks!