09.13.07

Validating XML Documents in Eclipse using DTD

Posted in Development at 9:21 pm by nick

Eclipse is able to validate any XML document on-the-fly if you provide a DTD. As you know, XML document is identified by one of two kinds of ID: public and system. The system identifier is basically the URL of the DTD defining the language used in the document. When you start editing an XML document, Eclipse tries to recognize the document type and match it to one of the types it knows (like one of supported XHTML flavors, Web Application Deployment Descriptor etc). However, if you are working with a language that is not known to Eclipse, it will be unable to validate the document. It will only be able to ensure that the document is well-formed.

Consider the following sample DTD:

<!ELEMENT test-root (test-header, test-body)>
<!ATTLIST test-root
	version		CDATA	#REQUIRED
>

<!ELEMENT test-header (#PCDATA)>
<!ATTLIST test-header
	id	ID	#IMPLIED
>

<!ELEMENT test-body (#PCDATA)>

If you want to be able to write a valid XML document conforming to this DTD in Eclipse, you could save this DTD in your filesystem and then specify its system ID as the pathname:

	<!DOCTYPE SYSTEM "/Users/nik/samples/test.dtd">
	...

However, this is not the best approach, ideally you should use the right system ID (or not to use it at all). At the end, this ID may be used by your application that handles these XML files.

Accessing XML Catalog Most of the XML processors have so-called "XML Catalog" that contains the mapping between the document IDs and the actual locations of the DTDs. In Eclipse you can access the catalog by opening the "Preferences" dialog, typing the word "catalog" in the search box and selecting "XML Catalog"

Adding new XML Catalog entry In this catalog you can see all the document types (with their public IDs) known to Eclipse. And you can add your own mappings here. Click on "Add.." button and fill the pop-up dialog as follows:

  • Location - the location of your DTD file (in the workspace or anywhere in the filesystem)
  • Key Type - select the "Public ID", this is the parameter Eclipse will be looking for in your document
  • Key - put the value of the public ID you want to use (quot;-//MyHowTo//Test Language 1.0//EN" in our example)

Now you can use a document like this to test the validation:

<!DOCTYPE test-root PUBLIC "-//MyHowTo//Test Language 1.0//EN"
	"http://www.myhowto.org/dtds/test.dtd">

<test-root>
	<test-header/>
</test-root>

Eclipse is validating the the document now Eclipse now takes the public ID of your XML document, looks for this key in the catalog where the key type is "Public ID" and uses the DTD from the location you have mapped to this key. You can map URIs to the locations or even the system IDs if you want. You can also manage (import and export) multiple catalogs in Eclipse.

References
  1. XML 1.0 specification
  2. XML Catalogs

5 Comments »

  1. killvin said,

    November 9, 2007 at 4:30 am

    how to define the “xml catalog” in eclipse3.2? thanks

  2. Admin said,

    November 9, 2007 at 7:17 am

    Window -> Preferences, then type “catalog” in the search box or go to “Web and XML” -> “XML Catalog” (see the first screenshot).

  3. killvin said,

    November 11, 2007 at 10:21 pm

    Do you download the “Eclipse IDE for Java Developers ” or “Eclipse Classic”?

  4. Admin said,

    November 12, 2007 at 6:56 pm

    I always used Java edition. I believe XML Editor is the part of WST (Web Standard Tools). And I am sure you can install it separately.

  5. Neeraj said,

    September 10, 2009 at 11:21 pm

    Thanks dude. I didn’t knew this and it is a new learning for me.

Leave a Comment

Spam protection by WP Captcha-Free