tags:

views:

284

answers:

3

I don't see why order of elements is significant but not the case for attributes.

Can any one provide a sound reason?

element addressBook {
  element card {
    attribute name { text },
    attribute email { text }
  }*
}

In XML, the order of attributes is traditionally not significant. RELAX NG follows this tradition. The above pattern would match both

<card name="John Smith" email="[email protected]"/>

and

<card email="[email protected]" name="John Smith"/>

In contrast, the order of elements is significant. The pattern

element card {
  element name { text },
  element email { text }
}

would not match

<card><email>[email protected]</email><name>John Smith</name></card>

And what about this one:

If the children of an element or an attribute match a datatype pattern, then the complete content of the element or attribute must match that datatype pattern. It is not permitted to have a pattern which allows part of the content to match a datatype pattern, and another part to match another pattern. For example, the following pattern is not allowed:

element bad {
  xsd:int,
  element note { text }
}

However, this would be fine:

element ok {
  xsd:int,
  attribute note { text }
}

Note that this restriction does not apply to the text pattern.

+6  A: 

Ordering is often naturally important. Imagine if XHTML paragraphs were randomly reordered, for example. Attributes, however, are naturally a "property bag" - a set of values rather than a sequence. That's also why you can't repeat attributes with an element declaration but you can repeat elements with the same name.

This is just part of how XML is designed. Properties and elements are different in many ways, not just ordering. Sometimes it makes sense to treat elements as being order-independent, but what that means will depend on the exact situation. It would be wrong, however, to treat one situation where ordering doesn't matter as a blanket justification that it should never matter.

Jon Skeet
Thanks a lot,but I've updated my question,could you explain that too?
Shore
I'm afraid the rest of the question is RELAX NG-specific; it's not really about XML in general. I have no experience with RELAX NG.
Jon Skeet
Thank you all the same!
Shore
+7  A: 

I suspect it's because attribute names must be unique in an element, and can be disambiguated regardless of order, while contained elements need not be unique. The only way to distinguish between otherwise identical elements is by their order. This is important when order is significant. E.g.:

<li>Put on underwear</li>
<li>Put on pants.</li>

Is semantically different from

<li>Put on pants.</li>
<li>Put on underwear</li>
Dour High Arch
+1 for the visual ;)
dss539
+3  A: 

Because elements with the same name can be repeated, e.g.

<List>
    <Item>a</Item>
    <Item>b</Item>
    <Item>c</Item>
</List>

Whereas attributes cannot, e.g.

<List Item="a" Item="b" Item="c" /> <!-- illegal -->

Which means that elements are useful for lists of things, whereas attributes are not. As lists are frequently positional it makes sense to have the position inferred from the order in the XML, which is rather less confusing than something like the following (which would require special knowledge of the way the position is indicated or extra XML reserved words that are used for this, neither of which is an attractive option).

<List>
    <Item Position="2">a</Item>
    <Item Position="1">b</Item>
    <Item Position="3">c</Item>
</List>
Greg Beech