We help IT Professionals succeed at work.

Using external DTD to define entities

Last Modified: 2013-11-19
I am trying to use a DTD to define a set of entities. I am not interested in performing any other kind of validation. A number of documents use these entities, so I want them to all be defined in the same place. My DTD file looks like this:

  <?xml version="1.0" encoding="UTF-8"?>
  <!ENTITY entity1 "declaration1">
  <!ENTITY entity2 "declaration2">
  <!ENTITY entity3 "declaration3">

Based on other answers I have read on Experts Exchange, I have also tried this:

  <?xml version="1.0" encoding="UTF-8"?>
  <!ENTITY % entity1 SYSTEM "declaration1">
  <!ENTITY % entity2 SYSTEM "declaration2">
  <!ENTITY % entity3 SYSTEM "declaration3">

XMLSpy declares these to be well-formed, but my browser (Firefox and IE) won't parse or display the DTD. IE gives me an error like this:

Cannot have a DTD declaration outside of a DTD. Error processing resource 'http://www.mysite.com/entities.dt...

  <!ENTITY % entity1 SYSTEM "declaration1">

I have two questions:
  1) If I want to use a DTD this way, how should it be formatted? (I don't want any ELEMENT declarations or any sort of well-formedness rules) Is there something other than a DTD I could use to do this?
  2) What declaration should I put in my XML files for them to include the entity definitions? In particular, does this document have a DOCTYPE? If so, what is it?

As I understand, I need to use a declaration like this:
<!DOCTYPE type SYSTEM  http://www.mysite.com/entities.dtd>

but I don't know exactly what to put here.

I have done this using internal DTDs and it works fine, but I want to centralize all of these entity declarations so they're easier to track and change. How can I do this?

Thank You.
Watch Question

Gertone (Geert Bormans)Information Architect
Top Expert 2006

Hi lfgreen,
> I have done this using internal DTDs and it works fine, but I want to
> centralize all of these entity declarations so they're easier to track
> and change. How can I do this?

basically this only works in an internal subset

since you don't want to "validate" (it is validating with element rules and atribute declarations)
you are just checking wellformedness rules

The parser should not care about any rules specified in a DTD unless they are in the internal subset
Basically you can only force the wellformedness checker to "read" the entity declarations if you set the standalone to yes

like in this example

<?xml version="1.0" encoding="UTF-8" standalone='yes' ?>
<!DOCTYPE test SYSTEM "entity.dtd">
<test>text &entity1;</test>

The parser will now complain about the general entity &entity1; not being defined
because it will not check the DTD, since the document lives standalone

in this example

<?xml version="1.0" encoding="UTF-8" standalone='no' ?>
<!DOCTYPE test SYSTEM "entity.dtd">
<test>text &entity1;</test>

the parser will tell you this is well formed, because he assumes
the entity is defined, but it should not be checked

only in this example entity validity is checked
regardless of the value of standalone

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE test [
   <!ENTITY entity1 "declaration1">
  <!ENTITY entity2 "declaration2">
  <!ENTITY entity3 "declaration3">
<test>text &entity1;</test>

So if you want to use the value of the entity you should have it defined in the internal subset

note that some parsers do something in between

If you have this text
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE test SYSTEM "entity.dtd">
<test>text &entity1;</test>

and this is the content of file entity.dtd
  <!ENTITY entity1 "declaration1">
  <!ENTITY entity2 "declaration2">
  <!ENTITY entity3 "declaration3">

this XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <xsl:copy-of select="."/>

will result (with most processors) into this
<test>text declaration1</test>

and I assume that is all you are after

all syntax in this post is correct
so if in your architecture, it is sufficient to not check the existence of the entity declaration,
but just use it in the XSLT (hoping it exists then)
you can use this doctype
<!DOCTYPE test SYSTEM "entity.dtd">

If you also need to verify that the entities are declared
you need them in the internal subset,
or do full validation

if you found this line on EE
  <!ENTITY % entity1 SYSTEM "declaration1">
this one does something completely different
it defines a parameter entity, for pulling in an external text entity
don't have to go there in this example



If I understand this correctly, if I want to create a "constants library" using DTDs, this won't work. (I know this is not what they're meant to do, but it seems like nothing else can do this either.)

These documents are going to be read by an undetermined parser, so I don't want to do anything that is very parser-specific. (I don't want to assume, for instance, that a parser will be able to execute XSLT.)

I have no problem with assuming that the entities are declared. I don't really need any validation at all (the bulk of these documents is auto-generated and they already have a schema.)

I just want to be able to change the content of all of the documents by editing one of them. Can this be done, using DTDs or antyhing else?
Information Architect
Top Expert 2006
This one is on us!
(Get your first solution completely free - no credit card required)


Is there any way to do this?


I have no objection to this. I was waiting for an answer from Gertone to my final question. Even a simple "no" would have been good enough, and I would have awarded full points.
Gertone (Geert Bormans)Information Architect
Top Expert 2006


I am sorry, I abandonned this question
sometimes you miss out on one and it requires a Cleanup volunteer to wake you up
sorry about that

>Is there any way to do this?

I can't help you. I think there isn't a way to do this without stuffing them all in the internal subset directly.
I have a project running where I occasionaly just change and replace all the internally defined Entities with Python text processing
It is a bilangual thing where some values are different in the French then they are in the Flemish
so I swap from time to time using some simple regex in a python script
maybe that is a technique to use for you
if you ever need a change, take a regex script and run it over all your XML files to swap the value

that it is the end of my pot of suggestions
sorry for letting you wait so long



Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.