Search code examples
xmldtd

Is this the right way to make a dtd?


if i have an xml file like this:

<books>
    <book>

        <title></title>
        <subtitle></subtitle>
        <info language="">

            <pages></pages>
            <chapters></chapters>

        </info>

        <author></author>

    </book>
    .
    .
    .
</books>

which one of this is the right dtd? FIRST WAY

    <!ELEMENT books(book+)>
    <!ELEMENT book(title,subtitle,info,author)>
    <!ELEMENT title (#PCDATA)>
    <!ELEMENT subtitle (#PCDATA)>
    <!ELEMENT info(pages,chapters)>
    <!ELEMENT pages (#PCDATA)>
    <!ELEMENT chapters(#PCDATA)>
    <!ELEMENT author(#PCDATA)>
    <!ATTLIST info language CDATA #REQUIRED>

SECOND WAY

   <!ELEMENT books(book+)>
   <!ELEMENT book(title,subtitle,info,author)>
   <!ELEMENT info(pages,chapters)>
   <!ELEMENT title (#PCDATA)>
   <!ELEMENT subtitle (#PCDATA)>
   <!ELEMENT pages (#PCDATA)>
   <!ELEMENT chapters(#PCDATA)>
   <!ELEMENT author(#PCDATA)>
   <!ATTLIST info language CDATA #REQUIRED>

So my questions are:

  1. If i have a nested node do i write it at the start or as soon is encounter it?
  2. Can i write attributes at the bottom or as soon is encounter it?

thanx if anyone will reply and sorry for my english.


Solution

  • Both cases do work after you add some spaces:
    The first one can be:

    <!DOCTYPE stylesheet [
        <!ELEMENT books (book+)>
        <!ELEMENT book (title,subtitle,info,author)>
        <!ELEMENT title (#PCDATA)>
        <!ELEMENT subtitle (#PCDATA)>
        <!ELEMENT info (pages,chapters)>
        <!ELEMENT pages (#PCDATA)>
        <!ELEMENT chapters (#PCDATA)>
        <!ELEMENT author (#PCDATA)>
        <!ATTLIST info language CDATA #REQUIRED>
    ]>
    <books>
        <book>
            <title></title>
            <subtitle></subtitle>
            <info language="">
                <pages></pages>
                <chapters></chapters>
            </info>
            <author></author>
        </book>
        .
        .
        .
    </books>
    

    And the second one can be:

    <!DOCTYPE stylesheet [
       <!ELEMENT books (book+)>
       <!ELEMENT book (title,subtitle,info,author)>
       <!ELEMENT info (pages,chapters)>
       <!ELEMENT title (#PCDATA)>
       <!ELEMENT subtitle (#PCDATA)>
       <!ELEMENT pages (#PCDATA)>
       <!ELEMENT chapters (#PCDATA)>
       <!ELEMENT author (#PCDATA)>
       <!ATTLIST info language CDATA #REQUIRED>
    ]>
    <books>
        <book>
            <title></title>
            <subtitle></subtitle>
            <info language="">
                <pages></pages>
                <chapters></chapters>
            </info>
            <author></author>
        </book>
        .
        .
        .
    </books>
    

    In both cases you have to add a space after the element's name.
    The result is the same in both cases.
    That's the whole thing you need to fix/change.