Search code examples
xmlxml-parsingdtd

XML DTD. Empty elements


How to designate empty element at an XML file: DTD:

<!ELEMENT WARNING EMPTY> 

XML:

 <WARNING /> 

или

<WARNING></WARNING> 

?

Both accepted by parser as correct.


Solution

  • The XML spec says explicitly that the two forms you give are to be treated as equivalent. See https://www.w3.org/TR/xml/#sec-starttags

    It does recommend (with a SHOULD) that the paired-tag form <e></e> be used for elements declared as being able to have content and the single-tag form <e/> for elements declared empty. In applications with neither DTD nor schema, this distinction is not available, and I don't have the impression that anyone pays any attention to the spec's recommendation in the matter. (It seemed like a good idea at the time.)

    [Addendum, later] I have recently had occasion to look again at the relevant part of the spec, and a friend called my attention to the fact that the recommendation that <e/> be used for elements declared EMPTY is labeled "For interoperability", which the spec defines as meaning "in order to increase the chances of successful inter0operation with SGML software that predates XML". That would seem to explain why few people follow the pattern described: few people nowadays need to inter-operate with SGML software developed before 1998.