tags:

views:

101

answers:

3

I'm not sure if namespaces are required for valid/correct XML. Can I add arbitrary tags to an XML document without having to define/declare a namespace for them?

I think namespaces are a good idea and are probably best practice but I just want to know if they are mandatory or not.

+3  A: 

Namespaces are not required, but at some point in the future, you'll be glad you used them.

Hank Gay
+2  A: 

Can I add arbitrary tags to an XML document without having to define/declare a namespace for them?

It depends what you mean by "valid/correct XML". For well-formedness it doesn't matter, any attribute whether in a namespace or not will be acceptable.

'Validity', on the other hand, has a specific meaning to do with schemas. Whether adding attributes to existing elements is allowable or not is decided by the schema definition of the document in question. This can be an XML Schema, or a DOCTYPE, or something else like RELAX-NG. If you have no declared schema for your document then the concept of 'validity' has no meaning and you can go ahead and drop in attributes where you like.

For example with an XHTML document of the normal doctype, it is not valid to add any attributes at all. However you can brew your own doctype from bits of XHTML with your own extensions if you want. This is what, for example, the "XHTML plus MathML plus SVG" doctype does. This uses namespaces to distinguish parts taken from each language; it is in general a good idea if you are mixing concepts from different specifications.

As Hank said, you don't need to, but it doesn't cost you anything to throw a few xmlns attributes in, and it makes a solid foundation for mixed/embedded documents.

bobince
+1  A: 

Namespaces are not striclty required in XML, however, as stated before they allow you to keep your code more organized, sort like namespaces in programming languages. If you want more specific information, you can read this W3C Recommendation in wich are explained the meanings of namespace-well-formed and namespace-valid documents.

Stefano Driussi