Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   XML (http://www.velocityreviews.com/forums/f32-xml.html)
-   -   XHTML user agent behavior regarding empty elements (http://www.velocityreviews.com/forums/t165509-xhtml-user-agent-behavior-regarding-empty-elements.html)

Mikko Ohtamaa 08-28-2003 07:35 AM

XHTML user agent behavior regarding empty elements
 
From XML specification:

[Definition: An element with no content is said to be empty.] The
representation of an empty element is either a start-tag immediately
followed by an end-tag, or an empty-element tag.

(This means that <foo></foo> is equal to <foo/>)

From XHTML specification:

C.3. Element Minimization and Empty Element Content
Given an empty instance of an element whose content model is not EMPTY
(for example, an empty title or paragraph) do not use the minimized form
(e.g. use <p> </p> and not <p />).

From XML point of view <div/> and <div></div> are equal. However, XHTML,
which should be valid XML, recommends(?) to use <div></div> only. Should
XHTML browsers accept empty-element tags?

A little testing shows that this is not the case. Both IE 5.5 and Netscape
7.0 fail to render following XHTML code correctly. They consider
empty-element tag <div/> equal to <div>.

This is nuisance, since when you are producing XHTML from XML with XSLT
transform, XSLT transformers present empty elements using empty-element
tag notation. You must use external postprocessor to change <div/>
elements to <div></div> pairs.

<?xml version="1.0" encoding="utf-8" ?>
<html>
<body>

<div style="margin-left: 10%; background: blue">
A working sample.
<div style="margin-left: 10%; background: red">
Lalihoo!
<div id="blaah"></div>
Am I red?
</div>
Am I blue?
</div>

<br/>

<div style="margin-left: 10%; background: blue">
Hiihoo!

<div style="margin-left: 10%; background: red">
Lalihoo!
<div id="blaah"/>
Am I red?
</div>
Am I blue? No, I am red because I am confused.
</div>
</body>
</html>

Julian F. Reschke 08-28-2003 07:52 AM

Re: XHTML user agent behavior regarding empty elements
 
"Mikko Ohtamaa" <moo@sneakmail.zzn.com> schrieb im Newsbeitrag
news:26caea2b.0308272335.3f47cef1@posting.google.c om...
> From XML specification:
>
> [Definition: An element with no content is said to be empty.] The
> representation of an empty element is either a start-tag immediately
> followed by an end-tag, or an empty-element tag.
>
> (This means that <foo></foo> is equal to <foo/>)
>
> From XHTML specification:
>
> C.3. Element Minimization and Empty Element Content
> Given an empty instance of an element whose content model is not EMPTY
> (for example, an empty title or paragraph) do not use the minimized form
> (e.g. use <p> </p> and not <p />).
>
> From XML point of view <div/> and <div></div> are equal. However, XHTML,
> which should be valid XML, recommends(?) to use <div></div> only. Should
> XHTML browsers accept empty-element tags?


a) The quote in C.3 is from the (non-normative) chapter "HTML compatibility
guidelines".

b) They must.

> A little testing shows that this is not the case. Both IE 5.5 and Netscape
> 7.0 fail to render following XHTML code correctly. They consider
> empty-element tag <div/> equal to <div>.


IE is known not to support XHTML. For NS 7, this may be a bug that needs to
be fixed. Make sure that you are serving the XHTML in a way that the browser
is *aware* that this is not HTML, though.

> ...


Julian



Johannes Koch 08-28-2003 08:11 AM

Re: XHTML user agent behavior regarding empty elements
 
Mikko Ohtamaa wrote:
> From XML specification:
>
> [Definition: An element with no content is said to be empty.] The
> representation of an empty element is either a start-tag immediately
> followed by an end-tag, or an empty-element tag.
>
> (This means that <foo></foo> is equal to <foo/>)
>
> From XHTML specification:
>
> C.3. Element Minimization and Empty Element Content
> Given an empty instance of an element whose content model is not EMPTY
> (for example, an empty title or paragraph) do not use the minimized form
> (e.g. use <p> </p> and not <p />).
>
> From XML point of view <div/> and <div></div> are equal.


From XML 1.0 Second Edition:
Empty-element tags may be used for any element which has no content,
whether or not it is declared using the keyword EMPTY. For
interoperability, the empty-element tag should be used, and should only
be used, for elements which are declared EMPTY.

> However, XHTML,
> which should be valid XML, recommends(?) to use <div></div> only. Should
> XHTML browsers accept empty-element tags?


Yes, they should.

> A little testing shows that this is not the case. Both IE 5.5 and Netscape
> 7.0 fail to render following XHTML code correctly.


IE 5.5 is no XHTML browser, maybe it can be called an XML browser.
In various browsers XML rules are only applied when the content is known
to be XML (via an appropriate Content-Type HTTP header).

> They consider
> empty-element tag <div/> equal to <div>.


In tag soup mode.

No f'up2 set, because it may be interesting for both groups.
--
Johannes Koch
In te domine speravi; non confundar in aeternum.
(Te Deum, 4th cent.)


Jukka K. Korpela 08-28-2003 08:45 AM

