DOCTYPE: How to postpone DTD location resolution at runtime

J

Jesse

Hi all,

my application reads and validate an XML file with a specific DTD. The two
are defined in separate files (no local declaration of the DTD in the XML).

The location of both the XML and the DTD is resolved at runtime. Indeed, due
to various deployment issues, the location of both files cannot be
statically defined. Even the relative path from the XML to its DTD cannot be
ensured.

Is there a way to parameter the DOCTYPE declaration to allow runtime setting
of the path to the DTD ?

For example using environment variables, with something like:
<!DOCTYPE greeting SYSTEM "$PATH_TO_DTD/hello.dtd">

If it exists, this is very likely to be a paser specific feature, toggled
via org.xml.sax.XMLReader setProperty or setFeature. However, I cannot find
anything relevant at
http://www.saxproject.org/apidoc/org/xml/sax/package-summary.html, nor at
http://xerces.apache.org/xerces2-j/features.html or
http://xerces.apache.org/xerces2-j/properties.html.

Indeed, I am using the XERCES SAX parser.

Thanks in advance,

JC.
 
M

Martin Honnen

Jesse said:
Is there a way to parameter the DOCTYPE declaration to allow runtime setting
of the path to the DTD ?

For example using environment variables, with something like:
<!DOCTYPE greeting SYSTEM "$PATH_TO_DTD/hello.dtd">

You could use a PUBLIC indentifier instead and resolve that in your
application. I think for that you can set up an EntityResolver which
returns an InputSoure:
<URL:http://www.saxproject.org/apidoc/org/xml/sax/EntityResolver.html>
 
R

Richard Tobin

Is there a way to parameter the DOCTYPE declaration to allow runtime setting
of the path to the DTD ?

For example using environment variables, with something like:
<!DOCTYPE greeting SYSTEM "$PATH_TO_DTD/hello.dtd">

If you can change the call to the parser you should be able to set up
an entity resolver to map it however you want to.

More portably, you could use a catalog (google for "xml catalogs").
You would either use a public identifier instead of a system
identifier, or choose some prefix for your URL that would be resolved
by the catalog.

For example, you could use:

<!DOCTYPE greeting SYSTEM "http://example.com/hello.dtd">

and have a catalog containing

<!DOCTYPE catalog PUBLIC "-//OASIS//DTD XML Catalogs V1.0//EN" "catalog.dtd">

<catalog>
<rewriteSystem systemIdStartString= "http://example.com/"
rewritePrefix= "file:///path-to-dtd-directory/" />
</catalog>

which would map the DTD location to file:///path-to-dtd-directory/hello.dtd.

Most parsers that support catalogs allow you to specify the catalog
location with an environment variable (e.g. XML_CATALOG_FILES), so you
could create a catalog on the fly and set the variable to point to it.

-- Richard
 
J

Jesse

Thanks !

Richard Tobin said:
If you can change the call to the parser you should be able to set up
an entity resolver to map it however you want to.

More portably, you could use a catalog (google for "xml catalogs").
You would either use a public identifier instead of a system
identifier, or choose some prefix for your URL that would be resolved
by the catalog.

For example, you could use:

<!DOCTYPE greeting SYSTEM "http://example.com/hello.dtd">

and have a catalog containing

<!DOCTYPE catalog PUBLIC "-//OASIS//DTD XML Catalogs V1.0//EN"
"catalog.dtd">

<catalog>
<rewriteSystem systemIdStartString= "http://example.com/"
rewritePrefix= "file:///path-to-dtd-directory/" />
</catalog>

which would map the DTD location to
file:///path-to-dtd-directory/hello.dtd.

Most parsers that support catalogs allow you to specify the catalog
location with an environment variable (e.g. XML_CATALOG_FILES), so you
could create a catalog on the fly and set the variable to point to it.

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top