Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I say <iframe /> and be done with it?

Tags:

xml

xhtml

I thought that in XML you could have a tag with opening and closing tags, and then if you didn't need the closing tag, you could just use a tag with a slash at the end of it.

<!-- needs text/elements within tag so opening and closing tag are needed -->
<myTag>Text here</myTag>
<!-- doesn't need text/elements so doesn't have closing tag -->
<myTag /> 

I'm guessing that this is just because that's whatever everyone decided was best and this is now just a XHTML peculiarity, but why? Is there a technical reason for this that I don't know about?

Thanks!

like image 992
slim Avatar asked Oct 21 '11 14:10

slim


2 Answers

It's because the XHTML DTD says so :)

Only these elements are allowed to be empty:

  • area
  • base
  • br
  • col
  • hr
  • img
  • input
  • link
  • meta
  • param

For example link is declared as:

<!ELEMENT link EMPTY>

whereas script and iframe are declared as:

<!ELEMENT script (#PCDATA)>

and

<!ELEMENT iframe %Flow;>
<!ENTITY % Flow "(#PCDATA | %block; | form | %inline; | %misc;)*">
...
like image 174
Daniel Rikowski Avatar answered Sep 19 '22 02:09

Daniel Rikowski


If browsers treated XHTML as XML, you would be right. But they treat it as HTML, where the rules are different.

like image 38
Michael Kay Avatar answered Sep 18 '22 02:09

Michael Kay