View Full Version : can a DTD specify that an element must contain data, or not exist at all

10-16-2002, 07:59 PM
here's my DTD

<!DOCTYPE pubinfo [

<!ELEMENT pubinfo (publication)*>
<!ELEMENT publication (title,url?,date?,size?,source?,author?)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT source (#PCDATA)>
<!ELEMENT author (#PCDATA)>

<!ATTLIST publication cat CDATA #REQUIRED>
<!ATTLIST publication sub CDATA #REQUIRED>
<!ATTLIST publication sba CDATA #REQUIRED>


Most of the children of <publication> are optional, but I want to specify that, for example

<url />

is not allowed - each optional tag-pair must either not be there, or contain data.

The reason I want to do this is to suit the parsing script. Is it bad practise to validate an XML so specifically, for the benefit of the parsing algorithm, or is it okay to do that? - and if so, how do I specify that in the DTD?

10-17-2002, 02:53 PM
I ws under the impression that if you specify an element contains PCDATA then it cannot be empty and still be valid.


No - I'm wrong!

Alex Vincent
10-19-2002, 02:59 AM
You can require the element have a child element. But that's about the best you'll be able to do in the DTD.

XML Schemas are much more defining and more strict, but they are incredibly complex.

If the XML document is in a Gecko-based browser like Netscape 6+ or Mozilla 1.0+, you can use a JavaScript with it:

<script xmlns="http://www.w3.org/1999/xhtml" src="myscript.js" />

-- and do your validation from there.

10-19-2002, 09:17 AM
Originally posted by Alex Vincent
you can use a JavaScript with it -- and do your validation from there.

You mean use the script to walk through the DOM and validate it like that? I hadn't thought of that ...

... but, i'm not DOM parsing it anyway. I'm doing it on the server using PHP extensions. It doesn't really validate; it just throws errors for malformed documents.

I remember reading a mention of Schema at w3schools; I think I'll check that out.

Thanks :thumbsup: