aspose file tools*
The moose likes Product and Other Certifications and the fly likes Parameter Entities Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Product and Other Certifications
Bookmark "Parameter Entities" Watch "Parameter Entities" New topic
Author

Parameter Entities

Anup Batra
Ranch Hand

Joined: Jan 21, 2002
Posts: 41
It is written in Prof. XML that
“ Parameter Entity substitution –within a declaration- only works within the external subset of the DTD. In the internal subset, parameter entity reference can only be made between other declarations; thus the replacement text for such an entity reference must be a complete declaration, otherwise the DTD will not be well formed.”
As per this statement
Parameter Entity should be declared in Internal subset and it should be referenced in the external subset.
Now my question is why can’t both be in internal subset? It works just fine when both are in external subset.
Can anyone pls. Explain me this.

TIA
ANUP BATRA
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1175
In my opinion, the reason parameter entities can be declared only in the external subset is to keep the DTDs design clean.
But keep in mind that the internal DTD subset can specify new replacement text for the entities.
Meaning, you can _redefine_ the parameter entity!
Cheers,
Dan


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1175
You know, 'XML in a Nutshell' convinced me that what I just said is right, but the Xerces Sax parser and IE 5 rejected it.
Sorry,
Dan
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1175
Sorry again, 'XML in a nutshell' is a great book!
It works. Here are two files that show it -
---- param_entity.xml -----
<?xml version = "1.0" ?>
<!DOCTYPE message SYSTEM "param_entity.dtd" [
<!ENTITY % flg "(true | false )">
]>
<message>
<approved flag = "true"/>
<signature>Chairman</signature>
</message>
---- param_entity.dtd -----
<!ENTITY % flg "(PlaceHolder)">
<!ELEMENT approved EMPTY>
<!ATTLIST approved
flag %flg;
>
<!ELEMENT message (approved,signature)>
<!ELEMENT signature (#PCDATA)>
Anup Batra
Ranch Hand

Joined: Jan 21, 2002
Posts: 41
Hi Dan,
First of all thanks for the reply. It looks that u r a great lover of Oreilly Books. Me too.
Now coming to question. I actually want to know the reason why parameter entities cannot be defined in external subset and then referenced in internal subset?
But if PE are defined in external subset and referenced in external then there is no error since they are defined first and then referenced. Ok
Now a similar situation with parsed entities:
External dtd
<!ENTITY sct “JAVA”>
internal dtd
<!ENTITY sct1 “&sct;”> ====� here the reference is resolved why ?
pls help with the problem
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
General entities and parameter entities are processed differently.
Here is what "The XML Handbook" said:
if entities are declared more than once, the first declaration "wins" and other are forgotten. Next: internal subset of DTD is processed first. That's why we can "override" parameter entites in an internal subset.
Now why we cannot refer parameter entities in internal subset, if they are declared in external? Because parameter entities are "parsed" when they are read, i.e. at declaration time. At this time external part isn't read yet and the reference is undefined.
In contrast, "general entity references are not resolved until the entity whose declaration contains them is referenced, which usually occurs in the document instance, after all DTD declarations have been processed." That's why references to general entities that aren't defined yet are legal.
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1175
Anup,
I actually want to know the reason why parameter entities cannot be defined in external subset and then referenced in internal subset?

Parameter entities _can_ be defined in the external subset and then be referenced in the internal subset.
Please look at the following example, in which m is a parameter entity, declared in the external subset and being referenced in the internal subset.
And again, you can even redefine m in the internal subset!
---- param_entity.xml -----
<?xml version = "1.0" ?>
<!DOCTYPE message SYSTEM "param_entity.dtd" [
<!ELEMENT tst %m;>
]>
<message>
<approved flag = "true"/>
<signature>Chairman</signature>
</message>
---- param_entity.dtd -----
<!ELEMENT approved EMPTY>
<!ATTLIST approved flag (true | false) "false">
<!ELEMENT message (approved,signature)>
<!ELEMENT signature (#PCDATA)>
<!ENTITY % m "(message)">
Cheers,
Dan
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Dan, have you tried to parse your example?
The only XML agent who didn't complain was XML Spy.
Xalan (I believe it uses Xerces parser internally):
SaxParseException: Next character must be "(" starting context model for element "tst"
Internet Explorer:
Parameter entities cannot be used inside markup declarations in an internal subset. Line 4, Position 14
<!ELEMENT tst %m;>
-------------^
I modified your example and put %m; instead of <!ELEMENT tst %m;>
<?xml version = "1.0" ?>
<!DOCTYPE message SYSTEM "param_entity.dtd" [ %m; ]>
<message>
<approved flag = "true"/>
<signature>Chairman</signature>
</message>
Results:
IE:
Parameter entity must be defined before it is used. Line 2, Position 48
<!DOCTYPE message SYSTEM "param_entity.dtd" [ %m; ]>
-----------------------------------------------^
Xalan:
reference to undefined parameter entity "%m;".
XML Spy:
This file is not valid. DTD error - entity 'm' may not be used in this location.
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1175
Mapraputa,
You are right! My second example is being approved only by XML Spy.
Not considered to be well-formed by the rest.
But please let's go back to the original question, which was roughly -
Where can parameter entities be defined?
My first example, in which we redefine a parameter entity in the internal subset, is approved by Xerces Sax.
But look at this -
---- param_entity3.xml -----
<?xml version = "1.0" ?>
<!DOCTYPE message SYSTEM "param_entity3.dtd" [
<!ENTITY % m "(message)">
]>
<message>
<approved flag = "true"/>
<signature>Chairman</signature>
</message>
---- param_entity3.dtd -----
<!ELEMENT approved EMPTY>
<!ATTLIST approved flag (true | false) "false">
<!ELEMENT message (approved,signature)>
<!ELEMENT signature (#PCDATA)>
Here, m is being defined in the internal subset!
No complaints from the Xerces Sax parser.
But, m can't be used anywhere. On the other hand, redefining flg in the first example is meaningful.
Cheers,
Dan
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
I admit, I didn't understand Anup's initial question, so I answered not the question that was asked, but what I could answer I was referring to the second post:
"I actually want to know the reason why parameter entities cannot be defined in external subset and then referenced in internal subset?"
One of the references "&sct" is general entity, not parameter entity, so I decided this is a question about differences in defining general/parameter entities.
"Where can parameter entities be defined?" I suppose everywhere, if they defined before used. But now going back to the first post, I think, the question was slightly different:
Anup Batra wrote:
As per this statement
Parameter Entity should be declared in Internal subset and it should be referenced in the external subset.
Now my question is why can’t both be in internal subset?

So I surmised the question is "why parameter entities can’t both be defined and referenced in internal subset"?
The only way to *use* parameter entities in an internal subset is:
%m;
- which sunstitutes the whole declaration.
Now what sense does it make to define m in an internal subset and then use it in this fashion? It cannot be reuse, we do not need two identical declarations in DTD. It cannot be the virtue of extensibility - we cannot change a part of declaration this way. The only meaningful goal is to override corresponding declaration in an external subset.
So, answering broader set of question:
Typically parameter entites are both defined and referenced in external sunbset. Here they can be a part of markup, they do ot have to substitute the whole declaration, so reuse and extensibility are all possible.
Parameter entities can be defined in an internal subset, and they can (in principle) be both defined and referenced in internal subset, albeit this doesn't make a lot of sense. They cannot be defined in an external and referenced in an internal subset, because of "define first" rule.
Theoretically it's possible, I think, to define entity in an internal subset and refer to it in an external but this is counternatural flow of control. Internal subsets are built on the base of external, not the other way around.
The last case, when they are defined both in an internal and external subset, the declaration from an internal subset takes precedence and override one in an external - how Dan demonstrated.
Jayadev Pulaparty
Ranch Hand

Joined: Mar 25, 2002
Posts: 662
Mapraputa,
Excellent explanation of the stuff; cleared a lot of doubts from my mind;
Thanks
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
Here is an excellent and easy to read article about the very topic:
http://www-106.ibm.com/developerworks/xml/library/x-tiparam.html
Map's point is illustrated there beautifully.
Ronak Patel
Ranch Hand

Joined: Aug 14, 2002
Posts: 31
Mapraputa
Nice clarification.
Thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Parameter Entities