How to specify a version for XML DTD?

291 views Asked by At

I want to specify a version (like 1.0 or 2.0) for my DTD (defined inline). My intention is to make the parser program reject the XML document gracefully if the version is different from what it is looking for.

1

There are 1 answers

1
helderdarocha On BEST ANSWER

You can add version information to a filename. Ex: myapp_1_2.dtd if you load it from the file system or from a URL. You can parse the name and obtain the version.

If that doesn't work in your scenario, you can use a Formal Public Identifier that can contain versioning information. To use it you will need to establish an XML Catalog.

A catalog is an XML file that maps FPIs to DTDs. It might already exist in your environment or server, so you will just need to edit it and add a new entry for each DTD. If not, you might need to write a catalog file and a catalog resolver for your application, and then register it with your parser.

A catalog file has this format:

<!DOCTYPE catalog
   PUBLIC "-//OASIS/DTD Entity Resolution XML Catalog V1.0//EN"
   "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd">
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">

  <group prefer="public" xml:base="file:///server/apps/dtds/" >  3
    <public publicId="-//MY GROUP//DTD MY APP V1.2//EN" uri="myapp_1_2.dtd"/>
    <system systemId="..." uri="..."/>
    ...
  </group>
</catalog>

With this set up, you can select your DTDs via <!DOCTYPE root PUBLIC="-//MY GROUP//DTD MY APP V1.2//EN" />.

See this article which has some starting points on how to set up a resolver, and search for "XML catalog" for other sources.

See also: Formal Public Identifiers and Catalogs