Go Back   Velocity Reviews > Newsgroups > XML
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

XML - problem with child text node when constraining other child node types

 
Thread Tools Search this Thread
Old 05-17-2006, 03:24 PM   #1
Default problem with child text node when constraining other child node types


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



Bryan Ax
  Reply With Quote
Old 05-17-2006, 03:27 PM   #2
Joe Kesselman
 
Posts: n/a
Default Re: problem with child text node when constraining other child nodetypes

Bryan Ax wrote:
> 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


Declare X as having mixed content.
http://www.w3.org/TR/2004/REC-xml11-...-mixed-content
  Reply With Quote
Old 05-17-2006, 03:36 PM   #3
Bryan Ax
 
Posts: n/a
Default Re: problem with child text node when constraining other child node types

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.

  Reply With Quote
Old 05-17-2006, 03:56 PM   #4
Bryan Ax
 
Posts: n/a
Default Re: problem with child text node when constraining other child node types

Upon further reading, there doesn't appear to be a way to do this.
Either I go to mixed-content, which is less constraining, or I need to
add a child node to hold the text element.

http://www.devguy.com/fp/XML/dtd.htm

  Reply With Quote
Old 05-17-2006, 04:29 PM   #5
Martin Honnen
 
Posts: n/a
Default Re: problem with child text node when constraining other child nodetypes



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.


--

Martin Honnen
http://JavaScript.FAQTs.com/
  Reply With Quote
Old 05-17-2006, 05:11 PM   #6
Joe Kesselman
 
Posts: n/a
Default Re: problem with child text node when constraining other child nodetypes

Bryan Ax wrote:
> 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.
  Reply With Quote
Old 05-17-2006, 10:08 PM   #7
Peter Flynn
 
Posts: n/a
Default Re: problem with child text node when constraining other child nodetypes

Bryan Ax wrote:
> 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
--
XML FAQ: http://xml.silmaril.ie/
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump