tags:

views:

133

answers:

3

As I understand the XML spec, the significance of the order of child elements is not guaranteed. XML parsers tend to keep child elements in the same order as they occur in the XML document, but they are under no obligation to do so.

If that's so, then are browsers free to render the <li>s in an <ol> or an <ul> in a different order than they occur in XHTML? Or is it specified in the XHTML spec somewhere that order has to be preserved?

I realise that all major browsers will respect the order of my <li>s. I'm just interested in the academic question of whether or not they are technically obliged to.

A: 

Even if they are under "no obligation" to do so, don't worry. Unless there are exceptional circumstances (of which I don't know even one) the HTML5/SGML/XML parser for HTML/XHTML will put elements into the DOM as they appear in the document.

Unrelated: if you're using XHTML, remember to send it as application/xhtml+xml (except for IE; check the Accept header) - such a simple thing most developers don't know.

Delan Azabani
I'm not worried that my list might show out of order in someone's browser. I'm interested from a technical point of view whether or not it would be legal to reverse the order when rendering, for example.
ctford
In the context of HTML browsers and rendering, they shouldn't for obvious reasons (even though you say DOM parsing rules allow it in general). I am unsure about the status of this issue in this context in the specs.
Delan Azabani
@Delan. There certainly are exceptional circumstances when text/html parsing processes invalid HTML. The best known is in tables. e.g. <table><tr><div>...</div><td>...</td></tr></table> will cause the div to be ejected from the table in the DOM.
Alohci
+4  A: 

According to the XHTML 1.1 Schema for list entities the contents of <ul> and <ol> is an <xs:sequence> of <li> items. Checking the XML Schema specfication it seems that <xs:sequence> operates such that "...the element information items match the particles in sequential order...". This tends to imply to me that order of such sequences is meaningful and should be retained.

JUST MY correct OPINION
Ah, so while XML element ordering is undefined, there's a constraint introduced in the XHTML schema that forces order to be significant?
ctford
+5  A: 

I think you've totally misread the XML spec there! The order of the content of an element must not be changed in the model constructed of it. (NB: the order of presentation might be a different matter, if there were specific instructions to visually reorder. But that would be weird and I don't know if current CSS can do it.) The only part of XML that is genuinely unordered is attributes; they're a true unordered map.

FWIW, the contents of an <ul> are defined to be a sequence (i.e., ordered!) of <li> elements, so browsers can't reorder when building the document tree. That is, reordering would be utterly wrong and nobody (not even the IE development team) is stupid enough to do it.

Donal Fellows
As far as I can tell, the XML 1.0 spec states the attribute order is not significant, but it doesn't specify anything about element order - http://www.ibm.com/developerworks/xml/library/x-eleord.html#N1009E
ctford
The DOM specification is clear that the child nodes of any node (which excludes attributes) are ordered; that is, the `childNodes` of a `Node` is of type `NodeList`, which is an ordered type. Theoretically, reordering is possible where permitted by the schema (it's `<xs:sequence>` vs `<xs:all>`) but neither HTML nor XHTML permit it for the content of `<ul>`.
Donal Fellows