简体   繁体   English

如何在log4j.xml中使用DTD ENTITY外部引用

[英]How to use DTD ENTITY external reference in log4j.xml

I tried to use entities from external dtd file. 我试图使用外部dtd文件中的实体。

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

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/">

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;
]>

The XML specs specifically forbid the use of external entities in attribute values. XML规范明确禁止在属性值中使用外部实体。 See here: http://www.w3.org/TR/2004/REC-xml-20040204/#forbidden 见这里: 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. 所以答案是:XML不会让你做你想做的事情。 You might, however, get a similar effect if you ran your XML through an XSLT processor and applied transformations as needed. 但是,如果通过XSLT处理器运行XML并根据需要应用转换,则可能会产生类似的效果。

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. 您正在使用实体名称logHome用于两个不同的事物(包含声明的外部实体,应该像Daniel Haley指出的那样是一个参数实体)和一个内部实体,其替换文本命名一个目录。
  2. As a consequence, your reference to &logHome; 因此,您对&logHome;的引用&logHome; in the attribute value is understood to be a reference to the resource whose URI is " log4j-entity.dtd ". 在属性值中,应理解为对URI为“ log4j-entity.dtd ”的资源的引用。

?The simplest way to achieve what you want would be to declare the logHome entity in the internal subset: ?实现所需内容的最简单方法是在内部子集中声明logHome实体:

<?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: 如果您真的希望logHome的声明是外部的,那么为参数实体使用不同的名称可能不那么令人困惑:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM