Can a xml be well formed having not empty elements self closed?
Anything like this:
<team>Chicago Bulls<playerA/>23<playerB/>43</team>
instead of:
<team>Chicago Bulls<playerA>23</playerA><playerB>43</playerB></team>
Would it be well formed and where I could read about it? Mostly from w3c.
Thanks in advance
CodePudding user response:
Yes, both of your examples are well-formed, but to precede a player number with a player
element rather than place it in the contents of the player
element would be a very poor design decision as it would fail to leverage the natural hierarchy that XML tools and developers would expect.
You can read more about well-formedness,
but do not expect to see your point addressed explicitly because your question is less about well-formedness than it is about design.
Historical Note
XML's predecessor, SGML, had a notion of omitted end tags, which might be related to the motivation for your question. Instead of as in your first example where you use self-closing tags (which produce empty elements, to be clear),
<team>Chicago Bulls<playerA/>23<playerB/>43</team>
the playerA
and playerB
elements could be defined to have an omitted end tags, allowing the following markup:
<team>Chicago Bulls<playerA>23<playerB>43</team>
Your self-closing player
elements have been replaced by open tags with omitted end tags. This would not be well-formed XML.
CodePudding user response:
The XML you show is well-formed, but there are no "non-empty self-closed elements". If we add some whitespace, we can show the relationships between nodes:
<team>
Chicago Bulls
<playerA/>
23
<playerB/>
43
</team>
So, the team
element has 5 child nodes:
- A text node containing "Chicago Bulls"
- An element called
playerA
with no attributes or content - A text node containing "23"
- An element called
playerB
with no attributes or content - A text node containing "43"
No relationship between these nodes is represented, other than their order - in particular, the text "23" is not associated with either of the self-closing elements.
Another way to show this is to remember that <foo/>
is just a different way of writing <foo></foo>
, so we can also write this:
<team>Chicago Bulls<playerA></playerA>23<playerB></playerB>43</team>
In contrast, systematically adding whitespace to your second example would look like this:
<team>
Chicago Bulls
<playerA>
23
</playerA>
<playerB>
43
</playerB>
</team>
In this case, the teams
element has only three direct children:
- A text node containing "Chicago Bulls"
- An element called
playerA
, which contains a text node containing "23" - An element called
playerB
, which contains a text node containing "43"