Is it possible to re-use `("0" | "1")` via a parameter entity in DTD?

37 Views Asked by At

Some companies "throw out" XML with a lengthy wordy description of their tags, but never made any effort to formally describe the structure of such mess. Thus I'm trying to build a DTD from such a description.

One thing is that 0 and 1 are used for Boolean values for several tags, so I tried to define ("0" | "1" ) as "parameter entity", but failed with various syntax errors.

My last attempt was:

<!-- Boolean -->
<!ENTITY _false "0">
<!ENTITY _true "1">
<!ENTITY % BOOL "(&_false; | &_true;)">

<!-- Error -->
<!ELEMENT Error %BOOL;>

xmllint threw out errors like this:

Entity: line 1: parser error : ContentDecl : Name or '(' expected
 %BOOL;
       ^
Entity: line 1:
(&_false; | &_true;)
 ^
Entity: line 1: parser error : expected '>'
 %BOOL;
       ^

Is it possible to use a parameter entity for that? I tried to search for examples, but failed to find some. Also my DTD practical experience is rather low.

Another Variant

Experimenting I tried this instead:

<!-- Boolean -->
<!ENTITY % BOOL '"0" | "1"'>

<!-- Error -->
<!ELEMENT Error (%BOOL;)>

Then xmllint threw out errors like this:

Entity: line 1: parser error : ContentDecl : Name or '(' expected
 %BOOL;
       ^
Entity: line 1:
"0" | "1"
^
Entity: line 1: parser error : expected '>'
 %BOOL;
       ^
Entity: line 1:
"0" | "1"
^
Entity: line 1: parser error : Content error in the external subset
 %BOOL;
       ^
Entity: line 1:
"0" | "1"
^
1

There are 1 best solutions below

3
Michael Kay On

DTDs cannot constrain the content of text nodes, only of attribute nodes.

For what you are attempting, you should be using a more powerful schema technology, for example XML Schema Definition (XSD), REgular LAnguage for XML Next Generation (RelaxNG), or Schematron.