4

I tried to use entities from external dtd file.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" 
[<!ENTITY logHome SYSTEM "log4j-entity.dtd">]
>

log4j-entity.dtd

<?xml version="1.0" encoding="UTF-8"?>

<!ENTITY logHome "/root/crm_test/">

I tried to use the entity values in attribute values like this.

<param name="File" value="&logHome;log/info.log"/>

I get this errror:

The external entity reference "&logHome;" is not permitted in an attribute value.

How can I do this?

Note:

This thing works..

<!ENTITY logHome  "/root/crm_test/">
Mawia
  • 4,220
  • 13
  • 40
  • 55

3 Answers3

5

You need to make the entity inside the internal subset a parameter entity and then reference it.

Change:

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" 
[<!ENTITY logHome SYSTEM "log4j-entity.dtd">]
>

to:

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" [
<!ENTITY % logHome SYSTEM "log4j-entity.dtd">
%logHome;
]>
Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
1

The XML specs specifically forbid the use of external entities in attribute values. See here: http://www.w3.org/TR/2004/REC-xml-20040204/#forbidden

The following are forbidden, and constitute fatal errors: [...] a reference to an external entity in an attribute value.

So the answer is: XML won't let you do what you're trying to do. You might, however, get a similar effect if you ran your XML through an XSLT processor and applied transformations as needed.

Magnus
  • 980
  • 4
  • 10
1

There are a couple things wrong here.

  1. You are using the entity name logHome for two different things (an external entity containing declarations, which should as Daniel Haley points out be a parameter entity) and an internal entity whose replacement text names a directory.
  2. As a consequence, your reference to &logHome; in the attribute value is understood to be a reference to the resource whose URI is "log4j-entity.dtd".

The simplest way to achieve what you want would be to declare the logHome entity in the internal subset:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" [
<!ENTITY logHome "/root/crm_test/">
]>

If you really want the declaration of logHome to be external, it might be less confusing to use a different name for the parameter entity:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" [
<!ENTITY % logHomeDeclaration SYSTEM "log4j-entity.dtd">
%logHomeDeclaration;
]>
C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65