problem with child text node when constraining other child node types

B

Bryan Ax

I'm struggling with whether or not it is possible to represent the
following construct in a dtd.

I have an element X that I want to contain 3 types of child elements.
Child Element A should have a 0 or 1 constraint, Child element B should
have a 0-n constraint. I also want X to be able to contain text,
resulting in xml that the following

<X>
<A/>
<B/>
<B/>
child text of element X also possible
</X?>

I've got the following, but can't seem to figure out a way to also
allow for a text child node. Any help appreciated

<!ELEMENT X (A?, B*)>

Bryan
 
B

Bryan Ax

Looking at this, I don't see a way to constrain it so that A only can
appear once, whereas B can appear multiple times...I think I'm missing
something.
 
M

Martin Honnen

Bryan Ax wrote:

I have an element X that I want to contain 3 types of child elements.
Child Element A should have a 0 or 1 constraint, Child element B should
have a 0-n constraint. I also want X to be able to contain text,

All you can do with mixed contents is described here
<http://www.w3.org/TR/REC-xml/#sec-mixed-content>
So you could have
<!ELEMENT X (#PCDATA | A | B)*>
which would define the possible child elements (A, B) but does not allow
you to constrain their number.
 
J

Joe Kesselman

Bryan said:
Looking at this, I don't see a way to constrain it

That's correct. Mixed content in DTDs doesn't allow constraining the
number or order of instances of child elements, only their types. Live
with that and constrain it in your application code, or switch from DTDs
to schemas, or (as you suggest) move the text into an element.
 
P

Peter Flynn

Bryan said:
I'm struggling with whether or not it is possible to represent the
following construct in a dtd.

I have an element X that I want to contain 3 types of child elements.
Child Element A should have a 0 or 1 constraint, Child element B should
have a 0-n constraint. I also want X to be able to contain text,
resulting in xml that the following

<X>
<A/>
<B/>
<B/>
child text of element X also possible
</X?>

I've got the following, but can't seem to figure out a way to also
allow for a text child node. Any help appreciated

<!ELEMENT X (A?, B*)>

You can't do this in XML, only in SGML.
The content model in SGML would be

<!element x - - (a?,b*,#pcdata)>

but the XML spec says in Mixed Content, #PCDATA must come first,
and in any case cannot be used in a sequence group.

Is there a reason why this model should use Mixed Content? It's
normally only used for text documents, where the separator is
the vertical bar, eg <!ELEMENT X (#PCDATA|A|B)*> (like HTML
paragraphs). As you need to provide 0/1 and 0/+ constraints,
it looks like you are modelling data, not text. In that case,
put the text in a container, eg

<!ELEMENT X (A?,B*,C)>
<!ELEMENT C (#PCDATA)>

This will be much more robust, easier to process, and avoids
any unpleasantness with line-ends in pernicious mixed content.

///Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top