Re: XHTML user agent behavior regarding empty elements
 
moo@sneakmail.zzn.com (Mikko Ohtamaa) wrote:

> From XHTML specification:
>
> C.3. Element Minimization and Empty Element Content
> Given an empty instance of an element whose content model is not
> EMPTY (for example, an empty title or paragraph) do not use the
> minimized form (e.g. use <p> </p> and not <p />).


I think it needs to be mentioned that the HTML 4.01 specification
explicitly frowns upon empty paragraphs and says authors should not use
them and browsers shoulds ignore them. It's not clear whether <p> </p> is
empty or not; a space character as the content is not the same as lack of
content (and the common construct <p>&nbsp;</p> that various programs
spit out is a yet another thing).

> A little testing shows that this is not the case. Both IE 5.5 and
> Netscape 7.0 fail to render following XHTML code correctly. They
> consider empty-element tag <div/> equal to <div>.


No wonder. And rumors say that there are even some small browsers that
process the construct <div/> _correctly_ by HTML rules as valid up to and
including HTML 4.01, namely as equivalent to <div>> (where the second
greater than sign is a data character).

> This is nuisance, since when you are producing XHTML from XML with
> XSLT transform, XSLT transformers present empty elements using
> empty-element tag notation. You must use external postprocessor to
> change <div/> elements to <div></div> pairs.


Why do you generate elements with empty content in the first place?
What is the meaning of a <div> element with no content, give that the
<div> element has no semantics except in the abstract sense that it
constitutes a block-element element?

Empty elements are extremely confusing, see
http://www.cs.tut.fi/~jkorpela/html/empty.html

--
Yucca, http://www.cs.tut.fi/~jkorpela/
Pages about Web authoring: http://www.cs.tut.fi/~jkorpela/www.html


Johannes Koch 08-29-2003 08:57 AM

Re: XHTML user agent behavior regarding empty elements
 
Mikko Ohtamaa wrote:
> I am using MSXML (Microsoft XML engine) to transform XML data to XHTML
> reports.


Why do you want to create _X_HTML reports, when several browsers don't
know about _X_HTML. Produce HTML instead.

> In XSLT it is too heavy to check if each element will be empty and
> implement a wrapper for it.


<xsl:template match="foo">
<xsl:if test="normalize-space(.) != ''">
<div class="{local-name()}">
<xsl:value-of select="."/>
</div>
</xsl:if>
</xsl:template>

Is this really too heavy?

xpost and f'up2 ctx
--
Johannes Koch
In te domine speravi; non confundar in aeternum.
(Te Deum, 4th cent.)


David Madore 09-01-2003 09:52 PM

Re: XHTML user agent behavior regarding empty elements
 
Mikko Ohtamaa in litteris
<26caea2b.0308272335.3f47cef1@posting.google.com > scripsit:
> From XML point of view <div/> and <div></div> are equal. However, XHTML,
> which should be valid XML, recommends(?) to use <div></div> only. Should
> XHTML browsers accept empty-element tags?


If the document is served with MIME content-type
"application/xhtml+xml", then <div /> _must_ be treated as equivalent
to <div></div>; on the other hand, if the document is served with MIME
content-type "text/html", then the browser is free to treat the
content as a soup of tag.

See <URL: http://www.w3.org/TR/xhtml-media-types/ > for more
information.

> A little testing shows that this is not the case. Both IE 5.5 and Netscape
> 7.0 fail to render following XHTML code correctly. They consider
> empty-element tag <div/> equal to <div>.


Mozilla (and Mozilla derivatives, such as Netscape7) treat <div/> as
equivalent to <div> when parsing the document as HTML, but as
<div></div> when parsing it as XHTML. The difference is determined by
the MIME content-type as explained above, or, in the absence of
higher-level protocol information, by the extension.

Note that Mozilla is about the only browser which supports the
application/xhtml+xml content-type anyway.

> This is nuisance, since when you are producing XHTML from XML with XSLT
> transform, XSLT transformers present empty elements using empty-element
> tag notation. You must use external postprocessor to change <div/>
> elements to <div></div> pairs.


Simply use <xsl:comment> to create a comment inside the <div> element
if it has any chance of being empty: this will prevent it from being
minimized. I use "<!-- EMPTY -->" in this context.

