Automatically adding namespace declarations with IntelliJ

Often people ask us what XML editor we are using. When I say I use IntelliJ from JetBrain, the next question is inevitably "uh? I thought IntelliJ was a Java IDE".

Yes, IntelliJ is primarily a Java IDE, but it also has excellent support for JavaScript, HTML, and XML. The XML editor is schema-aware and supports compound documents where you mix elements from multiple schemas, like XHTML with XForms. When you provide IntelliJ with the schema of the document you are editing, code completion and validation will be schema aware, as you would expect. Say in an XHTML document you put a paragraph into paragraph, which is not valid in (X)HTML, the error will be highlighted right away.

Showing how IntelliJ highlights invalid XML based on the schema

For those of us who have are writing Java code, IntelliJ had for as far as I can remember provided an auto-import feature: say you use the class ArrayList without importing java.util.ArrayList. Then IntelliJ suggests to add an import java.util.ArrayList for you, and you can accept the suggestion by just pressing Alt-Enter.

IntelliJ auto-import in Java

Now IntelliJ has the same feature for namespace declaration in XML. Say you start writing <xforms:model> but you don't have any namespace declaration for the xforms prefix. After you press Alt-Enter, IntelliJ will ask if you want to create a namespace declaration. If you accept, it will add for you the xmlns:xforms="http://www.w3.org/2002/xforms" where it is needed.

IntelliJ adding a namespace declaration for XForms

IntelliJ seems to be looking all your schemas, and picking the namespace of those that define an element with a matching name. When multiple namespaces could apply, IntelliJ will give you a choice. For instance in the example below I have a config element, which could either be the start of an XPL pipeline or a page flow controller document. Hence IntelliJ gives me the choice between the two:

IntelliJ adding a namespace declaration for XForms

Want to know more about IntelliJ? Download it, check out their blog, and if you are doing open source development you can even get a free license.