Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DTD: 0, 1 or 2 Elements

Tags:

xml

dtd

I need a DTD which allows an element A to have 0, 1 or 2 child-elements B. I tried it with

<!ELEMENT A (B? |(B,B))>

but then i get an error:

validity error : Content model of A is not determinist

what is the problem and how can i solve it? or isn't it possible to solve it with DTD?

like image 818
SPie Avatar asked May 31 '26 08:05

SPie


1 Answers

Content models such as

(B? |(B,B))

or

(B?, B?)

or

(B? | (B,B) | B)

are non-deterministic; the parser cannot tell which B is being matched without looking ahead to see what follows it.

Non-deterministic content models are disallowed in the XML 1.0 recommendation: http://www.w3.org/TR/xml/#determinism. However, the determinism requirement is there for compatibility with SGML. I guess that this may help in explaining differences in behaviour between XML parsers:

  • xmllint (libxml2) is more "draconian" and reports all of the above content models as errors ("Content model of A is not determinist").
  • Xerces-J (the parser used in Oxygen, btw) presumably does not attempt to be compatible with SGML and accepts them.

See also: What is the reason for not allowing non-deterministic element declarations in DTDs and XSD schemas?

like image 192
mzjn Avatar answered Jun 01 '26 22:06

mzjn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!