problem with child text node when constraining other child node types

Discussion in 'XML' started by Bryan Ax, May 17, 2006.

  1. Bryan Ax

    Bryan Ax Guest

    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, May 17, 2006
    #1
    1. Advertising

  2. Joe Kesselman, May 17, 2006
    #2
    1. Advertising

  3. Bryan Ax

    Bryan Ax Guest

    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.
    Bryan Ax, May 17, 2006
    #3
  4. Bryan Ax

    Bryan Ax Guest

    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
    Bryan Ax, May 17, 2006
    #4
  5. 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/
    Martin Honnen, May 17, 2006
    #5
  6. 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.
    Joe Kesselman, May 17, 2006
    #6
  7. Bryan Ax

    Peter Flynn Guest

    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/
    Peter Flynn, May 17, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kwaj
    Replies:
    1
    Views:
    612
    David Bishop
    Apr 25, 2004
  2. Replies:
    1
    Views:
    623
    Eliyahu Goldin
    Apr 17, 2005
  3. Tjerk Wolterink
    Replies:
    2
    Views:
    1,391
    Dimitre Novatchev
    Aug 24, 2006
  4. Replies:
    1
    Views:
    1,186
    Joe Kesselman
    Nov 1, 2006
  5. Beauregard T. Shagnasty

    Re: Constraining text within a border

    Beauregard T. Shagnasty, Apr 9, 2008, in forum: HTML
    Replies:
    1
    Views:
    349
    Beauregard T. Shagnasty
    Apr 9, 2008
Loading...

Share This Page