Document Type Definition - DTD / XML

Soldato
Joined
27 Jun 2006
Posts
6,331
Hey folks,

Trying to get my head around this, although anything I've stumbled upon online is a tad too complex for my actual needs.

I just wish to know if something like this is a valid DTD, using the example of Wayne Rooney, who is a player within the FA Premiership.

Code:
<!ELEMENT List (Item)+>
<!ELEMENT Item (#PCDATA)>

<fapremiershipplayers>
    <player>
         <name>Wayne Rooney</name>
         <nationality>English</nationality>
         <age>22</age>
         <size>Fat</size>
         <position>Striker</position>
    </player>
</fapremiershipplayers>

The plan of such a dtd would be that many players would fit in between the 'FA Premiership Players' brackets, but I'm unsure if this is valid practice or if I should uniquely identify each player instead.

Thanks for any help. :)
 
Hey folks,

Trying to get my head around this, although anything I've stumbled upon online is a tad too complex for my actual needs.

I just wish to know if something like this is a valid DTD, using the example of Wayne Rooney, who is a player within the FA Premiership.

Code:
<!ELEMENT List (Item)+>
<!ELEMENT Item (#PCDATA)>

<fapremiershipplayers>
    <player>
         <name>Wayne Rooney</name>
         <nationality>English</nationality>
         <age>22</age>
         <size>Fat</size>
         <position>Striker</position>
    </player>
</fapremiershipplayers>

The plan of such a dtd would be that many players would fit in between the 'FA Premiership Players' brackets, but I'm unsure if this is valid practice or if I should uniquely identify each player instead.

Thanks for any help. :)
You're best sticking to elements as you are, rather than attributes apparently.
 
Back again. This time it's a file containing reasons for choosing 3 types of dog.

Code:
<?xml version="1.0" ?>
<!DOCTYPE recommendeddogs [
<!ELEMENT recommendeddogs (#PCDATA)>
<!ELEMENT dog (group*,breed*,size*,grooming*,exercise*,locality*,lifespan*,reason*)>
<!ELEMENT group (#PCDATA)>
<!ELEMENT breed (#PCDATA)>
<!ELEMENT size (#PCDATA)>
<!ELEMENT grooming (#PCDATA)>
<!ELEMENT exercise (#PCDATA)> 
<!ELEMENT locality (#PCDATA)>
<!ELEMENT lifespan (#PCDATA)>
<!ELEMENT reason (#PCDATA)>
]>


<recommendeddogs>
        <dog>
        <group>Terrier</group>
        <breed>Jack Russell</breed>
        <size>10" Tall</size>
        <grooming>Moderate</grooming>
        <exercise>Weekly</exercise>
        <locality>Belfast</locality>
        <lifespan>7 years</lifespan>
        <reason>Jack Russells dont require much maintenance as they are a small dog. They are very loyal, fun and they enjoy exercise and walking.</reason>
        </dog>
                    <dog>
                    <group>Working</group>
                    <breed>Border Collie</breed>
                    <size>26" Tall</size>
                    <grooming>Moderate</grooming>
                    <exercise>Daily</exercise>
                    <locality>Lurgan</locality>
                    <lifespan>8 years</lifespan>
                    <reason>Border collies are very energetic dogs. They enjoy a lot of daily exercise and are affectionate. They are very well disciplined and therefore are house-trained relatively easier than other dogs.</reason>
                   </dog>
                                          <dog>
                                          <group>Hound</group>
                                          <breed>Dachshund</breed>
                                          <size>11" Tall</size>
                                          <grooming>Moderate</grooming>
                                          <exercise>Bi-Weekly</exercise>
                                          <locality>Carrickfergus</locality>
                                          <lifespan>5 years</lifespan>
                                          <reason>Dachshunds are very low maintenance when it comes to exercise as excessive amounts can cause them health problems. Their diet is carefully monitored also but they are very affectionate, loyal and fun dogs.</reason>
                                          </dog>
</recommendeddogs>

Could someone please tell me where I am going wrong with that?

The error report is:

Code:
Errors:

line 26, scratch.xml:
    error (1152): element violates enclosing tag's content model: dog 
line 36, scratch.xml:
    error (1152): element violates enclosing tag's content model: dog 
line 46, scratch.xml:
    error (1152): element violates enclosing tag's content model: dog 

Warnings:

line 13, scratch.xml:
    warning (690): end of DTD; unused element(s) detected: exercise, grooming, group, breed, reason, size, locality, dog, lifespan

I think the depression is kicking in.
 
been well over a year since i last did this but try this...
Code:
<?xml version="1.0" ?>
<!DOCTYPE recommendeddogs [
<!ELEMENT recommendeddogs (dog+)>
<!ELEMENT dog (group*,breed*,size*,grooming*,exercise*,locality*,lifespan*,reason*)>
<!ELEMENT group (#PCDATA)>
<!ELEMENT breed (#PCDATA)>
<!ELEMENT size (#PCDATA)>
<!ELEMENT grooming (#PCDATA)>
<!ELEMENT exercise (#PCDATA)> 
<!ELEMENT locality (#PCDATA)>
<!ELEMENT lifespan (#PCDATA)>
<!ELEMENT reason (#PCDATA)>
]>

You open with the recommendeddogs tag, then the dog, then the other nested items inside the dog tags. That makes me think you need the recommendeddogs definition to have (dogs+) in brackets i.e you can have many instances of dog inside the root node

The error is definitely on this line
Code:
<!ELEMENT recommendeddogs (#PCDATA)>

Because recommendeddogs is the root node it surely cannot have a value of #PCDATA
 
Last edited:
No probs at all - Had to look through it a few times before I noticed it!

Any time you have a parent tag the definition must contain a list of all the possible child nodes i.e.
Code:
<!ELEMENT dog (group*,breed*,size*,grooming*,exercise*,locality*,lifespan*,reason*)>

On nodes where there are no more child nodes then you can define the type i..e #PCDATA so thats why your code was never going to work.
Code:
<!ELEMENT size (#PCDATA)>

A good tip when checking the DTD is to ensure that for each child node there must be a reference to it inside the definition for its parent node.

Hope this helps.
 
wow, some pretty advanced looking XML there. Whenever I write it, I don't do all that DTD stuff. Anyone got any resources on this sort of thing?

Is there any need for having DTDs in XML, or is it just for well-formedness? I can't say I've ever been in a situation that needed it.
 
I think the main reasons why one would use a DTD is if agreeing on a set format for interchanging data. It ensures that the exact specification is agreed upon and thus that all transferred data is valid. For other uses of XML files such as RSS, for example, it is not necessary.
 
yeah, that's what I figured. I guess it's not really relevant for schemas created for personal use. It's one thing I should probably look into doing for little things I do that other people might see!
 
For creating my own xml files i'd just run it through a validator - this checks for "well formedness". If I am expecting information to be passed to or from a third party and need to agree a set format then I want to make sure I have a DTD - this checks for validation.

Edit: just thinking of an example of when I had to use a DTD. Was doing a project where an xml file was modified using an XSLT transform. In order for the transform to work properly the file had to be guaranteed to have the right elements and attributes. XPath was used in the XSLT also so any errors would have messed things up so the DTD was definitely a way of having an absolute guarantee that what you were doing was correct.
 
Last edited:
Back
Top Bottom