![]() |
|
|
|||||||
![]() |
XML - problem with child text node when constraining other child node types |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
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 |
|
|
|
|
#2 |
|
Posts: n/a
|
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 |
|
|
|
#3 |
|
Posts: n/a
|
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. |
|
|
|
#4 |
|
Posts: n/a
|
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 |
|
|
|
#5 |
|
Posts: n/a
|
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/ |
|
|
|
#6 |
|
Posts: n/a
|
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. |
|
|
|
#7 |
|
Posts: n/a
|
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/ |
|