--
David A. Madore
(david.madore@ens.fr,
http://www.eleves.ens.fr:8080/home/madore/ )

Alan J. Flavell 09-01-2003 10:02 PM

Re: XHTML user agent behavior regarding empty elements
 
On Mon, Sep 1, David Madore inscribed on the eternal scroll:

> Simply use <xsl:comment> to create a comment inside the <div> element
> if it has any chance of being empty: this will prevent it from being
> minimized. I use "<!-- EMPTY -->" in this context.


The div element is designed to contain, well, "content". If there
isn't any content, then it's semantically meaningless (syntax or no
syntax). Surely the logical move would be to take it out, rather than
looking for other kinds of content-free clutter to stick into it?

(I did once have a program that ran faster by inserting a NOP, but
that's a different story entirely.)

all the best

John Bokma 09-01-2003 10:43 PM

Re: XHTML user agent behavior regarding empty elements
 
Alan J. Flavell wrote:

> (I did once have a program that ran faster by inserting a NOP, but
> that's a different story entirely.)


Quad word alignment pops up :-)

--
Kind regards, feel free to mail: mail(at)johnbokma.com (or reply)
virtual home: http://johnbokma.com/ ICQ: 218175426
John web site hints: http://johnbokma.com/websitedesign/


David Madore 09-01-2003 11:29 PM

empty <div> and <span> elements (was: Re: XHTML user agent behavior...)
 
"Alan J. Flavell" in litteris
<Pine.LNX.4.53.0309012358440.16153@lxplus096.cern. ch> scripsit:
> The div element is designed to contain, well, "content". If there
> isn't any content, then it's semantically meaningless (syntax or no
> syntax). Surely the logical move would be to take it out, rather than
> looking for other kinds of content-free clutter to stick into it?


Generally speaking, I agree with you. There are rare cases, however,
where I find an empty <div> or <span> element to be useful and
appropriate. Here's one:

<div style="border: solid">
<img src="pornpicture.jpg" width="120" height="240"
alt="[Highly erotic image]" style="float: left" />
<p>To the left is a picture of me. Blah, blah, blah.</p>
<div style="clear: both"><!-- EMPTY --></div>
</div>

- in other words, the empty <div> is used to make sure that the border
of the outer <div> fully goes around the image even if the text is too
short for that.

Another case is when you want to style an element using the CSS
"content" property: sometimes there is nothing else to put in the
element. One intereting hack consists of using the CSS "content"
property on an empty <span> element as it seems to be the only way to
include foreign text in an HTML document without embedding it.
Similarly, using the Mozilla-invented XBL language it might turn out
to be useful to bind to empty <div> or <span> elements.

Another case is when the <div> or <span> element starts empty, but
receives dynamical content through the Document Object Model, e.g.,
via ECMAscript. Of course, the DOM might be used to create the <div>
or <span> element itself, but it might then be a major hassle to get
it in the right place, whereas an empty <div> or <span> element with a
correct id tag is so simple to locate in the DOM!

Speaking of which, of course, an empty <div> might be useful if you
want several anchors pointing to the same place in an HTML document.
It isn't very elegant, and I would advise against it in general, but
sometimes it seems to be the right thing to do.

But, again, in general, I agree with you: unless content generation
makes it very hard to tell in advance whether the <div> will be empty,
it is better to leave out empty <div>s.

Besides, I was using <div> just as an example: there are other
possibly empty tags to which the poster's question might validly
apply. <script> springs to my mind. (Unfortunately, as far as
<script> goes, there is the nasty problem of XML's PCDATA versus
SGML's CDATA content...)

--
David A. Madore
(david.madore@ens.fr,
http://www.eleves.ens.fr:8080/home/madore/ )

Jukka K. Korpela 09-02-2003 05:51 AM

Re: empty <div> and <span> elements (was: Re: XHTML user agent behavior...)
 
david.madore@ens.fr (David Madore) wrote:

> There are rare cases, however,
> where I find an empty <div> or <span> element to be useful and
> appropriate.


Let's see you examples:

> <div style="clear: both"><!-- EMPTY --></div>


You should assign clear: both to the next element. If there is no next
element in the document, no clearing is needed.

> Another case is when you want to style an element using the CSS
> "content" property:


The content property applies to :before and :after pseudo-elements only,
so you just need to select whether you wish to have the text inserted
before or after some text in the document.

> One intereting hack consists of using the CSS "content"
> property on an empty <span> element as it seems to be the only way to
> include foreign text in an HTML document without embedding it.


Would that really fall within the principle of using CSS for optional
presentational suggestions? It's hardly a good argument in favor of
something that it would be needed for a hack that shouldn't be used. But
even for such a hack, you can simply assign the content property to a
suitable pseudo-element (as you need to do anyway, but the point is that
the pseudo-element can be derived from a real element, as opposite to an
artificial element with empty content).

> Similarly, using the Mozilla-invented XBL language it might turn out
> to be useful to bind to empty <div> or <span> elements.


A similar case indeed, except that you're referring to a browser-specific
invention, it seems.

> Another case is when the <div> or <span> element starts empty, but
> receives dynamical content through the Document Object Model, e.g.,
> via ECMAscript.


This is the kind of emptyness that potentially makes sense in SGML-based
markup, but whether it makes sense in authoring for the WWW is less clear.

> Of course, the DOM might be used to create the <div>
> or <span> element itself,


I think you just objected your own example. If scripting is actually used
to change the document's structure by adding elements, why would you hide
this with making them technically static?

> Speaking of which, of course, an empty <div> might be useful if you
> want several anchors pointing to the same place in an HTML document.
> It isn't very elegant, and I would advise against it in general, but
> sometimes it seems to be the right thing to do.


The need still needs to be proven.

--
Yucca, http://www.cs.tut.fi/~jkorpela/
Pages about Web authoring: http://www.cs.tut.fi/~jkorpela/www.html



All times are GMT. The time now is 03:09 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.