views:

23

answers:

2

A related node here was - http://stackoverflow.com/questions/152313/xml-attributes-vs-elements but that was more a design question.

My question is about validity.

If a schema requires

<record name="foo" description="bar" />

and I supply

<record>
  <name>foo</name>
  <description>bar</description>
</record>

Will the XML be considered invalid?

And vice versa: if elements were specified and I provide attributes, is it invalid?

Is there a reference in an XML spec documenting what an XML parser should do in this case, where the semantics is the same, but the literal XML structure differs?

A: 

Attributes and elements are in no way interchangeable. If the schema requires one, using the other is not valid, and the XML schema validation logic should reject it. Whether they are semantically identical is also highly questionable.

skaffman
+1  A: 

If an XML schema says that is needs an element and you provide an attribute then that is invalid. The converse is the same. If you don't verify the XML, your code will still have problems because you will be looking for an attribute and it will not find the element.

Jeff Hornby